Add externalprofile to TwAPI.

This commit is contained in:
Roger Braun 2017-05-10 18:44:57 +02:00
parent 2e753e8cd7
commit 36448d6483
5 changed files with 38 additions and 0 deletions

View File

@ -38,6 +38,8 @@ def user_fetcher(username) do
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
post "/account/register", TwitterAPI.Controller, :register post "/account/register", TwitterAPI.Controller, :register
get "/externalprofile/show", TwitterAPI.Controller, :external_profile
end end
scope "/api", Pleroma.Web do scope "/api", Pleroma.Web do

View File

@ -350,4 +350,12 @@ def conversation_id_to_context(id) do
{:error, "No such conversation"} {:error, "No such conversation"}
end end
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 end

View File

@ -207,6 +207,14 @@ def update_avatar(%{assigns: %{user: user}} = conn, params) do
|> json_reply(200, response) |> json_reply(200, response)
end 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 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

@ -389,6 +389,17 @@ test "it returns errors on a problem", %{conn: conn} do
end end
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 defp valid_user(_context) do
user = insert(:user) user = insert(:user)
[user: user] [user: user]

View File

@ -358,4 +358,13 @@ test "returns an existing mapping for an existing object" do
assert conversation_id == object.id assert conversation_id == object.id
end end
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 end