Add public timeline TwAPI.
authorRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 20:09:20 +0000 (21:09 +0100)
committerRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 20:09:20 +0000 (21:09 +0100)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/activity_pub/activity_pub_test.exs
test/web/twitter_api/twitter_api_controller_test.exs

index 94e52a0fb0bd6e758fac77b8d3a629efc8ce05c1..5e4f3bbf5b5e0cba942ffe50eabe12cce2dcd6f4 100644 (file)
@@ -8,7 +8,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
 
   def fetch_public_activities(opts \\ %{}) do
-    since_id = opts[:since_id] || 0
+    since_id = opts["since_id"] || 0
 
     query = from activity in Activity,
       where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data),
index 1e865b358b819af9a2c3725e206d51dcff7a660a..0440af8c139a21fece628750fc72936bc8967ab8 100644 (file)
@@ -9,12 +9,19 @@ defmodule Pleroma.Web.Router do
 
   pipeline :api do
     plug :accepts, ["json"]
+    plug :fetch_session
+    plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Pleroma.Web.Router.user_fetcher/1, optional: true}
   end
 
   pipeline :authenticated_api do
     plug :accepts, ["json"]
     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
 
   scope "/api", Pleroma.Web do
index 9e3251df6db6ece674fdd7170b4666e84fe9bdaf..da707dd6ebd05348234af6c3e1a29b510776977f 100644 (file)
@@ -21,8 +21,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     ActivityPub.insert(activity)
   end
 
-  def fetch_public_statuses do
-    activities = ActivityPub.fetch_public_activities
+  def fetch_public_statuses(opts \\ %{}) do
+    activities = ActivityPub.fetch_public_activities(opts)
 
     Enum.map(activities, fn(activity) ->
       actor = get_in(activity.data, ["actor"])
index 1f0a547d0f9e11a3dbd45a03e9895bf8a26e39ad..404afef6d74e3f5272d3c25461490613d3806844 100644 (file)
@@ -16,6 +16,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
   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
     conn
     |> put_resp_content_type("application/json")
index 6f636d5d1716ad362ddaec8087f8c3eeedbfb083..ba157f191739b0f5bf38603be5ba67303bd445b5 100644 (file)
@@ -41,7 +41,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       since_id = List.last(activities).id
       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)
 
       assert length(activities) == 10
index c3241817d0fca3752dfc9e5399b1b318ee4df0df..e243942c6e8b24fcbb1538499e99dfb623a7facd 100644 (file)
@@ -1,7 +1,7 @@
 defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   use Pleroma.Web.ConnCase
   alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
-  alias Pleroma.Builders.UserBuilder
+  alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
   alias Pleroma.{Repo, Activity}
 
   describe "POST /api/account/verify_credentials" do
@@ -36,6 +36,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     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
     { :ok, user } = UserBuilder.insert
     [user: user]