diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index aa4854a6c2b..63913594234 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, archived: false, post: nil, group_archived: false) + def self.publish_private_message(topic, user_id: user_id, user_archive: false, post: nil, group_archived: false) return unless topic.private_message? channels = {} @@ -256,8 +256,10 @@ SQL channels["/private-messages/sent"] = [post.user_id] end - if archived - channels["/private-messages/archive"] = allowed_user_ids + if user_archive + user_ids = [user_id] + channels["/private-messages/archive"] = user_ids + channels["/private-messages/inbox"] = user_ids else topic.allowed_groups.each do |group| channel = "/private-messages/group/#{group.name.downcase}" @@ -274,13 +276,11 @@ SQL topic_id: topic.id } - admin_ids = User.admins.human_users.pluck(:id) - channels.each do |channel, user_ids| MessageBus.publish( channel, message.as_json, - user_ids: user_ids | admin_ids + user_ids: user_ids ) end end diff --git a/app/models/user_archived_message.rb b/app/models/user_archived_message.rb index 010e75dd604..c5932b4a7b2 100644 --- a/app/models/user_archived_message.rb +++ b/app/models/user_archived_message.rb @@ -14,7 +14,7 @@ class UserArchivedMessage < ActiveRecord::Base UserArchivedMessage.where(user_id: user_id, topic_id: topic_id).destroy_all trigger(:move_to_inbox, user_id, topic_id) MessageBus.publish("/topic/#{topic_id}", { type: "move_to_inbox" }, user_ids: [user_id]) - publish_topic_tracking_state(topic, false) + publish_topic_tracking_state(topic, user_id) end def self.archive!(user_id, topic) @@ -23,7 +23,7 @@ class UserArchivedMessage < ActiveRecord::Base UserArchivedMessage.create!(user_id: user_id, topic_id: topic_id) trigger(:archive_message, user_id, topic_id) MessageBus.publish("/topic/#{topic_id}", { type: "archived" }, user_ids: [user_id]) - publish_topic_tracking_state(topic, true) + publish_topic_tracking_state(topic, user_id) end def self.trigger(event, user_id, topic_id) @@ -36,9 +36,9 @@ class UserArchivedMessage < ActiveRecord::Base private - def self.publish_topic_tracking_state(topic, archived) + def self.publish_topic_tracking_state(topic, user_id) TopicTrackingState.publish_private_message( - topic, archived: archived + topic, user_id: user_id, user_archive: true ) end end diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb index 22cc79150fb..4553c4cd0c7 100644 --- a/spec/models/topic_tracking_state_spec.rb +++ b/spec/models/topic_tracking_state_spec.rb @@ -87,7 +87,7 @@ describe TopicTrackingState do expect(message.channel).to eq('/private-messages/inbox') expect(message.data["topic_id"]).to eq(private_message_topic.id) - expect(message.user_ids).to eq(allowed_users.map(&:id) << admin.id) + expect(message.user_ids).to eq(allowed_users.map(&:id)) end end @@ -114,7 +114,7 @@ describe TopicTrackingState do end expect(message.data["topic_id"]).to eq(private_message_topic.id) - expect(message.user_ids).to eq(group.users.map(&:id) << admin.id) + expect(message.user_ids).to eq(group.users.map(&:id)) end end end @@ -148,7 +148,7 @@ describe TopicTrackingState do end expect(message.data["topic_id"]).to eq(private_message_topic.id) - expect(message.user_ids).to eq(user_ids << admin.id) + expect(message.user_ids).to eq(user_ids) end end end @@ -158,20 +158,19 @@ describe TopicTrackingState do messages = MessageBus.track_publish do TopicTrackingState.publish_private_message( private_message_topic, - archived: true + user_id: private_message_post.user_id, + user_archive: true ) end - expect(messages.count).to eq(1) + ["/private-messages/archive", "/private-messages/inbox"].each do |channel| + message = messages.find do |message| + message.channel = channel + end - message = messages.first - - expect(message.channel).to eq('/private-messages/archive') - expect(message.data["topic_id"]).to eq(private_message_topic.id) - - expect(message.user_ids).to eq( - private_message_topic.allowed_users.map(&:id) << admin.id - ) + expect(message.data["topic_id"]).to eq(private_message_topic.id) + expect(message.user_ids).to eq([private_message_post.user_id]) + end end end