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),
)
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,
)
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
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
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
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)
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)