Add follower / friend fetching to user.
This commit is contained in:
parent
140f72725d
commit
44aef2183e
@ -186,4 +186,21 @@ defmodule Pleroma.User do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: these queries could be more efficient if the type in postgresql wasn't map, but array.
|
||||||
|
def get_followers(%User{id: id, follower_address: follower_address}) do
|
||||||
|
q = from u in User,
|
||||||
|
where: fragment("? @> ?", u.following, ^follower_address ),
|
||||||
|
where: u.id != ^id
|
||||||
|
|
||||||
|
{:ok, Repo.all(q)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_friends(%User{id: id, following: following}) do
|
||||||
|
q = from u in User,
|
||||||
|
where: u.follower_address in ^following,
|
||||||
|
where: u.id != ^id
|
||||||
|
|
||||||
|
{:ok, Repo.all(q)}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -197,5 +197,35 @@ defmodule Pleroma.UserTest do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "followers and friends" do
|
||||||
|
test "gets all followers for a given user" do
|
||||||
|
user = insert(:user)
|
||||||
|
follower_one = insert(:user)
|
||||||
|
follower_two = insert(:user)
|
||||||
|
not_follower = insert(:user)
|
||||||
|
|
||||||
|
{:ok, follower_one} = User.follow(follower_one, user)
|
||||||
|
{:ok, follower_two} = User.follow(follower_two, user)
|
||||||
|
|
||||||
|
{:ok, res} = User.get_followers(user)
|
||||||
|
|
||||||
|
assert res == [follower_one, follower_two]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "gets all friends (followed users) for a given user" do
|
||||||
|
user = insert(:user)
|
||||||
|
followed_one = insert(:user)
|
||||||
|
followed_two = insert(:user)
|
||||||
|
not_followed = insert(:user)
|
||||||
|
|
||||||
|
{:ok, user} = User.follow(user, followed_one)
|
||||||
|
{:ok, user} = User.follow(user, followed_two)
|
||||||
|
|
||||||
|
{:ok, res} = User.get_friends(user)
|
||||||
|
|
||||||
|
assert res == [followed_one, followed_two]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user