Add friends endpoint.

This commit is contained in:
Roger Braun 2017-07-20 20:35:30 +02:00
parent 94a394e3a8
commit a027b0027c
3 changed files with 27 additions and 0 deletions

View file

@ -72,6 +72,7 @@ def user_fetcher(username) do
post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar
get "/statuses/followers", TwitterAPI.Controller, :followers get "/statuses/followers", TwitterAPI.Controller, :followers
get "/statuses/friends", TwitterAPI.Controller, :friends
end end
pipeline :ostatus do pipeline :ostatus do

View file

@ -218,6 +218,14 @@ def followers(%{assigns: %{user: user}} = conn, _params) do
end end
end end
def friends(%{assigns: %{user: user}} = conn, _params) do
with {:ok, friends} <- User.get_friends(user) do
render(conn, UserView, "index.json", %{users: friends, for: user})
else
_e -> bad_request_reply(conn, "Can't get friends")
end
end
defp bad_request_reply(conn, error_message) do defp bad_request_reply(conn, error_message) do
json = error_json(conn, error_message) json = error_json(conn, error_message)
json_reply(conn, 400, json) json_reply(conn, 400, json)

View file

@ -437,6 +437,24 @@ test "it returns a user's followers", %{conn: conn} do
end end
end end
describe "GET /api/statuses/friends" do
test "it returns a user's friends", %{conn: conn} 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)
conn = conn
|> assign(:user, user)
|> get("/api/statuses/friends")
assert json_response(conn, 200) == UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
end
end
defp valid_user(_context) do defp valid_user(_context) do
user = insert(:user) user = insert(:user)
[user: user] [user: user]