forked from AkkomaGang/akkoma
TwitterAPI: Add follower/following pagination.
This commit is contained in:
parent
1881680e28
commit
7ac152ed38
3 changed files with 83 additions and 8 deletions
|
@ -471,7 +471,7 @@ def get_or_fetch_by_nickname(nickname) do
|
|||
end
|
||||
end
|
||||
|
||||
def get_followers_query(%User{id: id, follower_address: follower_address}) do
|
||||
def get_followers_query(%User{id: id, follower_address: follower_address}, nil) do
|
||||
from(
|
||||
u in User,
|
||||
where: fragment("? <@ ?", ^[follower_address], u.following),
|
||||
|
@ -479,13 +479,23 @@ def get_followers_query(%User{id: id, follower_address: follower_address}) do
|
|||
)
|
||||
end
|
||||
|
||||
def get_followers(user) do
|
||||
q = get_followers_query(user)
|
||||
def get_followers_query(user, page) do
|
||||
from(
|
||||
u in get_followers_query(user, nil),
|
||||
limit: 20,
|
||||
offset: ^((page - 1) * 20)
|
||||
)
|
||||
end
|
||||
|
||||
def get_followers_query(user), do: get_followers_query(user, nil)
|
||||
|
||||
def get_followers(user, page \\ nil) do
|
||||
q = get_followers_query(user, page)
|
||||
|
||||
{:ok, Repo.all(q)}
|
||||
end
|
||||
|
||||
def get_friends_query(%User{id: id, following: following}) do
|
||||
def get_friends_query(%User{id: id, following: following}, nil) do
|
||||
from(
|
||||
u in User,
|
||||
where: u.follower_address in ^following,
|
||||
|
@ -493,8 +503,18 @@ def get_friends_query(%User{id: id, following: following}) do
|
|||
)
|
||||
end
|
||||
|
||||
def get_friends(user) do
|
||||
q = get_friends_query(user)
|
||||
def get_friends_query(user, page) do
|
||||
from(
|
||||
u in get_friends_query(user, nil),
|
||||
limit: 20,
|
||||
offset: ^((page - 1) * 20)
|
||||
)
|
||||
end
|
||||
|
||||
def get_friends_query(user), do: get_friends_query(user, nil)
|
||||
|
||||
def get_friends(user, page \\ nil) do
|
||||
q = get_friends_query(user, page)
|
||||
|
||||
{:ok, Repo.all(q)}
|
||||
end
|
||||
|
|
|
@ -472,8 +472,10 @@ def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" =>
|
|||
end
|
||||
|
||||
def followers(%{assigns: %{user: for_user}} = conn, params) do
|
||||
page = params["page"] || 1
|
||||
|
||||
with {:ok, user} <- TwitterAPI.get_user(for_user, params),
|
||||
{:ok, followers} <- User.get_followers(user) do
|
||||
{:ok, followers} <- User.get_followers(user, page) do
|
||||
followers =
|
||||
cond do
|
||||
for_user && user.id == for_user.id -> followers
|
||||
|
@ -490,8 +492,10 @@ def followers(%{assigns: %{user: for_user}} = conn, params) do
|
|||
end
|
||||
|
||||
def friends(%{assigns: %{user: for_user}} = conn, params) do
|
||||
page = params["page"] || 1
|
||||
|
||||
with {:ok, user} <- TwitterAPI.get_user(conn.assigns[:user], params),
|
||||
{:ok, friends} <- User.get_friends(user) do
|
||||
{:ok, friends} <- User.get_friends(user, page) do
|
||||
friends =
|
||||
cond do
|
||||
for_user && user.id == for_user.id -> friends
|
||||
|
|
|
@ -1082,6 +1082,31 @@ test "it returns a user's followers", %{conn: conn} do
|
|||
assert Enum.sort(expected) == Enum.sort(result)
|
||||
end
|
||||
|
||||
test "it returns 20 followers per page", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
followers = insert_list(21, :user)
|
||||
|
||||
Enum.each(followers, fn follower ->
|
||||
User.follow(follower, user)
|
||||
end)
|
||||
|
||||
res_conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> get("/api/statuses/followers")
|
||||
|
||||
result = json_response(res_conn, 200)
|
||||
assert length(result) == 20
|
||||
|
||||
res_conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> get("/api/statuses/followers", %{page: 2})
|
||||
|
||||
result = json_response(res_conn, 200)
|
||||
assert length(result) == 1
|
||||
end
|
||||
|
||||
test "it returns a given user's followers with user_id", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
follower_one = insert(:user)
|
||||
|
@ -1183,6 +1208,32 @@ test "it returns the logged in user's friends", %{conn: conn} do
|
|||
assert Enum.sort(expected) == Enum.sort(result)
|
||||
end
|
||||
|
||||
test "it returns 20 friends per page", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
followeds = insert_list(21, :user)
|
||||
|
||||
{:ok, user} =
|
||||
Enum.reduce(followeds, {:ok, user}, fn followed, {:ok, user} ->
|
||||
User.follow(user, followed)
|
||||
end)
|
||||
|
||||
res_conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> get("/api/statuses/friends")
|
||||
|
||||
result = json_response(res_conn, 200)
|
||||
assert length(result) == 20
|
||||
|
||||
res_conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> get("/api/statuses/friends", %{page: 2})
|
||||
|
||||
result = json_response(res_conn, 200)
|
||||
assert length(result) == 1
|
||||
end
|
||||
|
||||
test "it returns a given user's friends with user_id", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
followed_one = insert(:user)
|
||||
|
|
Loading…
Reference in a new issue