2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-23 08:09:17 +08:00
|
|
|
class GroupArchivedMessage < ActiveRecord::Base
|
2020-05-23 12:56:13 +08:00
|
|
|
belongs_to :group
|
2015-12-23 08:09:17 +08:00
|
|
|
belongs_to :topic
|
2016-02-07 20:39:07 +08:00
|
|
|
|
2020-07-10 17:05:55 +08:00
|
|
|
def self.move_to_inbox!(group_id, topic, opts = {})
|
2018-03-06 14:38:43 +08:00
|
|
|
topic_id = topic.id
|
2020-07-10 17:05:55 +08:00
|
|
|
destroyed = GroupArchivedMessage.where(group_id: group_id, topic_id: topic_id).destroy_all
|
2017-02-28 22:56:41 +08:00
|
|
|
trigger(:move_to_inbox, group_id, topic_id)
|
2016-02-07 20:39:07 +08:00
|
|
|
MessageBus.publish("/topic/#{topic_id}", { type: "move_to_inbox" }, group_ids: [group_id])
|
2018-03-07 14:01:20 +08:00
|
|
|
publish_topic_tracking_state(topic)
|
2020-07-10 17:05:55 +08:00
|
|
|
set_imap_sync(topic_id) if !opts[:skip_imap_sync] && destroyed.present?
|
2016-02-07 20:39:07 +08:00
|
|
|
end
|
|
|
|
|
2020-07-10 17:05:55 +08:00
|
|
|
def self.archive!(group_id, topic, opts = {})
|
2018-03-06 14:38:43 +08:00
|
|
|
topic_id = topic.id
|
2020-07-10 17:05:55 +08:00
|
|
|
destroyed = GroupArchivedMessage.where(group_id: group_id, topic_id: topic_id).destroy_all
|
2016-02-07 20:39:07 +08:00
|
|
|
GroupArchivedMessage.create!(group_id: group_id, topic_id: topic_id)
|
2017-02-28 22:56:41 +08:00
|
|
|
trigger(:archive_message, group_id, topic_id)
|
2016-02-07 20:39:07 +08:00
|
|
|
MessageBus.publish("/topic/#{topic_id}", { type: "archived" }, group_ids: [group_id])
|
2018-03-07 14:01:20 +08:00
|
|
|
publish_topic_tracking_state(topic)
|
2020-07-10 17:05:55 +08:00
|
|
|
set_imap_sync(topic_id) if !opts[:skip_imap_sync] && destroyed.blank?
|
2016-02-07 20:39:07 +08:00
|
|
|
end
|
|
|
|
|
2017-02-28 22:56:41 +08:00
|
|
|
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
|
|
|
|
|
2018-03-06 14:38:43 +08:00
|
|
|
private
|
|
|
|
|
2018-03-07 14:01:20 +08:00
|
|
|
def self.publish_topic_tracking_state(topic)
|
2018-03-06 14:38:43 +08:00
|
|
|
TopicTrackingState.publish_private_message(
|
2018-03-07 14:01:20 +08:00
|
|
|
topic, group_archive: true
|
2018-03-06 14:38:43 +08:00
|
|
|
)
|
|
|
|
end
|
2020-07-10 17:05:55 +08:00
|
|
|
|
|
|
|
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
|
2015-12-23 08:09:17 +08:00
|
|
|
end
|
2016-01-11 14:30:56 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_archived_messages
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# group_id :integer not null
|
|
|
|
# topic_id :integer not null
|
2019-01-12 03:29:56 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2016-01-11 14:30:56 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_group_archived_messages_on_group_id_and_topic_id (group_id,topic_id) UNIQUE
|
|
|
|
#
|