From 308b35ebe2f2062b87a5d7703df8ab5b1210d9c8 Mon Sep 17 00:00:00 2001 From: href Date: Thu, 31 Jan 2019 18:07:46 +0100 Subject: [PATCH] User.follow_all: ensure its stays unique --- lib/pleroma/user.ex | 11 ++++++++++- test/user_test.exs | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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)