From 1969773fb4a8a6b2cf4354cd4e879bfe6c08dca6 Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 18 Dec 2017 16:23:04 +0200 Subject: [PATCH] Fix follower/following lists. Add tests. --- lib/pleroma/web/router.ex | 4 +-- .../web/twitter_api/twitter_api_controller.ex | 10 +++--- .../twitter_api_controller_test.exs | 32 ++++++++++++++++++- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 6806e8a75..8d8566490 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -142,6 +142,8 @@ defmodule Pleroma.Web.Router do get "/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline get "/users/show", TwitterAPI.Controller, :show_user + get "/statuses/followers", TwitterAPI.Controller, :followers + get "/statuses/friends", TwitterAPI.Controller, :friends get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation @@ -188,8 +190,6 @@ defmodule Pleroma.Web.Router do post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar - get "/statuses/followers", TwitterAPI.Controller, :followers - get "/statuses/friends", TwitterAPI.Controller, :friends get "/friends/ids", TwitterAPI.Controller, :friends_ids get "/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 73d96c73d..5284a8847 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -263,16 +263,18 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end - def followers(%{assigns: %{user: user}} = conn, _params) do - with {:ok, followers} <- User.get_followers(user) do + def followers(conn, params) do + with {:ok, user} <- TwitterAPI.get_user(conn.assigns.user, params), + {:ok, followers} <- User.get_followers(user) do render(conn, UserView, "index.json", %{users: followers, for: user}) else _e -> bad_request_reply(conn, "Can't get followers") end end - def friends(%{assigns: %{user: user}} = conn, _params) do - with {:ok, friends} <- User.get_friends(user) do + def friends(conn, params) do + with {:ok, user} <- TwitterAPI.get_user(conn.assigns.user, params), + {: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") diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 90d0fa654..39f1cdd4c 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -518,7 +518,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end describe "GET /api/statuses/friends" do - test "it returns a user's friends", %{conn: conn} do + test "it returns the logged in user's friends", %{conn: conn} do user = insert(:user) followed_one = insert(:user) followed_two = insert(:user) @@ -533,6 +533,36 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert MapSet.equal?(MapSet.new(json_response(conn, 200)), MapSet.new(UserView.render("index.json", %{users: [followed_one, followed_two], for: user}))) end + + test "it returns a given user's friends with user_id", %{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 + |> get("/api/statuses/friends", %{"user_id" => user.id}) + + assert MapSet.equal?(MapSet.new(json_response(conn, 200)), MapSet.new(UserView.render("index.json", %{users: [followed_one, followed_two], for: user}))) + end + + test "it returns a given user's friends with screen_name", %{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 + |> get("/api/statuses/friends", %{"screen_name" => user.nickname}) + + assert MapSet.equal?(MapSet.new(json_response(conn, 200)), MapSet.new(UserView.render("index.json", %{users: [followed_one, followed_two], for: user}))) + end end describe "GET /friends/ids" do