diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 3369bdb7d61..f45162f956c 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -427,15 +427,23 @@ class Conversation < ActiveRecord::Base (options[:only_users]).map(&:id)) if options[:only_users] skip_ids = options[:skip_users].try(:map, &:id) || [message.author_id] - skip_ids = [0] if skip_ids.empty? update_for_skips = options[:update_for_skips] != false # make sure this jumps to the top of the inbox and is marked as unread for anyone who's subscribed - cp_conditions = sanitize_sql([ - "cp.conversation_id = ? AND cp.workflow_state <> 'unread' AND (cp.last_message_at IS NULL OR cp.subscribed) AND cp.user_id NOT IN (?)", + sql = "cp.conversation_id = ? AND cp.workflow_state <> 'unread' AND (cp.last_message_at IS NULL OR cp.subscribed)" + params = [ self.id, - skip_ids - ]) + ] + if skip_ids.present? + sql += " AND cp.user_id NOT IN (?)" + params << skip_ids + end + if options[:only_users] + sql += " AND cp.user_id IN (?)" + params << (options[:only_users]).map(&:id) + end + cp_conditions = sanitize_sql(params.unshift(sql)) + if %w{MySQL Mysql2}.include?(connection.adapter_name) connection.execute <<-SQL UPDATE users, conversation_participants cp diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 992136ac14d..bbc5c95b5d0 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -283,6 +283,20 @@ describe Conversation do @unsubscribed_guy.conversations.unread.size.should eql 0 end + it "should increment only for message participants" do + root_convo = Conversation.initiate([@sender, @recipient, @subscribed_guy], false) + root_convo.add_message(@sender, 'test') + + @subscribed_guy.conversations.first.update_attribute(:workflow_state, "read") + @subscribed_guy.reload.unread_conversations_count.should eql 0 + @subscribed_guy.conversations.unread.size.should eql 0 + + root_convo.add_message(@sender, 'test2', :only_users => [@recipient]) + + @subscribed_guy.reload.unread_conversations_count.should eql 0 + @subscribed_guy.conversations.unread.size.should eql 0 + end + it "should decrement when deleting an unread conversation" do root_convo = Conversation.initiate([@sender, @unread_guy], false) root_convo.add_message(@sender, 'test')