verify :method => :post, :only => [ :reply, :destroy ], :redirect_to => { :action => :show }
verify :xhr => true, :only => :quote
-
+ helper :watchers
helper :attachments
include AttachmentsHelper
{:id => o.parent_id, :anchor => "message-#{o.id}"})}
acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}
+ acts_as_watchable
attr_protected :locked, :sticky
validates_presence_of :subject, :content
validates_length_of :subject, :maximum => 255
+ after_create :add_author_as_watcher
+
def validate_on_create
# Can not reply to a locked topic
errors.add_to_base 'Topic is locked' if root.locked? && self != root
def project
board.project
end
+
+ private
+
+ def add_author_as_watcher
+ Watcher.create(:watchable => self.root, :user => author)
+ end
end
class MessageObserver < ActiveRecord::Observer
def after_create(message)
- # send notification to the authors of the thread
- recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author && m.author.active?}
+ recipients = []
+ # send notification to the topic watchers
+ recipients += message.root.watcher_recipients
# send notification to the board watchers
recipients += message.board.watcher_recipients
# send notification to project members who want to be notified
link_to(h(@board.name), {:controller => 'boards', :action => 'show', :project_id => @project, :id => @board}) %>
<div class="contextual">
+ <%= watcher_tag(@topic, User.current) %>
<%= link_to_remote_if_authorized l(:button_quote), { :url => {:action => 'quote', :id => @topic} }, :class => 'icon icon-comment' %>
<%= link_to_if_authorized l(:button_edit), {:action => 'edit', :id => @topic}, :class => 'icon icon-edit' %>
<%= link_to_if_authorized l(:button_delete), {:action => 'destroy', :id => @topic}, :method => :post, :confirm => l(:text_are_you_sure), :class => 'icon icon-del' %>
--- /dev/null
+class SetTopicAuthorsAsWatchers < ActiveRecord::Migration
+ def self.up
+ # Sets active users who created/replied a topic as watchers of the topic
+ # so that the new watch functionality at topic level doesn't affect notifications behaviour
+ Message.connection.execute("INSERT INTO watchers (watchable_type, watchable_id, user_id)" +
+ " SELECT DISTINCT 'Message', COALESCE(messages.parent_id, messages.id), messages.author_id FROM messages, users" +
+ " WHERE messages.author_id = users.id AND users.status = 1")
+ end
+
+ def self.down
+ # Removes all message watchers
+ Watcher.delete_all("watchable_type = 'Message'")
+ end
+end
watchable_type: Issue
watchable_id: 2
user_id: 3
+watchers_002:
+ watchable_type: Message
+ watchable_id: 1
+ user_id: 1
\ No newline at end of file
require File.dirname(__FILE__) + '/../test_helper'
class MessageTest < Test::Unit::TestCase
- fixtures :projects, :boards, :messages
+ fixtures :projects, :boards, :messages, :users, :watchers
def setup
@board = Board.find(1)
# messages count incremented
assert_equal messages_count+1, @board[:messages_count]
assert_equal message, @board.last_message
+ # author should be watching the message
+ assert message.watched_by?(@user)
end
def test_reply
@message = Message.find(1)
replies_count = @message.replies_count
- reply = Message.new(:board => @board, :subject => 'Test reply', :content => 'Test reply content', :parent => @message, :author => @user)
+ reply_author = User.find(2)
+ reply = Message.new(:board => @board, :subject => 'Test reply', :content => 'Test reply content', :parent => @message, :author => reply_author)
assert reply.save
@board.reload
# same topics count
# replies count incremented
assert_equal replies_count+1, @message[:replies_count]
assert_equal reply, @message.last_reply
+ # author should be watching the message
+ assert @message.watched_by?(reply_author)
end
def test_destroy_topic
message = Message.find(1)
board = message.board
topics_count, messages_count = board.topics_count, board.messages_count
- assert message.destroy
+
+ assert_difference('Watcher.count', -1) do
+ assert message.destroy
+ end
board.reload
# Replies deleted
# Checks counters
assert_equal topics_count - 1, board.topics_count
assert_equal messages_count - 3, board.messages_count
+ # Watchers removed
end
def test_destroy_reply