Add public timeline TwAPI.

This commit is contained in:
Roger Braun 2017-03-21 21:09:20 +01:00
parent b9d0e34506
commit 2db28df4cf
6 changed files with 37 additions and 6 deletions

View File

@ -8,7 +8,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
def fetch_public_activities(opts \\ %{}) do def fetch_public_activities(opts \\ %{}) do
since_id = opts[:since_id] || 0 since_id = opts["since_id"] || 0
query = from activity in Activity, query = from activity in Activity,
where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data), where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data),

View File

@ -9,12 +9,19 @@ defmodule Pleroma.Web.Router do
pipeline :api do pipeline :api do
plug :accepts, ["json"] plug :accepts, ["json"]
plug :fetch_session
plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Pleroma.Web.Router.user_fetcher/1, optional: true}
end end
pipeline :authenticated_api do pipeline :authenticated_api do
plug :accepts, ["json"] plug :accepts, ["json"]
plug :fetch_session plug :fetch_session
plug Pleroma.Plugs.AuthenticationPlug, fetcher: &Pleroma.Web.Router.user_fetcher/1 plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Pleroma.Web.Router.user_fetcher/1}
end
scope "/api", Pleroma.Web do
pipe_through :api
get "/statuses/public_timeline.json", TwitterAPI.Controller, :public_timeline
end end
scope "/api", Pleroma.Web do scope "/api", Pleroma.Web do

View File

@ -21,8 +21,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
ActivityPub.insert(activity) ActivityPub.insert(activity)
end end
def fetch_public_statuses do def fetch_public_statuses(opts \\ %{}) do
activities = ActivityPub.fetch_public_activities activities = ActivityPub.fetch_public_activities(opts)
Enum.map(activities, fn(activity) -> Enum.map(activities, fn(activity) ->
actor = get_in(activity.data, ["actor"]) actor = get_in(activity.data, ["actor"])

View File

@ -16,6 +16,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user})) |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
end end
def public_timeline(conn, params) do
statuses = TwitterAPI.fetch_public_statuses(params)
{:ok, json} = Poison.encode(statuses)
conn
|> json_reply(200, json)
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

@ -41,7 +41,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
since_id = List.last(activities).id since_id = List.last(activities).id
last_expected = List.last(later_activities) last_expected = List.last(later_activities)
activities = ActivityPub.fetch_public_activities(%{since_id: since_id}) activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
last = List.last(activities) last = List.last(activities)
assert length(activities) == 10 assert length(activities) == 10

View File

@ -1,7 +1,7 @@
defmodule Pleroma.Web.TwitterAPI.ControllerTest do defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
alias Pleroma.{Repo, Activity} alias Pleroma.{Repo, Activity}
describe "POST /api/account/verify_credentials" do describe "POST /api/account/verify_credentials" do
@ -36,6 +36,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
end end
end end
describe "GET /statuses/public_timeline.json" do
test "returns statuses", %{conn: conn} do
{:ok, user} = UserBuilder.insert
activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
ActivityBuilder.insert_list(10, %{}, %{user: user})
since_id = List.last(activities).id
conn = conn
|> get("/api/statuses/public_timeline.json", %{since_id: since_id})
response = json_response(conn, 200)
assert length(response) == 10
end
end
defp valid_user(_context) do defp valid_user(_context) do
{ :ok, user } = UserBuilder.insert { :ok, user } = UserBuilder.insert
[user: user] [user: user]