Add single status fetching to TwAPI.
authorRoger Braun <roger@rogerbraun.net>
Fri, 24 Mar 2017 00:16:28 +0000 (01:16 +0100)
committerRoger Braun <roger@rogerbraun.net>
Fri, 24 Mar 2017 00:16:28 +0000 (01:16 +0100)
lib/pleroma/web/endpoint.ex
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
mix.exs
mix.lock
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs

index a79b0dca96c7067652a843a0eb0af5a91bb00373..e35a94c84976cb375a9dd6d9d43b4e9e0e9481e7 100644 (file)
@@ -17,6 +17,7 @@ defmodule Pleroma.Web.Endpoint do
     plug Phoenix.CodeReloader
   end
 
+  plug TrailingFormatPlug
   plug Plug.RequestId
   plug Plug.Logger
 
index e7d0b94377210d8955e96c211c5884077743d9f1..d9a3968a7c85cd42335a6b822f59b26b3bd46344 100644 (file)
@@ -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
index b982832480baea08c74e30af4bf2b6ad68e1f84d..f777ba848a40642e6f99259178d69281f8450bd2 100644 (file)
@@ -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
index 42485933beb40f105995a447d238829b774c2400..2a49b6270b2c9e6544a1a3e6e55205c480625c15 100644 (file)
@@ -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 ce5ef720ee3c1f4dd920688dc29cf043919007fa..397a54d9c31f8efc9e19f91ff2633b06eca233e3 100644 (file)
--- 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
 
index b7d93f284a87e7ce50c12ff72ab9eced0cad4a16..415ae72f719bc2a8e84c8a6d747adca22a8b41c3 100644 (file)
--- 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]}]}}
index 851d5f4a7570ea617abc8f0c5a01f08a92ca9326..4cc009326177180568d64b3218f59adc5e90d4d4 100644 (file)
@@ -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
index 507db5488c631564ac22883030d7423f07022296..2d289ffcb3b1acc2625ada672e3100d62b453924 100644 (file)
@@ -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"})