User.follow_all: ensure its stays unique
authorhref <href@random.sh>
Thu, 31 Jan 2019 17:07:46 +0000 (18:07 +0100)
committerhref <href@random.sh>
Thu, 31 Jan 2019 17:17:44 +0000 (18:17 +0100)
lib/pleroma/user.ex
test/user_test.exs

index 60d1d4811720c76c4151c71be00c30548c24bf42..33630ac7c98fe439105def36f08528ccf471c211 100644 (file)
@@ -315,7 +315,16 @@ defmodule Pleroma.User do
     q =
       from(u in User,
         where: u.id == ^follower.id,
-        update: [set: [following: fragment("array_cat(?, ?)", u.following, ^followed_addresses)]]
+        update: [
+          set: [
+            following:
+              fragment(
+                "array(select distinct unnest (array_cat(?, ?)))",
+                u.following,
+                ^followed_addresses
+              )
+          ]
+        ]
       )
 
     {1, [follower]} = Repo.update_all(q, [], returning: true)
index 9815c4d5a91f2c537c323b92a5227086a5cc94d4..98d3bc4641a89dde961f50a220f4c43aabd5b047 100644 (file)
@@ -65,6 +65,19 @@ defmodule Pleroma.UserTest do
     refute User.following?(user, not_followed)
   end
 
+  test "follow_all follows mutliple users without duplicating" do
+    user = insert(:user)
+    followed_zero = insert(:user)
+    followed_one = insert(:user)
+    followed_two = insert(:user)
+
+    {:ok, user} = User.follow_all(user, [followed_zero, followed_one])
+    assert length(user.following) == 3
+
+    {:ok, user} = User.follow_all(user, [followed_one, followed_two])
+    assert length(user.following) == 4
+  end
+
   test "follow takes a user and another user" do
     user = insert(:user)
     followed = insert(:user)