socialforge/app/models/blog_comment.rb

124 lines
4.0 KiB
Ruby

class BlogComment < ActiveRecord::Base
# attr_accessible :title, :body
require 'net/http'
require 'json'
include Redmine::SafeAttributes
belongs_to :blog
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
acts_as_tree :counter_cache => :comments_count, :order => "#{BlogComment.table_name}.sticky desc ,#{BlogComment.table_name}.created_on ASC"
acts_as_attachable
belongs_to :last_reply, :class_name => 'BlogComment', :foreign_key => 'last_comment_id'
# 虚拟关联
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
acts_as_watchable
validates_presence_of :title, :content
validates_length_of :title, :maximum => 255
#validate :cannot_reply_to_locked_comment, :on => :create
safe_attributes 'title', 'content',"sticky", "locked"
after_save :add_user_activity
after_update :update_activity
after_create :update_parent_time, :blog_wechat_message
before_destroy :destroy_user_activity
scope :like, lambda {|arg|
if arg.blank?
where(nil)
else
pattern = "%#{arg.to_s.strip.downcase}%"
where(" LOWER(title) LIKE :p ", :p => pattern)
end
}
#动态更新
def update_activity
user_activity = UserActivity.where("act_type='BlogComment' and act_id =?",self.id).first
if user_activity
user_activity.updated_at = Time.now
user_activity.save
end
end
#在个人动态里面增加当前动态
def add_user_activity
if self.parent_id.nil? #只有发博文才插入动态
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'").first
if user_activity
user_activity.save
else
user_activity = UserActivity.new
user_activity.act_id = self.id
user_activity.act_type = self.class.to_s
user_activity.container_type = "Blog"
user_activity.container_id = self.blog_id
user_activity.user_id = self.author_id
user_activity.save
end
end
end
def destroy_user_activity
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'")
user_activity.destroy_all
end
def deleted_attach_able_by? user
(user && user.logged? && (self.author == user) ) || user.admin?
end
def update_parent_time
if !self.parent.nil?
self.root.update_attribute(:updated_on, self.updated_on)
end
end
def project
end
#博客回复微信模板消息
def blog_wechat_message
unless self.parent_id == nil
uw = UserWechat.where(user_id: self.parent.author_id).first
#unless uw.nil? && self.parent.author_id != User.current.id
unless uw.nil?
data = {
touser:uw.openid,
template_id:"A_3f5v90-zK73V9Kijm-paDkl9S-NuM8Cf-1UJi92_c",
url:"http://weixin.qq.com/download",
topcolor:"#FF0000",
data:{
first: {
value:"您的博客有新回复了",
color:"#173177"
},
keyword1:{
value:self.author.try(:realname),
color:"#173177"
},
keyword2:{
value:self.created_at,
color:"#173177"
},
keyword3:{
value:h(truncate(" - #{self.content.html_safe}", length:50, omission: '...')),
color:"#173177"
},
remark:{
value:"具体内容请点击详情查看网站",
color:"#173177"
}
}
}
uri = URI("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=1234567")
body = data.to_json
res = Net::HTTP.new(uri.host, uri.port).start do |client|
request = Net::HTTP::Post.new(uri.path)
request.body = body
request["Content-Type"] = "application/json"
client.request(request)
end
end
end
end
end