From: href Date: Thu, 31 Jan 2019 17:07:46 +0000 (+0100) Subject: User.follow_all: ensure its stays unique X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=308b35ebe2f2062b87a5d7703df8ab5b1210d9c8;p=akkoma User.follow_all: ensure its stays unique --- diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 60d1d4811..33630ac7c 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -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) diff --git a/test/user_test.exs b/test/user_test.exs index 9815c4d5a..98d3bc464 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -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)