2015-12-07 14:31:05 +08:00
|
|
|
|
require 'elasticsearch/model'
|
2015-04-07 13:47:39 +08:00
|
|
|
|
class Memo < ActiveRecord::Base
|
|
|
|
|
include Redmine::SafeAttributes
|
|
|
|
|
include UserScoreHelper
|
|
|
|
|
include ApplicationHelper
|
2015-12-07 14:31:05 +08:00
|
|
|
|
include Elasticsearch::Model
|
2015-04-07 13:47:39 +08:00
|
|
|
|
belongs_to :forum
|
|
|
|
|
has_many_kindeditor_assets :assets, :dependent => :destroy
|
|
|
|
|
belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
|
|
|
|
|
validates_presence_of :author_id, :forum_id, :subject,:content
|
|
|
|
|
# 若是主题帖,则内容可以是空
|
|
|
|
|
#validates :content, presence: true, if: Proc.new{|o| !o.parent_id.nil? }
|
|
|
|
|
validates_length_of :subject, maximum: 50
|
2015-11-09 11:27:05 +08:00
|
|
|
|
validates_length_of :content, maximum: 30000
|
2015-04-07 13:47:39 +08:00
|
|
|
|
validate :cannot_reply_to_locked_topic, :on => :create
|
|
|
|
|
|
2015-12-07 14:31:05 +08:00
|
|
|
|
|
|
|
|
|
#elasticsearch kaminari init
|
|
|
|
|
Kaminari::Hooks.init
|
|
|
|
|
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari
|
2015-12-11 19:34:17 +08:00
|
|
|
|
settings index: {
|
|
|
|
|
number_of_shards: 5 ,
|
|
|
|
|
analysis: {
|
|
|
|
|
char_filter: {
|
|
|
|
|
and_filter: {
|
|
|
|
|
type: "mapping",
|
|
|
|
|
mappings: [ "&=> and "]
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
analyzer: {
|
|
|
|
|
my_analyzer: {
|
|
|
|
|
type: 'custom',
|
|
|
|
|
tokenizer: 'standard',
|
|
|
|
|
filter: ['classic'],
|
|
|
|
|
char_filter: ['html_strip']
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} do
|
2015-12-07 14:31:05 +08:00
|
|
|
|
mappings dynamic: 'false' do
|
2015-12-11 19:34:17 +08:00
|
|
|
|
indexes :subject, analyzer: 'smartcn',index_options: 'offsets'#, char_filter: 'html_strip'
|
|
|
|
|
indexes :content, analyzer:'my_analyzer',index_options: 'offsets',search_analyzer: 'smartcn'
|
2015-12-09 12:57:25 +08:00
|
|
|
|
indexes :updated_at,index:"not_analyzed" ,type:'date'
|
2015-12-07 14:31:05 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-07 13:47:39 +08:00
|
|
|
|
acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC"
|
|
|
|
|
acts_as_attachable
|
|
|
|
|
has_many :user_score_details, :class_name => 'UserScoreDetails',:as => :score_changeable_obj
|
|
|
|
|
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
|
2016-09-23 16:44:49 +08:00
|
|
|
|
has_one :praise_tread_cache, as: :object, dependent: :destroy
|
|
|
|
|
|
2015-08-19 11:06:39 +08:00
|
|
|
|
# 消息
|
2015-08-19 17:29:05 +08:00
|
|
|
|
has_many :memo_messages, :class_name =>'MemoMessage', :dependent => :destroy
|
|
|
|
|
# end
|
2015-04-07 13:47:39 +08:00
|
|
|
|
belongs_to :last_reply, :class_name => 'Memo', :foreign_key => 'last_reply_id'
|
|
|
|
|
# acts_as_searchable :column => ['subject', 'content'],
|
|
|
|
|
# #:include => { :forum => :p}
|
|
|
|
|
# #:project_key => "#{Forum.table_name}.project_id"
|
|
|
|
|
# :date_column => "#{table_name}.created_at"
|
|
|
|
|
acts_as_event :title => Proc.new {|o| "#{o.forum.name}: #{o.subject}"},
|
|
|
|
|
:datetime => :updated_at,
|
|
|
|
|
# :datetime => :created_at,
|
|
|
|
|
:description => :content,
|
|
|
|
|
:author => :author,
|
|
|
|
|
:type => Proc.new {|o| o.parent_id.nil? ? 'Memo' : 'Reply'},
|
|
|
|
|
:url => Proc.new {|o| {:controller => 'memos', :action => 'show', :forum_id => o.forum_id}.merge(o.parent_id.nil? ? {:id => o.id} : {:id => o.parent_id, :r => o.id, :anchor => "reply-#{o.id}"})}
|
|
|
|
|
acts_as_activity_provider :author_key => :author_id,
|
|
|
|
|
:func => 'memos',
|
|
|
|
|
:timestamp => 'created_at'
|
|
|
|
|
# :find_options => {:type => 'memos'}
|
|
|
|
|
# acts_as_watchable
|
|
|
|
|
|
|
|
|
|
safe_attributes "author_id",
|
|
|
|
|
"subject",
|
|
|
|
|
"content",
|
|
|
|
|
"forum_id",
|
|
|
|
|
"last_memo_id",
|
|
|
|
|
"lock",
|
|
|
|
|
"sticky",
|
|
|
|
|
"parent_id",
|
2016-11-29 09:36:38 +08:00
|
|
|
|
"replies_count",
|
|
|
|
|
"root_id"
|
2015-04-07 13:47:39 +08:00
|
|
|
|
|
2016-11-30 10:58:13 +08:00
|
|
|
|
after_create :add_author_as_watcher, :reset_counters!, :send_message,:create_memo_ealasticsearch_index
|
2015-12-07 14:31:05 +08:00
|
|
|
|
after_update :update_memo_ealasticsearch_index
|
|
|
|
|
after_destroy :reset_counters!,:delete_kindeditor_assets,:delete_memo_ealasticsearch_index#,:down_user_score -- 公共区发帖暂不计入得分,
|
2015-04-07 13:47:39 +08:00
|
|
|
|
# after_create :send_notification
|
|
|
|
|
# after_save :plusParentAndForum
|
|
|
|
|
# after_destroy :minusParentAndForum
|
|
|
|
|
#before_save :be_user_score
|
|
|
|
|
# scope :visible, lambda { |*args|
|
|
|
|
|
# includes(:forum => ).where()
|
|
|
|
|
# }
|
2015-12-07 14:31:05 +08:00
|
|
|
|
scope :indexable,lambda {
|
|
|
|
|
where('parent_id is null')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def self.search(query)
|
|
|
|
|
__elasticsearch__.search(
|
|
|
|
|
{
|
|
|
|
|
query: {
|
|
|
|
|
multi_match: {
|
|
|
|
|
query: query,
|
|
|
|
|
type:"most_fields",
|
|
|
|
|
operator: "or",
|
|
|
|
|
fields: ['subject','content^0.5']
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
sort: {
|
|
|
|
|
_score:{order: "desc" },
|
|
|
|
|
updated_at:{order: "desc" }
|
|
|
|
|
},
|
|
|
|
|
highlight: {
|
|
|
|
|
pre_tags: ['<span class="c_red">'],
|
|
|
|
|
post_tags: ['</span>'],
|
|
|
|
|
fields: {
|
|
|
|
|
subject: {},
|
|
|
|
|
content: {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
end
|
2015-04-07 13:47:39 +08:00
|
|
|
|
|
|
|
|
|
def send_mail
|
2016-12-23 14:29:48 +08:00
|
|
|
|
# Mailer.run.forum_message_added(self) if Setting.notified_events.include?('forum_message_added')
|
2015-04-07 13:47:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
2016-11-29 09:36:38 +08:00
|
|
|
|
def creator_user
|
|
|
|
|
self.author
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def created_time
|
|
|
|
|
self.created_on
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def content_detail
|
|
|
|
|
self.content
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-19 11:06:39 +08:00
|
|
|
|
# 公共贴吧消息记录
|
|
|
|
|
# 原则:贴吧创始人;发帖人,wanglingchun(特殊用户)
|
|
|
|
|
def send_message
|
|
|
|
|
receivers = []
|
|
|
|
|
u = User.find(6)
|
|
|
|
|
receivers << u
|
|
|
|
|
# 主贴
|
|
|
|
|
if self.parent_id.nil?
|
|
|
|
|
if self.author_id != self.forum.creator_id # 发帖人不是吧主
|
|
|
|
|
receivers << self.forum.creator
|
|
|
|
|
end
|
|
|
|
|
else # 回帖
|
|
|
|
|
# 添加吧主
|
|
|
|
|
if self.author_id != self.forum.creator_id
|
|
|
|
|
receivers << self.forum.creator
|
|
|
|
|
end
|
|
|
|
|
# 添加发帖人
|
2015-09-30 11:31:29 +08:00
|
|
|
|
if self.author_id != self.parent.author_id && self.parent.author_id != self.forum.creator_id
|
2015-08-19 11:06:39 +08:00
|
|
|
|
receivers << self.parent.author
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
receivers.each do |r|
|
2015-08-19 17:29:05 +08:00
|
|
|
|
self.memo_messages << MemoMessage.new(:user_id => r.id, :forum_id => self.forum_id, :memo_id => self.id, :memo_type => "Memo", :viewed => false)
|
2015-08-19 11:06:39 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-07 13:47:39 +08:00
|
|
|
|
def cannot_reply_to_locked_topic
|
|
|
|
|
errors.add :base, l(:label_memo_locked) if root.locked? && self != root
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# def update_memos_forum
|
|
|
|
|
# if forum_id_changed?
|
|
|
|
|
# Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id ])
|
|
|
|
|
# Forum.reset_counters!(forum_id_was)
|
|
|
|
|
# Forum.reset_counters!(forum_id)
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
def reset_counters!
|
|
|
|
|
if parent && parent.id
|
|
|
|
|
Memo.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
|
|
|
|
|
parent.update_attribute(:updated_at, Time.now)
|
|
|
|
|
end
|
|
|
|
|
forum.reset_counters!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sticky?
|
|
|
|
|
sticky == 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def replies
|
|
|
|
|
Memo.where("parent_id = ?", id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def locked?
|
|
|
|
|
self.lock
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def editable_by? user
|
|
|
|
|
# user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))
|
|
|
|
|
user.admin? || self.author == user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroyable_by? user
|
2017-01-16 15:28:56 +08:00
|
|
|
|
(user && self.author == user) || user.admin? || self.forum.creator == user || Memo.find(self.root_id).author == user
|
2015-04-07 13:47:39 +08:00
|
|
|
|
#self.author == user || user.admin?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def deleted_attach_able_by? user
|
|
|
|
|
(user && user.logged? && (self.author == user) ) || user.admin?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def add_author_as_watcher
|
|
|
|
|
Watcher.create(:watchable => self.root, :user => author)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_notification
|
2016-12-23 14:29:48 +08:00
|
|
|
|
# if Setting.notified_events.include?('message_posted')
|
|
|
|
|
# Mailer.run.message_posted(self)
|
|
|
|
|
# end
|
2015-04-07 13:47:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def plusParentAndForum
|
|
|
|
|
@forum = Forum.find(self.forum_id)
|
|
|
|
|
@forum.memo_count = @forum.memo_count.to_int + 1
|
|
|
|
|
@forum.last_memo_id = self.id
|
|
|
|
|
if self.parent_id
|
|
|
|
|
@parent_memo = Memo.find_by_id(self.parent_id)
|
|
|
|
|
@parent_memo.last_reply_id = self
|
|
|
|
|
@parent_memo.replies_count = @parent_memo.replies_count.to_int + 1
|
|
|
|
|
@parent_memo.save
|
|
|
|
|
else
|
|
|
|
|
@forum.topic_count = @forum.topic_count.to_int + 1
|
|
|
|
|
end
|
|
|
|
|
@forum.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def minusParentAndForum
|
|
|
|
|
@forum = Forum.find(self.forum_id)
|
|
|
|
|
@forum.memo_count = @forum.memo_count.to_int - 1
|
|
|
|
|
@forum.memo_count = 0 if @forum.memo_count.to_int < 0
|
|
|
|
|
# @forum.last_memo_id = Memo.reorder('created_at ASC').find_all_by_forum_id(self.forum_id).last.id
|
|
|
|
|
if self.parent_id
|
|
|
|
|
@parent_memo = Memo.find_by_id(self.parent_id)
|
|
|
|
|
# @parent_memo.last_reply_id = Memo.reorder('created_at ASC').find_all_by_parent_id(self.parent_id).last.id
|
|
|
|
|
@parent_memo.replies_count = @parent_memo.replies_count.to_int - 1
|
|
|
|
|
@parent_memo.replies_count = 0 if @parent_memo.replies_count.to_int < 0
|
|
|
|
|
@parent_memo.save
|
|
|
|
|
else
|
|
|
|
|
@forum.topic_count = @forum.topic_count.to_int - 1
|
|
|
|
|
@forum.topic_count = 0 if @forum.topic_count.to_int < 0
|
|
|
|
|
end
|
|
|
|
|
@forum.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#更新用户分数 -by zjc
|
|
|
|
|
def be_user_score
|
|
|
|
|
#新建memo且无parent的为发帖
|
|
|
|
|
if self.parent_id.nil?
|
|
|
|
|
UserScore.joint(:post_message, User.current,nil,self ,{ memo_id: self.id })
|
|
|
|
|
update_memo_number(User.current,1)
|
|
|
|
|
|
|
|
|
|
#新建memo且有parent的为回帖
|
|
|
|
|
elsif !self.parent_id.nil?
|
|
|
|
|
UserScore.joint(:reply_posting, User.current,self.parent.author,self, { memo_id: self.id })
|
|
|
|
|
update_replay_for_memo(User.current,1)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#被删除时更新用户分数
|
|
|
|
|
def down_user_score
|
|
|
|
|
update_memo_number(User.current,1)
|
|
|
|
|
update_replay_for_memo(User.current,1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Time 2015-03-26 15:20:24
|
|
|
|
|
# Author lizanle
|
|
|
|
|
# Description 从硬盘上删除资源
|
|
|
|
|
def delete_kindeditor_assets
|
2015-12-07 14:31:05 +08:00
|
|
|
|
delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::MEMO
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_memo_ealasticsearch_index
|
|
|
|
|
if self.parent_id.nil?
|
|
|
|
|
self.__elasticsearch__.index_document
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
def update_memo_ealasticsearch_index
|
|
|
|
|
if self.parent_id.nil?
|
|
|
|
|
self.__elasticsearch__.update_document
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
def delete_memo_ealasticsearch_index
|
|
|
|
|
if self.parent_id.nil?
|
|
|
|
|
self.__elasticsearch__.delete_document
|
|
|
|
|
end
|
2015-04-07 13:47:39 +08:00
|
|
|
|
end
|
|
|
|
|
end
|