Commit 7ac152ed authored by lain's avatar lain
Browse files

TwitterAPI: Add follower/following pagination.

parent 1881680e
...@@ -471,7 +471,7 @@ def get_or_fetch_by_nickname(nickname) do ...@@ -471,7 +471,7 @@ def get_or_fetch_by_nickname(nickname) do
end end
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( from(
u in User, u in User,
where: fragment("? <@ ?", ^[follower_address], u.following), where: fragment("? <@ ?", ^[follower_address], u.following),
...@@ -479,13 +479,23 @@ def get_followers_query(%User{id: id, follower_address: follower_address}) do ...@@ -479,13 +479,23 @@ def get_followers_query(%User{id: id, follower_address: follower_address}) do
) )
end end
def get_followers(user) do def get_followers_query(user, page) do
q = get_followers_query(user) 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)} {:ok, Repo.all(q)}
end end
def get_friends_query(%User{id: id, following: following}) do def get_friends_query(%User{id: id, following: following}, nil) do
from( from(
u in User, u in User,
where: u.follower_address in ^following, where: u.follower_address in ^following,
...@@ -493,8 +503,18 @@ def get_friends_query(%User{id: id, following: following}) do ...@@ -493,8 +503,18 @@ def get_friends_query(%User{id: id, following: following}) do
) )
end end
def get_friends(user) do def get_friends_query(user, page) do
q = get_friends_query(user) 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)} {:ok, Repo.all(q)}
end end
......
...@@ -472,8 +472,10 @@ def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => ...@@ -472,8 +472,10 @@ def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" =>
end end
def followers(%{assigns: %{user: for_user}} = conn, params) do def followers(%{assigns: %{user: for_user}} = conn, params) do
page = params["page"] || 1
with {:ok, user} <- TwitterAPI.get_user(for_user, params), 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 = followers =
cond do cond do
for_user && user.id == for_user.id -> followers for_user && user.id == for_user.id -> followers
...@@ -490,8 +492,10 @@ def followers(%{assigns: %{user: for_user}} = conn, params) do ...@@ -490,8 +492,10 @@ def followers(%{assigns: %{user: for_user}} = conn, params) do
end end
def friends(%{assigns: %{user: for_user}} = conn, params) do def friends(%{assigns: %{user: for_user}} = conn, params) do
page = params["page"] || 1
with {:ok, user} <- TwitterAPI.get_user(conn.assigns[:user], params), 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 = friends =
cond do cond do
for_user && user.id == for_user.id -> friends for_user && user.id == for_user.id -> friends
......
...@@ -1082,6 +1082,31 @@ test "it returns a user's followers", %{conn: conn} do ...@@ -1082,6 +1082,31 @@ test "it returns a user's followers", %{conn: conn} do
assert Enum.sort(expected) == Enum.sort(result) assert Enum.sort(expected) == Enum.sort(result)
end 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 test "it returns a given user's followers with user_id", %{conn: conn} do
user = insert(:user) user = insert(:user)
follower_one = insert(:user) follower_one = insert(:user)
...@@ -1183,6 +1208,32 @@ test "it returns the logged in user's friends", %{conn: conn} do ...@@ -1183,6 +1208,32 @@ test "it returns the logged in user's friends", %{conn: conn} do
assert Enum.sort(expected) == Enum.sort(result) assert Enum.sort(expected) == Enum.sort(result)
end 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 test "it returns a given user's friends with user_id", %{conn: conn} do
user = insert(:user) user = insert(:user)
followed_one = insert(:user) followed_one = insert(:user)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment