diff --git a/app/jobs/regular/publish_topic_to_category.rb b/app/jobs/regular/publish_topic_to_category.rb index 31a838a2c85..c67adde491b 100644 --- a/app/jobs/regular/publish_topic_to_category.rb +++ b/app/jobs/regular/publish_topic_to_category.rb @@ -8,7 +8,13 @@ module Jobs return if topic.blank? PostTimestampChanger.new(timestamp: Time.zone.now, topic: topic).change! do - topic.change_category_to_id(topic_status_update.category_id) + if topic.private_message? + topic = TopicConverter.new(topic, Discourse.system_user) + .convert_to_public_topic(topic_status_update.category_id) + else + topic.change_category_to_id(topic_status_update.category_id) + end + topic.update_columns(visible: true) topic_status_update.trash!(Discourse.system_user) end diff --git a/app/models/topic_converter.rb b/app/models/topic_converter.rb index ddbda3a6903..dd5bc30559c 100644 --- a/app/models/topic_converter.rb +++ b/app/models/topic_converter.rb @@ -7,9 +7,19 @@ class TopicConverter @user = user end - def convert_to_public_topic + def convert_to_public_topic(category_id = nil) Topic.transaction do - @topic.category_id = SiteSetting.allow_uncategorized_topics ? SiteSetting.uncategorized_category_id : Category.where(read_restricted: false).first.id + @topic.category_id = + if category_id + category_id + elsif SiteSetting.allow_uncategorized_topics + SiteSetting.uncategorized_category_id + else + Category.where(read_restricted: false) + .where.not(id: SiteSetting.uncategorized_category_id) + .first.id + end + @topic.archetype = Archetype.default @topic.save update_user_stats diff --git a/spec/jobs/publish_topic_to_category_spec.rb b/spec/jobs/publish_topic_to_category_spec.rb index e49a3190979..976735cc2de 100644 --- a/spec/jobs/publish_topic_to_category_spec.rb +++ b/spec/jobs/publish_topic_to_category_spec.rb @@ -37,7 +37,7 @@ RSpec.describe Jobs::PublishTopicToCategory do end end - it 'should publish the topic to the new category correctly' do + it 'should publish the topic to the new category' do Timecop.travel(1.hour.ago) { topic.update!(visible: false) } message = MessageBus.track_publish do @@ -56,4 +56,32 @@ RSpec.describe Jobs::PublishTopicToCategory do expect(message.data[:reload_topic]).to be_present expect(message.data[:refresh_stream]).to be_present end + + describe 'when topic is a private message' do + before do + expect { topic.convert_to_private_message(Discourse.system_user) } + .to change { topic.private_message? }.to(true) + end + + + it 'should publish the topic to the new category' do + described_class.new.execute(topic_status_update_id: topic.topic_status_update.id) + + message = MessageBus.track_publish do + described_class.new.execute(topic_status_update_id: topic.topic_status_update.id) + end.first + + topic.reload + expect(topic.category).to eq(another_category) + expect(topic.visible).to eq(true) + expect(topic.private_message?).to eq(false) + + %w{created_at bumped_at updated_at last_posted_at}.each do |attribute| + expect(topic.public_send(attribute)).to be_within(1.second).of(Time.zone.now) + end + + expect(message.data[:reload_topic]).to be_present + expect(message.data[:refresh_stream]).to be_present + end + end end diff --git a/spec/models/topic_converter_spec.rb b/spec/models/topic_converter_spec.rb index 96bd61e9153..cee779633be 100644 --- a/spec/models/topic_converter_spec.rb +++ b/spec/models/topic_converter_spec.rb @@ -5,13 +5,42 @@ describe TopicConverter do context 'convert_to_public_topic' do let(:admin) { Fabricate(:admin) } let(:author) { Fabricate(:user) } + let(:category) { Fabricate(:category) } let(:private_message) { Fabricate(:private_message_topic, user: author) } context 'success' do it "converts private message to regular topic" do - topic = private_message.convert_to_public_topic(admin) + SiteSetting.allow_uncategorized_topics = true + topic = described_class.new(private_message, admin).convert_to_public_topic + topic.reload + expect(topic).to be_valid expect(topic.archetype).to eq("regular") + expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id) + end + + describe 'when uncategorized category is not allowed' do + before do + SiteSetting.allow_uncategorized_topics = false + category.update!(read_restricted: false) + end + + it 'should convert private message into the right category' do + topic = described_class.new(private_message, admin).convert_to_public_topic + topic.reload + + expect(topic).to be_valid + expect(topic.archetype).to eq("regular") + expect(topic.category_id).to eq(category.id) + end + end + + describe 'when a custom category_id is given' do + it 'should convert private message into the right category' do + topic = described_class.new(private_message, admin).convert_to_public_topic(category.id) + + expect(topic.reload.category).to eq(category) + end end it "updates user stats" do