From acf337d58300a7cca189fa49059902a7aa37ef55 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 27 Feb 2020 11:09:37 +0530 Subject: [PATCH] FEATURE: auto archive group message if topic is closed. (#9046) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: RĂ©gis Hanol --- app/models/topic.rb | 11 +++++++++++ spec/models/topic_spec.rb | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/app/models/topic.rb b/app/models/topic.rb index 44d02260aea..ff4b7857087 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -502,6 +502,17 @@ class Topic < ActiveRecord::Base def update_status(status, enabled, user, opts = {}) TopicStatusUpdater.new(self, user).update!(status, enabled, opts) DiscourseEvent.trigger(:topic_status_updated, self, status, enabled) + + if enabled && private_message? && status.to_s["closed"] + group_ids = user.groups.pluck(:id) + if group_ids.present? + allowed_group_ids = self.allowed_groups + .where('topic_allowed_groups.group_id IN (?)', group_ids).pluck(:id) + allowed_group_ids.each do |id| + GroupArchivedMessage.archive!(id, self) + end + end + end end # Atomically creates the next post number diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 73bfdc330b5..ec2956d6608 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1114,6 +1114,14 @@ describe Topic do context 'closed' do let(:status) { 'closed' } it_should_behave_like 'a status that closes a topic' + + it 'should archive group message' do + group = Fabricate(:group) + group.add(@user) + topic = Fabricate(:private_message_topic, allowed_groups: [group]) + + expect { topic.update_status(status, true, @user) }.to change(topic.group_archived_messages, :count).by(1) + end end context 'autoclosed' do