mirror of
https://github.com/discourse/discourse.git
synced 2024-12-11 17:15:47 +08:00
bc23dcd30b
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
52 lines
1.5 KiB
Ruby
52 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UserArchivedMessage < ActiveRecord::Base
|
|
belongs_to :user
|
|
belongs_to :topic
|
|
|
|
def self.move_to_inbox!(user_id, topic)
|
|
topic_id = topic.id
|
|
|
|
return if (TopicUser.where(
|
|
user_id: user_id,
|
|
topic_id: topic_id,
|
|
notification_level: TopicUser.notification_levels[:muted]
|
|
).exists?)
|
|
|
|
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])
|
|
end
|
|
|
|
def self.archive!(user_id, topic)
|
|
topic_id = topic.id
|
|
UserArchivedMessage.where(user_id: user_id, topic_id: topic_id).destroy_all
|
|
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])
|
|
end
|
|
|
|
def self.trigger(event, user_id, topic_id)
|
|
user = User.find_by(id: user_id)
|
|
topic = Topic.find_by(id: topic_id)
|
|
if user && topic
|
|
DiscourseEvent.trigger(event, user: user, topic: topic)
|
|
end
|
|
end
|
|
end
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: user_archived_messages
|
|
#
|
|
# id :integer not null, primary key
|
|
# user_id :integer not null
|
|
# topic_id :integer not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_user_archived_messages_on_user_id_and_topic_id (user_id,topic_id) UNIQUE
|
|
#
|