FIX: Prevent from creation of duplicated TopicAllowedUsers (#8169)

FIX: Prevent from creation of duplicated TopicAllowedUsers (#8169)

Ensure that we don’t try to create duplicated TopicAllowedUsers

Related to Error message "Topic allowed users is invalid" - bug - Discourse Meta

Spec amended to cover a case creating an overlap.

diff --git a/app/models/topic_converter.rb b/app/models/topic_converter.rb
index f59e96c..aa23da1 100644
--- a/app/models/topic_converter.rb
+++ b/app/models/topic_converter.rb
@@ -81,6 +81,7 @@ class TopicConverter
       user.user_stat.save!
     end
     @topic.topic_allowed_users.build(user_id: @user.id) unless @topic.topic_allowed_users.where(user_id: @user.id).exists?
+    @topic.topic_allowed_users = @topic.topic_allowed_users.uniq(&:user_id)
     # 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 fb4b351..82b2664 100644
--- a/spec/models/topic_converter_spec.rb
+++ b/spec/models/topic_converter_spec.rb
@@ -114,7 +114,7 @@ describe TopicConverter do
 
     context 'success' do
       it "converts regular topic to private message" do
-        private_message = topic.convert_to_private_message(admin)
+        private_message = topic.convert_to_private_message(post.user)
         expect(private_message).to be_valid
         expect(topic.archetype).to eq("private_message")
         expect(topic.category_id).to eq(nil)

GitHub sha: 694a5bf2

This commit has been mentioned on Discourse Meta. There might be relevant details there: