Implemented preloading of relationships with parent activities' actors for statuses...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 24 Mar 2020 19:14:26 +0000 (22:14 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 24 Mar 2020 19:14:26 +0000 (22:14 +0300)
lib/pleroma/activity/queries.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/mastodon_api/views/notification_view.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/controllers/timeline_controller_test.exs

index 04593b9fb70f32dcbaf16d291c8a2e56b7827023..a34c20343471582e7c0dbb8e0bebd647d5cd83e0 100644 (file)
@@ -35,6 +35,13 @@ defmodule Pleroma.Activity.Queries do
     from(a in query, where: a.actor == ^ap_id)
   end
 
+  def find_by_object_ap_id(activities, object_ap_id) do
+    Enum.find(
+      activities,
+      &(object_ap_id in [is_map(&1.data["object"]) && &1.data["object"]["id"], &1.data["object"]])
+    )
+  end
+
   @spec by_object_id(query, String.t() | [String.t()]) :: query
   def by_object_id(query \\ Activity, object_id)
 
index 2fe46158b305d1e8dce2922aca2134f61705fd04..89bea995733512f1d402c5c9ace34b6ab523eca0 100644 (file)
@@ -46,8 +46,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
         "relationship.json",
         %{user: %User{} = reading_user, target: %User{} = target} = opts
       ) do
-    user_relationships = Map.get(opts, :user_relationships)
-    following_relationships = opts[:following_relationships]
+    user_relationships = get_in(opts, [:relationships, :user_relationships])
+    following_relationships = get_in(opts, [:relationships, :following_relationships])
 
     follow_state =
       if following_relationships do
@@ -61,17 +61,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
     followed_by =
       if following_relationships do
-        with %{state: "accept"} <-
-               find_following_rel(following_relationships, target, reading_user) do
-          true
-        else
+        case find_following_rel(following_relationships, target, reading_user) do
+          %{state: "accept"} -> true
           _ -> false
         end
       else
         User.following?(target, reading_user)
       end
 
-    # TODO: add a note on adjusting StatusView.user_relationships_opt/1 re: preloading of user relations
+    # NOTE: adjust StatusView.relationships_opts/2 if adding new relation-related flags
     %{
       id: to_string(target.id),
       following: follow_state == "accept",
@@ -173,8 +171,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
       render("relationship.json", %{
         user: opts[:for],
         target: user,
-        user_relationships: opts[:user_relationships],
-        following_relationships: opts[:following_relationships]
+        relationships: opts[:relationships]
       })
 
     %{
index 33145c484d6f74fe03e95a88292ebd9cb58d2bc1..e9c618496e710cda79e2956c95e608779d717301 100644 (file)
@@ -13,19 +13,68 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
   alias Pleroma.Web.MastodonAPI.NotificationView
   alias Pleroma.Web.MastodonAPI.StatusView
 
-  def render("index.json", %{notifications: notifications, for: user}) do
-    safe_render_many(notifications, NotificationView, "show.json", %{for: user})
+  def render("index.json", %{notifications: notifications, for: reading_user}) do
+    activities = Enum.map(notifications, & &1.activity)
+
+    parent_activities =
+      activities
+      |> Enum.filter(
+        &(Activity.mastodon_notification_type(&1) in [
+            "favourite",
+            "reblog",
+            "pleroma:emoji_reaction"
+          ])
+      )
+      |> Enum.map(& &1.data["object"])
+      |> Activity.create_by_object_ap_id()
+      |> Activity.with_preloaded_object(:left)
+      |> Pleroma.Repo.all()
+
+    move_activities_targets =
+      activities
+      |> Enum.filter(&(Activity.mastodon_notification_type(&1) == "move"))
+      |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
+
+    actors =
+      activities
+      |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
+      |> Enum.filter(& &1)
+      |> Kernel.++(move_activities_targets)
+
+    opts = %{
+      for: reading_user,
+      parent_activities: parent_activities,
+      relationships: StatusView.relationships_opts(reading_user, actors)
+    }
+
+    safe_render_many(notifications, NotificationView, "show.json", opts)
   end
 
-  def render("show.json", %{
-        notification: %Notification{activity: activity} = notification,
-        for: user
-      }) do
+  def render(
+        "show.json",
+        %{
+          notification: %Notification{activity: activity} = notification,
+          for: reading_user
+        } = opts
+      ) do
     actor = User.get_cached_by_ap_id(activity.data["actor"])
-    parent_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
+
+    parent_activity_fn = fn ->
+      if opts[:parent_activities] do
+        Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
+      else
+        Activity.get_create_by_object_ap_id(activity.data["object"])
+      end
+    end
+
     mastodon_type = Activity.mastodon_notification_type(activity)
 
-    with %{id: _} = account <- AccountView.render("show.json", %{user: actor, for: user}) do
+    with %{id: _} = account <-
+           AccountView.render("show.json", %{
+             user: actor,
+             for: reading_user,
+             relationships: opts[:relationships]
+           }) do
       response = %{
         id: to_string(notification.id),
         type: mastodon_type,
@@ -36,24 +85,28 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
         }
       }
 
+      relationships_opts = %{relationships: opts[:relationships]}
+
       case mastodon_type do
         "mention" ->
-          put_status(response, activity, user)
+          put_status(response, activity, reading_user, relationships_opts)
 
         "favourite" ->
-          put_status(response, parent_activity, user)
+          put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
 
         "reblog" ->
-          put_status(response, parent_activity, user)
+          put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
 
         "move" ->
-          put_target(response, activity, user)
+          put_target(response, activity, reading_user, relationships_opts)
 
         "follow" ->
           response
 
         "pleroma:emoji_reaction" ->
-          put_status(response, parent_activity, user) |> put_emoji(activity)
+          response
+          |> put_status(parent_activity_fn.(), reading_user, relationships_opts)
+          |> put_emoji(activity)
 
         _ ->
           nil
@@ -64,16 +117,21 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
   end
 
   defp put_emoji(response, activity) do
-    response
-    |> Map.put(:emoji, activity.data["content"])
+    Map.put(response, :emoji, activity.data["content"])
   end
 
-  defp put_status(response, activity, user) do
-    Map.put(response, :status, StatusView.render("show.json", %{activity: activity, for: user}))
+  defp put_status(response, activity, reading_user, opts) do
+    status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
+    status_render = StatusView.render("show.json", status_render_opts)
+
+    Map.put(response, :status, status_render)
   end
 
-  defp put_target(response, activity, user) do
-    target = User.get_cached_by_ap_id(activity.data["target"])
-    Map.put(response, :target, AccountView.render("show.json", %{user: target, for: user}))
+  defp put_target(response, activity, reading_user, opts) do
+    target_user = User.get_cached_by_ap_id(activity.data["target"])
+    target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
+    target_render = AccountView.render("show.json", target_render_opts)
+
+    Map.put(response, :target, target_render)
   end
 end
index 55a5513f9c4259746ea89a1e66df98f5782ce7f2..0ef65b3521a623fad0337da6c52cf25265818365 100644 (file)
@@ -72,41 +72,46 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     present?(user && user.ap_id in (object.data["announcements"] || []))
   end
 
-  defp relationships_opts(opts) do
-    reading_user = opts[:for]
-
-    {user_relationships, following_relationships} =
-      if reading_user do
-        activities = opts[:activities]
-        actors = Enum.map(activities, fn a -> get_user(a.data["actor"]) end)
-
-        user_relationships =
-          UserRelationship.dictionary(
-            [reading_user],
-            actors,
-            [:block, :mute, :notification_mute, :reblog_mute],
-            [:block, :inverse_subscription]
-          )
-
-        following_relationships =
-          FollowingRelationship.all_between_user_sets([reading_user], actors)
-
-        {user_relationships, following_relationships}
-      else
-        {[], []}
-      end
+  def relationships_opts(_reading_user = nil, _actors) do
+    %{user_relationships: [], following_relationships: []}
+  end
+
+  def relationships_opts(reading_user, actors) do
+    user_relationships =
+      UserRelationship.dictionary(
+        [reading_user],
+        actors,
+        [:block, :mute, :notification_mute, :reblog_mute],
+        [:block, :inverse_subscription]
+      )
+
+    following_relationships = FollowingRelationship.all_between_user_sets([reading_user], actors)
 
     %{user_relationships: user_relationships, following_relationships: following_relationships}
   end
 
   def render("index.json", opts) do
-    activities = opts.activities
+    # To do: check AdminAPIControllerTest on the reasons behind nil activities in the list
+    activities = Enum.filter(opts.activities, & &1)
     replied_to_activities = get_replied_to_activities(activities)
 
+    parent_activities =
+      activities
+      |> Enum.filter(&(&1.data["type"] == "Announce" && &1.data["object"]))
+      |> Enum.map(&Object.normalize(&1).data["id"])
+      |> Activity.create_by_object_ap_id()
+      |> Activity.with_preloaded_object(:left)
+      |> Activity.with_preloaded_bookmark(opts[:for])
+      |> Activity.with_set_thread_muted_field(opts[:for])
+      |> Repo.all()
+
+    actors = Enum.map(activities ++ parent_activities, &get_user(&1.data["actor"]))
+
     opts =
       opts
       |> Map.put(:replied_to_activities, replied_to_activities)
-      |> Map.merge(relationships_opts(opts))
+      |> Map.put(:parent_activities, parent_activities)
+      |> Map.put(:relationships, relationships_opts(opts[:for], actors))
 
     safe_render_many(activities, StatusView, "show.json", opts)
   end
@@ -119,17 +124,25 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     created_at = Utils.to_masto_date(activity.data["published"])
     activity_object = Object.normalize(activity)
 
-    reblogged_activity =
-      Activity.create_by_object_ap_id(activity_object.data["id"])
-      |> Activity.with_preloaded_bookmark(opts[:for])
-      |> Activity.with_set_thread_muted_field(opts[:for])
-      |> Repo.one()
+    reblogged_parent_activity =
+      if opts[:parent_activities] do
+        Activity.Queries.find_by_object_ap_id(
+          opts[:parent_activities],
+          activity_object.data["id"]
+        )
+      else
+        Activity.create_by_object_ap_id(activity_object.data["id"])
+        |> Activity.with_preloaded_bookmark(opts[:for])
+        |> Activity.with_set_thread_muted_field(opts[:for])
+        |> Repo.one()
+      end
 
-    reblogged = render("show.json", Map.put(opts, :activity, reblogged_activity))
+    reblog_rendering_opts = Map.put(opts, :activity, reblogged_parent_activity)
+    reblogged = render("show.json", reblog_rendering_opts)
 
     favorited = opts[:for] && opts[:for].ap_id in (activity_object.data["likes"] || [])
 
-    bookmarked = Activity.get_bookmark(reblogged_activity, opts[:for]) != nil
+    bookmarked = Activity.get_bookmark(reblogged_parent_activity, opts[:for]) != nil
 
     mentions =
       activity.recipients
@@ -145,8 +158,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         AccountView.render("show.json", %{
           user: user,
           for: opts[:for],
-          user_relationships: opts[:user_relationships],
-          following_relationships: opts[:following_relationships]
+          relationships: opts[:relationships]
         }),
       in_reply_to_id: nil,
       in_reply_to_account_id: nil,
@@ -156,7 +168,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       reblogs_count: 0,
       replies_count: 0,
       favourites_count: 0,
-      reblogged: reblogged?(reblogged_activity, opts[:for]),
+      reblogged: reblogged?(reblogged_parent_activity, opts[:for]),
       favourited: present?(favorited),
       bookmarked: present?(bookmarked),
       muted: false,
@@ -293,12 +305,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         _ -> []
       end
 
-    user_relationships_opt = opts[:user_relationships]
-
     muted =
       thread_muted? ||
         UserRelationship.exists?(
-          user_relationships_opt,
+          get_in(opts, [:relationships, :user_relationships]),
           :mute,
           opts[:for],
           user,
@@ -313,8 +323,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         AccountView.render("show.json", %{
           user: user,
           for: opts[:for],
-          user_relationships: user_relationships_opt,
-          following_relationships: opts[:following_relationships]
+          relationships: opts[:relationships]
         }),
       in_reply_to_id: reply_to && to_string(reply_to.id),
       in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
index 47849fc48d38d52ae67f70cb2fbe3ff61a2c26e9..97b1c3e66c35b06d82d1488d1b3b89b0a6f19f6f 100644 (file)
@@ -68,7 +68,6 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
                    "account" => %{
                      "acct" => "repeated",
                      "pleroma" => %{
-                       # This part does not match correctly
                        "relationship" => %{"following" => false, "followed_by" => true}
                      }
                    }