Reduce OP fetching queries.
authorlain <lain@soykaf.club>
Tue, 27 Mar 2018 16:18:24 +0000 (18:18 +0200)
committerlain <lain@soykaf.club>
Tue, 27 Mar 2018 16:18:24 +0000 (18:18 +0200)
lib/pleroma/activity.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/status_view_test.exs

index a8154859a7f81429eac2b293c2a32cd44f8a34f9..96ddcc480866835e6a186ddde84fa2e7345f63b9 100644 (file)
@@ -38,9 +38,14 @@ defmodule Pleroma.Activity do
     Repo.all(all_by_object_ap_id_q(ap_id))
   end
 
+  def create_activity_by_object_id_query(ap_ids) do
+    from activity in Activity,
+      where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)", activity.data, activity.data, ^ap_ids),
+      where: fragment("(?)->>'type' = 'Create'", activity.data)
+  end
+
   def get_create_activity_by_object_ap_id(ap_id) do
-    Repo.one(from activity in Activity,
-      where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id)),
-      where: fragment("(?)->>'type' = 'Create'", activity.data))
+    create_activity_by_object_id_query([ap_id])
+    |> Repo.one
   end
 end
index 822a6800cd59b6b4b94d1f119dac0d86ace217f2..a4539d47efa51bb0d7b4a65ed00ca337be129299 100644 (file)
@@ -4,9 +4,25 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   alias Pleroma.{User, Activity}
   alias Pleroma.Web.CommonAPI.Utils
   alias Pleroma.Web.MediaProxy
+  alias Pleroma.Repo
+
+  # TODO: Add cached version.
+  defp get_replied_to_activities(activities) do
+    activities
+    |> Enum.map(fn
+      (%{data: %{"type" => "Create", "object" => %{"inReplyTo" => inReplyTo}}}) ->
+        (inReplyTo != "") && inReplyTo
+      _ -> nil
+    end)
+    |> Enum.filter(&(&1))
+    |> Activity.create_activity_by_object_id_query()
+    |> Repo.all
+    |> Enum.reduce(%{}, fn(activity, acc) -> Map.put(acc,activity.data["object"]["id"], activity) end)
+  end
 
   def render("index.json", opts) do
-    render_many(opts.activities, StatusView, "status.json", opts)
+    replied_to_activities = get_replied_to_activities(opts.activities)
+    render_many(opts.activities, StatusView, "status.json", Map.put(opts, :replied_to_activities, replied_to_activities))
   end
 
   def render("status.json", %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts) do
@@ -51,6 +67,19 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     }
   end
 
+  def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
+    id = activity.data["object"]["inReplyTo"]
+    replied_to_activities[activity.data["object"]["inReplyTo"]]
+  end
+
+  def get_reply_to(%{data: %{"object" => object}}, _) do
+    if object["inReplyTo"] && object["inReplyTo"] != "" do
+      Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
+    else
+      nil
+    end
+  end
+
   def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
     user = User.get_cached_by_ap_id(activity.data["actor"])
 
@@ -72,12 +101,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
 
     created_at = Utils.to_masto_date(object["published"])
 
-    # TODO: Add cached version.
-    reply_to = if object["inReplyTo"] && object["inReplyTo"] != "" do
-      Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
-    else
-      nil
-    end
+    reply_to = get_reply_to(activity, opts)
     reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
 
     emojis = (activity.data["object"]["emoji"] || [])
index b0812763971bd54e53176b5eb5a1e6ce3c112fa3..74f5b71337d37103eadc0836d5a4a2b241d20a68 100644 (file)
@@ -59,7 +59,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
     user = insert(:user)
     {:ok, activity} = CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
 
-    assert activity.data["object"]["inReplyTo"] == note.data["object"]["id"]
+    status = StatusView.render("status.json", %{activity: activity})
+
+    assert status.in_reply_to_id == note.id
+
+    [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
+
+    assert status.in_reply_to_id == note.id
   end
 
   test "contains mentions" do