diff --git a/app/models/group_archived_message.rb b/app/models/group_archived_message.rb index 9bc45aa8c98..778a3944c18 100644 --- a/app/models/group_archived_message.rb +++ b/app/models/group_archived_message.rb @@ -7,7 +7,7 @@ class GroupArchivedMessage < ActiveRecord::Base GroupArchivedMessage.where(group_id: group_id, topic_id: topic_id).destroy_all trigger(:move_to_inbox, group_id, topic_id) MessageBus.publish("/topic/#{topic_id}", { type: "move_to_inbox" }, group_ids: [group_id]) - publish_topic_tracking_state(topic, false) + publish_topic_tracking_state(topic) end def self.archive!(group_id, topic) @@ -16,7 +16,7 @@ class GroupArchivedMessage < ActiveRecord::Base GroupArchivedMessage.create!(group_id: group_id, topic_id: topic_id) trigger(:archive_message, group_id, topic_id) MessageBus.publish("/topic/#{topic_id}", { type: "archived" }, group_ids: [group_id]) - publish_topic_tracking_state(topic, true) + publish_topic_tracking_state(topic) end def self.trigger(event, group_id, topic_id) @@ -29,9 +29,9 @@ class GroupArchivedMessage < ActiveRecord::Base private - def self.publish_topic_tracking_state(topic, archived) + def self.publish_topic_tracking_state(topic) TopicTrackingState.publish_private_message( - topic, group_archived: archived + topic, group_archive: true ) end end diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index 8a64ea47be9..5256a99d4a9 100644 --- a/app/models/topic_tracking_state.rb +++ b/app/models/topic_tracking_state.rb @@ -246,7 +246,7 @@ SQL sql end - def self.publish_private_message(topic, user_id: user_id, user_archive: false, post: nil, group_archived: false) + def self.publish_private_message(topic, user_id: user_id, user_archive: false, post: nil, group_archive: false) return unless topic.private_message? channels = {} @@ -266,9 +266,10 @@ SQL end else topic.allowed_groups.each do |group| - channel = "/private-messages/group/#{group.name.downcase}" - channel = "#{channel}/archive" if group_archived - channels[channel] = group.users.pluck(:id) + group_channels = [] + group_channels << "/private-messages/group/#{group.name.downcase}" + group_channels << "#{group_channels.first}/archive" if group_archive + group_channels.each { |channel| channels[channel] = group.users.pluck(:id) } end end diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb index 8413fefd095..abf7dcbdd43 100644 --- a/spec/models/topic_tracking_state_spec.rb +++ b/spec/models/topic_tracking_state_spec.rb @@ -108,6 +108,8 @@ describe TopicTrackingState do ) end + expect(messages.count).to eq(2) + [group1, group2].each do |group| message = messages.find do |message| message.channel == "/private-messages/group/#{group.name}" @@ -117,6 +119,35 @@ describe TopicTrackingState do expect(message.user_ids).to eq(group.users.map(&:id)) end end + + describe "archiving topic" do + it "should publish the right message" do + messages = MessageBus.track_publish do + TopicTrackingState.publish_private_message( + private_message_topic, + group_archive: true + ) + end + + expect(messages.count).to eq(4) + + [group1, group2].each do |group| + group_channel = "/private-messages/group/#{group.name}" + + [ + group_channel, + "#{group_channel}/archive" + ].each do |channel| + message = messages.find do |message| + message.channel == "/private-messages/group/#{group.name}" + end + + expect(message.data["topic_id"]).to eq(private_message_topic.id) + expect(message.user_ids).to eq(group.users.map(&:id)) + end + end + end + end end describe 'topic with new post' do