From 36448d6483c7f53266052e72bd39b04558478410 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 10 May 2017 18:44:57 +0200 Subject: [PATCH] Add externalprofile to TwAPI. --- lib/pleroma/web/router.ex | 2 ++ lib/pleroma/web/twitter_api/twitter_api.ex | 8 ++++++++ lib/pleroma/web/twitter_api/twitter_api_controller.ex | 8 ++++++++ test/web/twitter_api/twitter_api_controller_test.exs | 11 +++++++++++ test/web/twitter_api/twitter_api_test.exs | 9 +++++++++ 5 files changed, 38 insertions(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 50a3934d6..15f66b24f 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -38,6 +38,8 @@ defmodule Pleroma.Web.Router do get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation post "/account/register", TwitterAPI.Controller, :register + + get "/externalprofile/show", TwitterAPI.Controller, :external_profile end scope "/api", Pleroma.Web do diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index d73ef3c77..d048d07c3 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -350,4 +350,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do {:error, "No such conversation"} end end + + def get_external_profile(for_user, uri) do + with %User{} = user <- User.get_cached_by_ap_id(uri) do + {:ok, UserRepresenter.to_map(user, %{for: for_user})} + else _e -> + {:error, "Couldn't find user"} + end + end end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 3fb60da6c..4a1622cc8 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -207,6 +207,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, response) end + def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do + with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri), + response <- Poison.encode!(user_map) do + conn + |> json_reply(200, response) + end + end + defp bad_request_reply(conn, error_message) do json = error_json(conn, error_message) json_reply(conn, 400, json) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index f8afbaee5..0e9d29018 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -389,6 +389,17 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "GET /api/externalprofile/show" do + test "it returns the user", %{conn: conn} do + user = insert(:user) + + conn = conn + |> get("/api/externalprofile/show", %{profileurl: user.ap_id}) + + assert json_response(conn, 200) == UserRepresenter.to_map(user) + end + end + defp valid_user(_context) do user = insert(:user) [user: user] diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index fd309e5ed..ebc9e362f 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -358,4 +358,13 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert conversation_id == object.id end end + + describe "fetching a user by uri" do + test "fetches a user by uri" do + user = insert(:user) + + {:ok, represented} = TwitterAPI.get_external_profile(user, user.ap_id) + assert represented = UserRepresenter.to_map(user, %{for: user}) + end + end end