discourse/app/models/group_archived_message.rb
Alan Guo Xiang Tan bc23dcd30b
FIX: Don't publish PM archive events to acting user. (#14291)
When a user archives a personal message, they are redirected back to the
inbox and will refresh the list of the topics for the given filter.
Publishing an event to the user results in an incorrect incoming message
because the list of topics has already been refreshed.

This does mean that if a user has two tabs opened, the non-active tab
will not receive the incoming message but at this point we do not think
the technical trade-offs are worth it to support this feature. We
basically have to somehow exclude a client from an incoming message
which is not easy to do.

Follow-up to fc1fd1b416
2021-09-10 09:20:50 +08:00

66 lines
2.2 KiB
Ruby

# frozen_string_literal: true
class GroupArchivedMessage < ActiveRecord::Base
belongs_to :group
belongs_to :topic
def self.move_to_inbox!(group_id, topic, opts = {})
topic_id = topic.id
destroyed = 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, group_id, opts[:acting_user_id])
set_imap_sync(topic_id) if !opts[:skip_imap_sync] && destroyed.present?
end
def self.archive!(group_id, topic, opts = {})
topic_id = topic.id
destroyed = GroupArchivedMessage.where(group_id: group_id, topic_id: topic_id).destroy_all
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, group_id, opts[:acting_user_id])
set_imap_sync(topic_id) if !opts[:skip_imap_sync] && destroyed.blank?
end
def self.trigger(event, group_id, topic_id)
group = Group.find_by(id: group_id)
topic = Topic.find_by(id: topic_id)
if group && topic
DiscourseEvent.trigger(event, group: group, topic: topic)
end
end
def self.set_imap_sync(topic_id)
IncomingEmail.joins(:post)
.where.not(imap_uid: nil)
.where(topic_id: topic_id, posts: { post_number: 1 })
.update_all(imap_sync: true)
end
private_class_method :set_imap_sync
def self.publish_topic_tracking_state(topic, group_id, acting_user_id = nil)
PrivateMessageTopicTrackingState.publish_group_archived(
topic: topic,
group_id: group_id,
acting_user_id: acting_user_id
)
end
private_class_method :publish_topic_tracking_state
end
# == Schema Information
#
# Table name: group_archived_messages
#
# id :integer not null, primary key
# group_id :integer not null
# topic_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_group_archived_messages_on_group_id_and_topic_id (group_id,topic_id) UNIQUE
#