Add friends timeline.
authorRoger Braun <roger@rogerbraun.net>
Wed, 22 Mar 2017 16:25:59 +0000 (17:25 +0100)
committerRoger Braun <roger@rogerbraun.net>
Wed, 22 Mar 2017 16:25:59 +0000 (17:25 +0100)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/support/builders/activity_builder.ex
test/web/twitter_api/twitter_api_controller_test.exs

index aa2f7865ea3be7a9bb38e59415eced032896d3e6..c34f03cbba02aa1cf9ddd9fc0e6c1b2b61e8be0a 100644 (file)
@@ -30,5 +30,6 @@ defmodule Pleroma.Web.Router do
 
     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
   end
 end
index 404afef6d74e3f5272d3c25461490613d3806844..088439f488fbfca4fee55bb5107a2525790fd40e 100644 (file)
@@ -24,6 +24,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> json_reply(200, json)
   end
 
+  def friends_timeline(%{assigns: %{user: user}} = conn, params) do
+    statuses = TwitterAPI.fetch_friend_statuses(user, 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 b44afa896899c93e1496f40adcd74a7c39f79ed3..a82dc29d431f827d1073174495071cdde6fbfe55 100644 (file)
@@ -23,7 +23,7 @@ defmodule Pleroma.Builders.ActivityBuilder do
 
   def insert_list(times, data \\ %{}, opts \\ %{}) do
     Enum.map(1..times, fn (n) ->
-      {:ok, activity} = insert(%{"id" => n})
+      {:ok, activity} = insert(Map.merge(data, %{"id" => n}))
       activity
     end)
   end
index e243942c6e8b24fcbb1538499e99dfb623a7facd..86c03c652a89abee13396b81860546ef2d443b70 100644 (file)
@@ -2,7 +2,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   use Pleroma.Web.ConnCase
   alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
   alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
-  alias Pleroma.{Repo, Activity}
+  alias Pleroma.{Repo, Activity, User}
 
   describe "POST /api/account/verify_credentials" do
     setup [:valid_user]
@@ -52,8 +52,35 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "GET /statuses/friends_timeline.json" do
+    setup [:valid_user]
+    test "without valid credentials", %{conn: conn} do
+      conn = get conn, "/api/statuses/friends_timeline.json"
+      assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+    end
+
+    test "with credentials", %{conn: conn, user: current_user} do
+      {:ok, user} = UserBuilder.insert
+      activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
+      ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
+      {:ok, other_user} = UserBuilder.insert(%{ap_id: "glimmung", nickname: "nockame"})
+      ActivityBuilder.insert_list(10, %{}, %{user: other_user})
+      since_id = List.last(activities).id
+
+      current_user = Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) |> Repo.update!
+
+      conn = conn
+        |> with_credentials(current_user.nickname, "test")
+        |> get("/api/statuses/friends_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
+    { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
     [user: user]
   end