Add single status fetching to TwAPI.

This commit is contained in:
Roger Braun 2017-03-24 01:16:28 +01:00
parent 866a2663d4
commit e4de0ddc77
8 changed files with 58 additions and 12 deletions

View File

@ -17,6 +17,7 @@ defmodule Pleroma.Web.Endpoint do
plug Phoenix.CodeReloader plug Phoenix.CodeReloader
end end
plug TrailingFormatPlug
plug Plug.RequestId plug Plug.RequestId
plug Plug.Logger plug Plug.Logger

View File

@ -21,17 +21,18 @@ defmodule Pleroma.Web.Router do
scope "/api", Pleroma.Web do scope "/api", Pleroma.Web do
pipe_through :api pipe_through :api
get "/statuses/public_timeline.json", TwitterAPI.Controller, :public_timeline get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/public_and_external_timeline.json", TwitterAPI.Controller, :public_timeline get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
end end
scope "/api", Pleroma.Web do scope "/api", Pleroma.Web do
pipe_through :authenticated_api pipe_through :authenticated_api
post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
post "/statuses/update.json", TwitterAPI.Controller, :status_update post "/statuses/update", TwitterAPI.Controller, :status_update
get "/statuses/friends_timeline.json", TwitterAPI.Controller, :friends_timeline get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline
post "/friendships/create.json", TwitterAPI.Controller, :follow post "/friendships/create", TwitterAPI.Controller, :follow
post "/friendships/destroy.json", TwitterAPI.Controller, :unfollow post "/friendships/destroy", TwitterAPI.Controller, :unfollow
end end
end end

View File

@ -69,9 +69,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
defp activities_to_statuses(activities, opts) do defp activities_to_statuses(activities, opts) do
Enum.map(activities, fn(activity) -> Enum.map(activities, fn(activity) ->
activity_to_status(activity, opts)
end)
end
defp activity_to_status(activity, opts) do
actor = get_in(activity.data, ["actor"]) actor = get_in(activity.data, ["actor"])
user = Repo.get_by!(User, ap_id: actor) user = Repo.get_by!(User, ap_id: actor)
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user})) ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
end) end
def fetch_status(user, id) do
with %Activity{} = activity <- Repo.get(Activity, id) do
activity_to_status(activity, %{for: user})
end
end end
end end

View File

@ -50,6 +50,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|> json_reply(200, response) |> json_reply(200, response)
end 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 defp json_reply(conn, status, json) do
conn conn
|> put_resp_content_type("application/json") |> put_resp_content_type("application/json")

View File

@ -33,8 +33,9 @@ defmodule Pleroma.Mixfile do
{:phoenix_ecto, "~> 3.2"}, {:phoenix_ecto, "~> 3.2"},
{:postgrex, ">= 0.0.0"}, {:postgrex, ">= 0.0.0"},
{:gettext, "~> 0.11"}, {:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"}, {:cowboy, "~> 1.0", override: true},
{:comeonin, "~> 3.0"}, {:comeonin, "~> 3.0"},
{:trailing_format_plug, "~> 0.0.5" },
{:mix_test_watch, "~> 0.2", only: :dev}] {:mix_test_watch, "~> 0.2", only: :dev}]
end end

View File

@ -17,4 +17,5 @@
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}, "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]}]}, "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]}]}}

View File

@ -52,6 +52,21 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
end end
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 describe "GET /statuses/friends_timeline.json" do
setup [:valid_user] setup [:valid_user]
test "without valid credentials", %{conn: conn} do test "without valid credentials", %{conn: conn} do

View File

@ -69,6 +69,16 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user}) assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
end 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 test "Follow another user" do
{ :ok, user } = UserBuilder.insert { :ok, user } = UserBuilder.insert
{ :ok, following } = UserBuilder.insert(%{nickname: "guy"}) { :ok, following } = UserBuilder.insert(%{nickname: "guy"})