From 18cd04ade576969b25e96733dddb8255fbf13a6d Mon Sep 17 00:00:00 2001 From: dtluna Date: Fri, 14 Apr 2017 16:09:13 +0300 Subject: [PATCH] Add user timeline --- lib/pleroma/web/activity_pub/activity_pub.ex | 7 ++++++ lib/pleroma/web/twitter_api/twitter_api.ex | 17 ++++++++++++++ .../web/twitter_api/twitter_api_controller.ex | 22 ++++--------------- test/web/twitter_api/twitter_api_test.exs | 18 +++++++++++++++ 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 75e4101f2..406ca5c42 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -60,6 +60,13 @@ def fetch_activities(recipients, opts \\ %{}) 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 0217b28d6..064120031 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -78,6 +78,12 @@ def fetch_public_statuses(user, opts \\ %{}) 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 @@ defp activity_to_status(activity, opts) 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 11e7b3bdf..1d9d7f540 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -43,24 +43,10 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) 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 diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index e8853a910..558f26e74 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -96,6 +96,24 @@ test "fetch friends' statuses" 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()