diff --git a/app/models/topic_converter.rb b/app/models/topic_converter.rb index 5ebe6516250..aed196943a8 100644 --- a/app/models/topic_converter.rb +++ b/app/models/topic_converter.rb @@ -49,7 +49,7 @@ class TopicConverter @topic.category_id = nil @topic.archetype = Archetype.private_message add_allowed_users - @topic.save + @topic.save! watch_topic(topic) end @topic @@ -77,7 +77,7 @@ class TopicConverter user.user_stat.post_count -= 1 user.user_stat.save! end - @topic.topic_allowed_users.build(user_id: @user.id) + @topic.topic_allowed_users.build(user_id: @user.id) unless @topic.topic_allowed_users.where(user_id: @user.id).exists? # update topics count @topic.user.user_stat.topic_count -= 1 @topic.user.user_stat.save! diff --git a/spec/models/topic_converter_spec.rb b/spec/models/topic_converter_spec.rb index 05d28b10e62..66e2c94380d 100644 --- a/spec/models/topic_converter_spec.rb +++ b/spec/models/topic_converter_spec.rb @@ -131,5 +131,17 @@ describe TopicConverter do expect(topic.reload.user.user_stat.post_count).to eq(0) end end + + context 'when user already exists in topic_allowed_users table' do + before do + topic.topic_allowed_users.create!(user_id: admin.id) + end + + it "works" do + private_message = topic.convert_to_private_message(admin) + expect(private_message).to be_valid + expect(topic.archetype).to eq("private_message") + end + end end end