1
0
Fork 1
mirror of https://akkoma.dev/AkkomaGang/akkoma.git synced 2024-11-19 11:29:17 +00:00

User.follow_all: ensure its stays unique

This commit is contained in:
href 2019-01-31 18:07:46 +01:00
parent 44913c1019
commit 308b35ebe2
No known key found for this signature in database
GPG key ID: EE8296C1A152C325
2 changed files with 23 additions and 1 deletions

View file

@ -315,7 +315,16 @@ def follow_all(follower, followeds) 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)

View file

@ -65,6 +65,19 @@ test "follow_all follows mutliple users" 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)