From e4de0ddc77bceb0bbaad6fd25b327c3af53f198c Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 24 Mar 2017 01:16:28 +0100 Subject: [PATCH] Add single status fetching to TwAPI. --- lib/pleroma/web/endpoint.ex | 1 + lib/pleroma/web/router.ex | 15 ++++++++------- lib/pleroma/web/twitter_api/twitter_api.ex | 16 +++++++++++++--- .../web/twitter_api/twitter_api_controller.ex | 7 +++++++ mix.exs | 3 ++- mix.lock | 3 ++- .../twitter_api/twitter_api_controller_test.exs | 15 +++++++++++++++ test/web/twitter_api/twitter_api_test.exs | 10 ++++++++++ 8 files changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index a79b0dca9..e35a94c84 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -17,6 +17,7 @@ defmodule Pleroma.Web.Endpoint do plug Phoenix.CodeReloader end + plug TrailingFormatPlug plug Plug.RequestId plug Plug.Logger diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index e7d0b9437..d9a3968a7 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -21,17 +21,18 @@ defmodule Pleroma.Web.Router do scope "/api", Pleroma.Web do pipe_through :api - get "/statuses/public_timeline.json", TwitterAPI.Controller, :public_timeline - get "/statuses/public_and_external_timeline.json", TwitterAPI.Controller, :public_timeline + get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline + get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline + get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status end scope "/api", Pleroma.Web do pipe_through :authenticated_api - post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials - post "/statuses/update.json", TwitterAPI.Controller, :status_update - get "/statuses/friends_timeline.json", TwitterAPI.Controller, :friends_timeline - post "/friendships/create.json", TwitterAPI.Controller, :follow - post "/friendships/destroy.json", TwitterAPI.Controller, :unfollow + post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials + post "/statuses/update", TwitterAPI.Controller, :status_update + get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline + post "/friendships/create", TwitterAPI.Controller, :follow + post "/friendships/destroy", TwitterAPI.Controller, :unfollow end end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index b98283248..f777ba848 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -69,9 +69,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do defp activities_to_statuses(activities, opts) do Enum.map(activities, fn(activity) -> - actor = get_in(activity.data, ["actor"]) - user = Repo.get_by!(User, ap_id: actor) - ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user})) + activity_to_status(activity, opts) end) end + + defp activity_to_status(activity, opts) do + actor = get_in(activity.data, ["actor"]) + user = Repo.get_by!(User, ap_id: actor) + ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user})) + end + + def fetch_status(user, id) do + with %Activity{} = activity <- Repo.get(Activity, id) do + activity_to_status(activity, %{for: 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 42485933b..2a49b6270 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -50,6 +50,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, response) end + def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do + response = TwitterAPI.fetch_status(user, id) |> Poison.encode! + + conn + |> json_reply(200, response) + end + defp json_reply(conn, status, json) do conn |> put_resp_content_type("application/json") diff --git a/mix.exs b/mix.exs index ce5ef720e..397a54d9c 100644 --- a/mix.exs +++ b/mix.exs @@ -33,8 +33,9 @@ defmodule Pleroma.Mixfile do {:phoenix_ecto, "~> 3.2"}, {:postgrex, ">= 0.0.0"}, {:gettext, "~> 0.11"}, - {:cowboy, "~> 1.0"}, + {:cowboy, "~> 1.0", override: true}, {:comeonin, "~> 3.0"}, + {:trailing_format_plug, "~> 0.0.5" }, {:mix_test_watch, "~> 0.2", only: :dev}] end diff --git a/mix.lock b/mix.lock index b7d93f284..415ae72f7 100644 --- a/mix.lock +++ b/mix.lock @@ -17,4 +17,5 @@ "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []}, "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}, "postgrex": {:hex, :postgrex, "0.13.2", "2b88168fc6a5456a27bfb54ccf0ba4025d274841a7a3af5e5deb1b755d95154e", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, - "ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], []}} + "ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], []}, + "trailing_format_plug": {:hex, :trailing_format_plug, "0.0.6", "777fd71bbc30e54cb36133bacd38fac88d44be410bc60353fe48e91e14c0b990", [:mix], [{:cowboy, "~> 1.0.0", [hex: :cowboy, optional: false]}, {:plug, "> 0.12.0", [hex: :plug, optional: false]}]}} diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 851d5f4a7..4cc009326 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -52,6 +52,21 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "GET /statuses/show/:id.json" do + test "returns one status", %{conn: conn} do + {:ok, user} = UserBuilder.insert + {:ok, activity} = ActivityBuilder.insert(%{}, %{user: user}) + actor = Repo.get_by!(User, ap_id: activity.data["actor"]) + + conn = conn + |> get("/api/statuses/show/#{activity.id}.json") + + response = json_response(conn, 200) + + assert reponse = ActivityRepresenter.to_map(activity, %{user: actor}) + end + end + describe "GET /statuses/friends_timeline.json" do setup [:valid_user] test "without valid credentials", %{conn: conn} do diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 507db5488..2d289ffcb 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -69,6 +69,16 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user}) end + test "fetch a single status" do + {:ok, activity} = ActivityBuilder.insert() + {:ok, user} = UserBuilder.insert() + actor = Repo.get_by!(User, ap_id: activity.data["actor"]) + + status = TwitterAPI.fetch_status(user, activity.id) + + assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor}) + end + test "Follow another user" do { :ok, user } = UserBuilder.insert { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) -- 2.45.2