Reduce OP fetching queries.
This commit is contained in:
parent
ff7afe46b9
commit
37e5014d3e
|
@ -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
|
||||
|
|
|
@ -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"] || [])
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue