From f9828e578cf4c193fb35cbcc55133cd8e615755d Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 17:34:48 +0200 Subject: [PATCH] TwitterAPI: Add /users/show. --- lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 13 ++++++ .../twitter_api_controller_test.exs | 40 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8fe1d8ec6..71b4a3692 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -140,6 +140,7 @@ defmodule Pleroma.Web.Router do get "/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline get "/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline + get "/users/show", TwitterAPI.Controller, :show_user get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 125eb8a1e..5e50c3b18 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -65,6 +65,19 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, json) end + def show_user(conn, params) do + with {:ok, shown} <- TwitterAPI.get_user(params) do + if user = conn.assigns.user do + render conn, UserView, "show.json", %{user: shown, for: user} + else + render conn, UserView, "show.json", %{user: shown} + end + else + {:error, msg} -> + bad_request_reply(conn, msg) + end + end + def user_timeline(%{assigns: %{user: user}} = conn, params) do case TwitterAPI.get_user(user, params) do {:ok, target_user} -> diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 6bdf29a13..b886536d6 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -6,6 +6,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.TwitterAPI.UserView alias Pleroma.Web.CommonAPI + alias Pleroma.Web.TwitterAPI.TwitterAPI import Pleroma.Factory @@ -101,6 +102,45 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "GET /users/show.json" do + test "gets user with screen_name", %{conn: conn} do + user = insert(:user) + + conn = conn + |> get("/api/users/show.json", %{"screen_name" => user.nickname}) + + response = json_response(conn, 200) + + assert response["id"] == user.id + end + + test "gets user with user_id", %{conn: conn} do + user = insert(:user) + + conn = conn + |> get("/api/users/show.json", %{"user_id" => user.id}) + + response = json_response(conn, 200) + + assert response["id"] == user.id + end + + test "gets a user for a logged in user", %{conn: conn} do + user = insert(:user) + logged_in = insert(:user) + + {:ok, logged_in, user, _activity} = TwitterAPI.follow(logged_in, %{"user_id" => user.id}) + + conn = conn + |> with_credentials(logged_in.nickname, "test") + |> get("/api/users/show.json", %{"user_id" => user.id}) + + response = json_response(conn, 200) + + assert response["following"] == true + end + end + describe "GET /statusnet/conversation/:id.json" do test "returns the statuses in the conversation", %{conn: conn} do {:ok, _user} = UserBuilder.insert