FIX: don't double add users to topic allowed users

FIX: don't double add users to topic allowed users

In latest rails building the same relation twice results in an error
Previous versions would deduplicate

From e17d0c0526936ce66c02aa93fe90bd8100fe9b39 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Mon, 10 Dec 2018 14:45:02 +1100
Subject: [PATCH] FIX: don't double add users to topic allowed users

In latest rails building the same relation twice results in an error
Previous versions would deduplicate

diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index a62d42b..840a0bd 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -190,7 +190,11 @@ class TopicCreator
     add_users(topic, @opts[:target_usernames])
     add_emails(topic, @opts[:target_emails])
     add_groups(topic, @opts[:target_group_names])
-    topic.topic_allowed_users.build(user_id: @user.id)
+
+    if !@added_users.include?(user)
+      topic.topic_allowed_users.build(user_id: @user.id)
+    end
+
   end
 
   def save_topic(topic)
@@ -228,8 +232,10 @@ class TopicCreator
         display_name = email.split("@").first
 
         if user = find_or_create_user(email, display_name)
-          @added_users << user
-          topic.topic_allowed_users.build(user_id: user.id)
+          if !@added_users.include?(user)
+            @added_users << user
+            topic.topic_allowed_users.build(user_id: user.id)
+          end
           len += 1
         end
       end

GitHub

2 Likes

Is that the only spot where we do that? :worried: