2013-08-01 10:33:49 +08:00
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class Message < ActiveRecord::Base
|
|
|
|
include Redmine::SafeAttributes
|
|
|
|
belongs_to :board
|
|
|
|
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
|
|
|
|
acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
|
|
|
|
acts_as_attachable
|
|
|
|
belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
|
2013-08-12 15:37:42 +08:00
|
|
|
|
|
|
|
# added by fq
|
|
|
|
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
|
|
|
|
# end
|
2013-08-01 10:33:49 +08:00
|
|
|
|
|
|
|
acts_as_searchable :columns => ['subject', 'content'],
|
|
|
|
:include => {:board => :project},
|
|
|
|
:project_key => "#{Board.table_name}.project_id",
|
|
|
|
:date_column => "#{table_name}.created_on"
|
2014-06-06 13:56:48 +08:00
|
|
|
acts_as_searchable :columns => ['subject', 'content'],
|
|
|
|
:include => {:board => :course},
|
|
|
|
:course_key => "#{Board.table_name}.course_id",
|
|
|
|
:date_column => "#{table_name}.created_at"
|
2013-08-01 10:33:49 +08:00
|
|
|
acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
|
|
|
|
:description => :content,
|
2013-12-27 13:54:57 +08:00
|
|
|
:datetime => :updated_on,
|
2013-12-07 10:15:55 +08:00
|
|
|
# :datetime => "#{Message.table_name}.created_on",
|
2013-08-01 10:33:49 +08:00
|
|
|
:group => :parent,
|
|
|
|
:type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
|
|
|
|
:url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
|
|
|
|
{:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
|
|
|
|
|
|
|
|
acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
|
2013-12-07 10:15:55 +08:00
|
|
|
:author_key => :author_id
|
2014-06-06 13:56:48 +08:00
|
|
|
acts_as_activity_provider :find_options => {:include => [{:board => :course}, :author]},
|
2014-06-13 11:49:28 +08:00
|
|
|
:type => 'course_messages',
|
2014-06-06 13:56:48 +08:00
|
|
|
:author_key => :author_id
|
2013-08-01 10:33:49 +08:00
|
|
|
acts_as_watchable
|
|
|
|
|
|
|
|
validates_presence_of :board, :subject, :content
|
|
|
|
validates_length_of :subject, :maximum => 255
|
|
|
|
validate :cannot_reply_to_locked_topic, :on => :create
|
|
|
|
|
|
|
|
after_create :add_author_as_watcher, :reset_counters!
|
|
|
|
after_update :update_messages_board
|
|
|
|
after_destroy :reset_counters!
|
2013-08-12 15:37:42 +08:00
|
|
|
|
|
|
|
# fq
|
2013-08-12 17:30:31 +08:00
|
|
|
after_create :act_as_activity
|
2014-06-09 17:37:48 +08:00
|
|
|
before_save :be_user_score
|
2014-06-11 17:41:47 +08:00
|
|
|
#before_destroy :down_user_score
|
2013-08-12 15:37:42 +08:00
|
|
|
# end
|
2013-08-01 10:33:49 +08:00
|
|
|
|
|
|
|
scope :visible, lambda {|*args|
|
|
|
|
includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
|
|
|
|
}
|
|
|
|
|
2014-06-16 10:38:38 +08:00
|
|
|
scope :course_visible, lambda {|*args|
|
|
|
|
includes(:board => :course).where(Course.allowed_to_condition(args.shift || User.current, :view_course_messages, *args))
|
|
|
|
}
|
|
|
|
|
2014-06-06 13:56:48 +08:00
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
safe_attributes 'subject', 'content'
|
|
|
|
safe_attributes 'locked', 'sticky', 'board_id',
|
|
|
|
:if => lambda {|message, user|
|
|
|
|
user.allowed_to?(:edit_messages, message.project)
|
|
|
|
}
|
|
|
|
|
|
|
|
def visible?(user=User.current)
|
2014-06-06 13:56:48 +08:00
|
|
|
if project
|
|
|
|
!user.nil? && user.allowed_to?(:view_messages, project)
|
|
|
|
elsif course
|
|
|
|
!user.nil? && user.allowed_to?(:view_messages, course)
|
|
|
|
end
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def cannot_reply_to_locked_topic
|
|
|
|
# Can not reply to a locked topic
|
|
|
|
errors.add :base, 'Topic is locked' if root.locked? && self != root
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_messages_board
|
|
|
|
if board_id_changed?
|
|
|
|
Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id])
|
|
|
|
Board.reset_counters!(board_id_was)
|
|
|
|
Board.reset_counters!(board_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_counters!
|
|
|
|
if parent && parent.id
|
|
|
|
Message.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
|
|
|
|
end
|
2014-06-12 10:03:01 +08:00
|
|
|
board.reset_counters!
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def sticky=(arg)
|
|
|
|
write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sticky?
|
|
|
|
sticky == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
board.project
|
|
|
|
end
|
|
|
|
|
2014-06-06 13:56:48 +08:00
|
|
|
def course
|
|
|
|
board.course
|
|
|
|
end
|
|
|
|
|
2013-08-01 10:33:49 +08:00
|
|
|
def editable_by?(usr)
|
|
|
|
usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroyable_by?(usr)
|
|
|
|
usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_author_as_watcher
|
|
|
|
Watcher.create(:watchable => self.root, :user => author)
|
|
|
|
end
|
2013-08-12 15:37:42 +08:00
|
|
|
|
|
|
|
# fq
|
|
|
|
def act_as_activity
|
|
|
|
self.acts << Activity.new(:user_id => self.author_id)
|
|
|
|
end
|
|
|
|
# end
|
2014-06-09 17:37:48 +08:00
|
|
|
|
2014-06-10 17:47:10 +08:00
|
|
|
#更新用户分数 -by zjc
|
2014-06-09 17:37:48 +08:00
|
|
|
def be_user_score
|
2014-06-10 17:47:10 +08:00
|
|
|
#新建message且无parent的为发帖
|
|
|
|
if self.new_record? && self.parent_id.nil?
|
2014-06-09 17:37:48 +08:00
|
|
|
UserScore.joint(:post_message, User.current,nil, { message_id: self.id })
|
2014-06-10 17:47:10 +08:00
|
|
|
#新建message且有parent的为回帖
|
|
|
|
elsif self.new_record? && !self.parent_id.nil?
|
|
|
|
UserScore.joint(:reply_posting, User.current,self.parent.author, { message_id: self.id })
|
2014-06-09 17:37:48 +08:00
|
|
|
end
|
|
|
|
end
|
2014-06-11 17:41:47 +08:00
|
|
|
#减少用户分数
|
|
|
|
def down_user_score
|
|
|
|
#删除发帖
|
|
|
|
if self.parent_id.nil?
|
|
|
|
UserScore.joint(:delete_message, User.current,nil, { message_id: self.id })
|
|
|
|
#删除留言
|
|
|
|
else
|
|
|
|
UserScore.joint(:reply_deleting, User.current,self.parent.author, { message_id: self.id })
|
|
|
|
end
|
|
|
|
end
|
2013-08-01 10:33:49 +08:00
|
|
|
end
|