From 37e5014d3ee571622d324cdadfc2bf7c345cc1fc Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 27 Mar 2018 18:18:24 +0200 Subject: [PATCH] Reduce OP fetching queries. --- lib/pleroma/activity.ex | 11 ++++-- .../web/mastodon_api/views/status_view.ex | 38 +++++++++++++++---- test/web/mastodon_api/status_view_test.exs | 8 +++- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index a8154859a..96ddcc480 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -38,9 +38,14 @@ def all_by_object_ap_id(ap_id) 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 diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 822a6800c..a4539d47e 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -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 @@ def render("status.json", %{activity: %{data: %{"type" => "Announce", "object" = } 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 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity} 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"] || []) diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index b08127639..74f5b7133 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -59,7 +59,13 @@ test "a reply" 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