2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-22 19:09:17 -05:00
|
|
|
class UserArchivedMessage < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2016-02-07 07:39:07 -05:00
|
|
|
|
2018-03-06 01:38:43 -05:00
|
|
|
def self.move_to_inbox!(user_id, topic)
|
|
|
|
topic_id = topic.id
|
|
|
|
|
2016-02-14 18:56:39 -05:00
|
|
|
return if (TopicUser.where(
|
|
|
|
user_id: user_id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
notification_level: TopicUser.notification_levels[:muted]
|
|
|
|
).exists?)
|
|
|
|
|
2016-02-07 07:39:07 -05:00
|
|
|
UserArchivedMessage.where(user_id: user_id, topic_id: topic_id).destroy_all
|
2017-02-28 09:56:41 -05:00
|
|
|
trigger(:move_to_inbox, user_id, topic_id)
|
2017-07-27 21:20:09 -04:00
|
|
|
MessageBus.publish("/topic/#{topic_id}", { type: "move_to_inbox" }, user_ids: [user_id])
|
2018-03-06 22:28:29 -05:00
|
|
|
publish_topic_tracking_state(topic, user_id)
|
2016-02-07 07:39:07 -05:00
|
|
|
end
|
|
|
|
|
2018-03-06 01:38:43 -05:00
|
|
|
def self.archive!(user_id, topic)
|
|
|
|
topic_id = topic.id
|
2016-02-07 07:39:07 -05:00
|
|
|
UserArchivedMessage.where(user_id: user_id, topic_id: topic_id).destroy_all
|
|
|
|
UserArchivedMessage.create!(user_id: user_id, topic_id: topic_id)
|
2017-02-28 09:56:41 -05:00
|
|
|
trigger(:archive_message, user_id, topic_id)
|
2017-07-27 21:20:09 -04:00
|
|
|
MessageBus.publish("/topic/#{topic_id}", { type: "archived" }, user_ids: [user_id])
|
2018-03-06 22:28:29 -05:00
|
|
|
publish_topic_tracking_state(topic, user_id)
|
2016-02-07 07:39:07 -05:00
|
|
|
end
|
2017-02-28 09:56:41 -05:00
|
|
|
|
|
|
|
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
|
2017-07-27 21:20:09 -04:00
|
|
|
DiscourseEvent.trigger(event, user: user, topic: topic)
|
2017-02-28 09:56:41 -05:00
|
|
|
end
|
|
|
|
end
|
2018-03-06 01:38:43 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def self.publish_topic_tracking_state(topic, user_id)
|
|
|
|
TopicTrackingState.publish_private_message(
|
|
|
|
topic, archive_user_id: user_id
|
|
|
|
)
|
|
|
|
end
|
2015-12-22 19:09:17 -05:00
|
|
|
end
|
2016-01-11 01:30:56 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_archived_messages
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# topic_id :integer not null
|
2019-01-11 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2016-01-11 01:30:56 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_archived_messages_on_user_id_and_topic_id (user_id,topic_id) UNIQUE
|
|
|
|
#
|