Add user timeline
authordtluna <dtluna@openmailbox.org>
Fri, 14 Apr 2017 13:09:13 +0000 (16:09 +0300)
committerdtluna <dtluna@openmailbox.org>
Fri, 14 Apr 2017 13:09:13 +0000 (16:09 +0300)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_test.exs

index 75e4101f281d8a7b41e43891993a0407f2075cad..406ca5c421dcd754816c7ae9a044d879ccb58389 100644 (file)
@@ -60,6 +60,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       query
     end
 
+    query = if opts["actor_id"] do
+      from activity in query,
+        where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
+    else
+      query
+    end
+
     Repo.all(query)
     |> Enum.reverse
   end
index 0217b28d64139a815636f36a3908b3e1605fd732..0641200312e86f02b31bf34919f47c4f1f68c79d 100644 (file)
@@ -78,6 +78,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     |> activities_to_statuses(%{for: user})
   end
 
+  def fetch_user_statuses(user, opts \\ %{}) do
+    target = get_user(user, opts)
+    ActivityPub.fetch_activities([], Map.merge(opts, %{"actor_id" => target.ap_id}))
+    |> activities_to_statuses(%{for: user})
+  end
+
   def fetch_conversation(user, id) do
     query = from activity in Activity,
       where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
@@ -188,4 +194,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   defp make_date do
     DateTime.utc_now() |> DateTime.to_iso8601
   end
+
+  defp get_user(user, params) do
+    case params do
+      %{ "user_id" => user_id } ->
+        Repo.get(User, user_id)
+      %{ "screen_name" => nickname } ->
+        Repo.get_by!(User, nickname: nickname)
+      _ ->
+        user
+    end
+  end
 end
index 11e7b3bdf13d0af0ae1d12397d432978df666919..1d9d7f5402e2e20d52f8805aaf54a95080f5cc1b 100644 (file)
@@ -43,24 +43,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> json_reply(200, json)
   end
 
-  def user_timeline(conn, %{ "user_id" => user_id } = params) do
-    user = Repo.get(User, user_id)
-    conn = Map.merge(conn, %{assigns: %{user: user}})
-    params = Map.delete(params, "user_id")
-
-    friends_timeline(conn, params)
-  end
-
-  def user_timeline(conn, %{ "screen_name" => nickname} = params) do
-    user = Repo.get_by!(User, nickname: nickname)
-    conn = Map.merge(conn, %{assigns: %{user: user}})
-    params = Map.delete(params, "screen_name")
-
-    friends_timeline(conn, params)
-  end
-
-  def user_timeline(conn, params) do
-    friends_timeline(conn, params)
+  def user_timeline(%{assigns: %{user: user}} = conn, params) do
+    statuses = TwitterAPI.fetch_user_statuses(user, params)
+    conn
+    |> json_reply(200, statuses |> Poison.encode!)
   end
 
   def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
index e8853a9100c249f05aaa98f2c06376ec09934398..558f26e74def1c94330d9e5eaf486142284179de 100644 (file)
@@ -96,6 +96,24 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]})
   end
 
+  test "fetch user's statuses" do
+    {:ok, user1} = UserBuilder.insert(%{ap_id: "some id"})
+    {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2"})
+
+    {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
+    {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
+
+    user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{})
+
+    assert length(user1_statuses) == 1
+    assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
+
+    user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"screen_name" => user2.nickname })
+
+    assert length(user2_statuses) == 1
+    assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
+  end
+
   test "fetch a single status" do
     {:ok, activity} = ActivityBuilder.insert()
     {:ok, user} = UserBuilder.insert()