mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-27 00:09:35 +00:00
Implemented preloading of relationships with parent activities' actors for statuses/timeline rendering. Applied preloading for notifications rendering. Fixed announces rendering issue (preloading-related).
This commit is contained in:
parent
eec1fcaf55
commit
13cbb9f6ad
|
@ -35,6 +35,13 @@ defmodule Pleroma.Activity.Queries do
|
||||||
from(a in query, where: a.actor == ^ap_id)
|
from(a in query, where: a.actor == ^ap_id)
|
||||||
end
|
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
|
@spec by_object_id(query, String.t() | [String.t()]) :: query
|
||||||
def by_object_id(query \\ Activity, object_id)
|
def by_object_id(query \\ Activity, object_id)
|
||||||
|
|
||||||
|
|
|
@ -46,8 +46,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
"relationship.json",
|
"relationship.json",
|
||||||
%{user: %User{} = reading_user, target: %User{} = target} = opts
|
%{user: %User{} = reading_user, target: %User{} = target} = opts
|
||||||
) do
|
) do
|
||||||
user_relationships = Map.get(opts, :user_relationships)
|
user_relationships = get_in(opts, [:relationships, :user_relationships])
|
||||||
following_relationships = opts[:following_relationships]
|
following_relationships = get_in(opts, [:relationships, :following_relationships])
|
||||||
|
|
||||||
follow_state =
|
follow_state =
|
||||||
if following_relationships do
|
if following_relationships do
|
||||||
|
@ -61,17 +61,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
|
|
||||||
followed_by =
|
followed_by =
|
||||||
if following_relationships do
|
if following_relationships do
|
||||||
with %{state: "accept"} <-
|
case find_following_rel(following_relationships, target, reading_user) do
|
||||||
find_following_rel(following_relationships, target, reading_user) do
|
%{state: "accept"} -> true
|
||||||
true
|
|
||||||
else
|
|
||||||
_ -> false
|
_ -> false
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
User.following?(target, reading_user)
|
User.following?(target, reading_user)
|
||||||
end
|
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),
|
id: to_string(target.id),
|
||||||
following: follow_state == "accept",
|
following: follow_state == "accept",
|
||||||
|
@ -173,8 +171,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
render("relationship.json", %{
|
render("relationship.json", %{
|
||||||
user: opts[:for],
|
user: opts[:for],
|
||||||
target: user,
|
target: user,
|
||||||
user_relationships: opts[:user_relationships],
|
relationships: opts[:relationships]
|
||||||
following_relationships: opts[:following_relationships]
|
|
||||||
})
|
})
|
||||||
|
|
||||||
%{
|
%{
|
||||||
|
|
|
@ -13,19 +13,68 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
|
||||||
alias Pleroma.Web.MastodonAPI.NotificationView
|
alias Pleroma.Web.MastodonAPI.NotificationView
|
||||||
alias Pleroma.Web.MastodonAPI.StatusView
|
alias Pleroma.Web.MastodonAPI.StatusView
|
||||||
|
|
||||||
def render("index.json", %{notifications: notifications, for: user}) do
|
def render("index.json", %{notifications: notifications, for: reading_user}) do
|
||||||
safe_render_many(notifications, NotificationView, "show.json", %{for: user})
|
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
|
end
|
||||||
|
|
||||||
def render("show.json", %{
|
def render(
|
||||||
|
"show.json",
|
||||||
|
%{
|
||||||
notification: %Notification{activity: activity} = notification,
|
notification: %Notification{activity: activity} = notification,
|
||||||
for: user
|
for: reading_user
|
||||||
}) do
|
} = opts
|
||||||
|
) do
|
||||||
actor = User.get_cached_by_ap_id(activity.data["actor"])
|
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)
|
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 = %{
|
response = %{
|
||||||
id: to_string(notification.id),
|
id: to_string(notification.id),
|
||||||
type: mastodon_type,
|
type: mastodon_type,
|
||||||
|
@ -36,24 +85,28 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
relationships_opts = %{relationships: opts[:relationships]}
|
||||||
|
|
||||||
case mastodon_type do
|
case mastodon_type do
|
||||||
"mention" ->
|
"mention" ->
|
||||||
put_status(response, activity, user)
|
put_status(response, activity, reading_user, relationships_opts)
|
||||||
|
|
||||||
"favourite" ->
|
"favourite" ->
|
||||||
put_status(response, parent_activity, user)
|
put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
|
||||||
|
|
||||||
"reblog" ->
|
"reblog" ->
|
||||||
put_status(response, parent_activity, user)
|
put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
|
||||||
|
|
||||||
"move" ->
|
"move" ->
|
||||||
put_target(response, activity, user)
|
put_target(response, activity, reading_user, relationships_opts)
|
||||||
|
|
||||||
"follow" ->
|
"follow" ->
|
||||||
response
|
response
|
||||||
|
|
||||||
"pleroma:emoji_reaction" ->
|
"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
|
nil
|
||||||
|
@ -64,16 +117,21 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp put_emoji(response, activity) do
|
defp put_emoji(response, activity) do
|
||||||
response
|
Map.put(response, :emoji, activity.data["content"])
|
||||||
|> Map.put(:emoji, activity.data["content"])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp put_status(response, activity, user) do
|
defp put_status(response, activity, reading_user, opts) do
|
||||||
Map.put(response, :status, StatusView.render("show.json", %{activity: activity, for: user}))
|
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
|
end
|
||||||
|
|
||||||
defp put_target(response, activity, user) do
|
defp put_target(response, activity, reading_user, opts) do
|
||||||
target = User.get_cached_by_ap_id(activity.data["target"])
|
target_user = User.get_cached_by_ap_id(activity.data["target"])
|
||||||
Map.put(response, :target, AccountView.render("show.json", %{user: target, for: user}))
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -72,14 +72,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
present?(user && user.ap_id in (object.data["announcements"] || []))
|
present?(user && user.ap_id in (object.data["announcements"] || []))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp relationships_opts(opts) do
|
def relationships_opts(_reading_user = nil, _actors) do
|
||||||
reading_user = opts[:for]
|
%{user_relationships: [], following_relationships: []}
|
||||||
|
end
|
||||||
{user_relationships, following_relationships} =
|
|
||||||
if reading_user do
|
|
||||||
activities = opts[:activities]
|
|
||||||
actors = Enum.map(activities, fn a -> get_user(a.data["actor"]) end)
|
|
||||||
|
|
||||||
|
def relationships_opts(reading_user, actors) do
|
||||||
user_relationships =
|
user_relationships =
|
||||||
UserRelationship.dictionary(
|
UserRelationship.dictionary(
|
||||||
[reading_user],
|
[reading_user],
|
||||||
|
@ -88,25 +85,33 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
[:block, :inverse_subscription]
|
[:block, :inverse_subscription]
|
||||||
)
|
)
|
||||||
|
|
||||||
following_relationships =
|
following_relationships = FollowingRelationship.all_between_user_sets([reading_user], actors)
|
||||||
FollowingRelationship.all_between_user_sets([reading_user], actors)
|
|
||||||
|
|
||||||
{user_relationships, following_relationships}
|
|
||||||
else
|
|
||||||
{[], []}
|
|
||||||
end
|
|
||||||
|
|
||||||
%{user_relationships: user_relationships, following_relationships: following_relationships}
|
%{user_relationships: user_relationships, following_relationships: following_relationships}
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("index.json", opts) do
|
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)
|
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 =
|
||||||
opts
|
opts
|
||||||
|> Map.put(:replied_to_activities, replied_to_activities)
|
|> 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)
|
safe_render_many(activities, StatusView, "show.json", opts)
|
||||||
end
|
end
|
||||||
|
@ -119,17 +124,25 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
created_at = Utils.to_masto_date(activity.data["published"])
|
created_at = Utils.to_masto_date(activity.data["published"])
|
||||||
activity_object = Object.normalize(activity)
|
activity_object = Object.normalize(activity)
|
||||||
|
|
||||||
reblogged_activity =
|
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.create_by_object_ap_id(activity_object.data["id"])
|
||||||
|> Activity.with_preloaded_bookmark(opts[:for])
|
|> Activity.with_preloaded_bookmark(opts[:for])
|
||||||
|> Activity.with_set_thread_muted_field(opts[:for])
|
|> Activity.with_set_thread_muted_field(opts[:for])
|
||||||
|> Repo.one()
|
|> 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"] || [])
|
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 =
|
mentions =
|
||||||
activity.recipients
|
activity.recipients
|
||||||
|
@ -145,8 +158,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
AccountView.render("show.json", %{
|
AccountView.render("show.json", %{
|
||||||
user: user,
|
user: user,
|
||||||
for: opts[:for],
|
for: opts[:for],
|
||||||
user_relationships: opts[:user_relationships],
|
relationships: opts[:relationships]
|
||||||
following_relationships: opts[:following_relationships]
|
|
||||||
}),
|
}),
|
||||||
in_reply_to_id: nil,
|
in_reply_to_id: nil,
|
||||||
in_reply_to_account_id: nil,
|
in_reply_to_account_id: nil,
|
||||||
|
@ -156,7 +168,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
reblogs_count: 0,
|
reblogs_count: 0,
|
||||||
replies_count: 0,
|
replies_count: 0,
|
||||||
favourites_count: 0,
|
favourites_count: 0,
|
||||||
reblogged: reblogged?(reblogged_activity, opts[:for]),
|
reblogged: reblogged?(reblogged_parent_activity, opts[:for]),
|
||||||
favourited: present?(favorited),
|
favourited: present?(favorited),
|
||||||
bookmarked: present?(bookmarked),
|
bookmarked: present?(bookmarked),
|
||||||
muted: false,
|
muted: false,
|
||||||
|
@ -293,12 +305,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
_ -> []
|
_ -> []
|
||||||
end
|
end
|
||||||
|
|
||||||
user_relationships_opt = opts[:user_relationships]
|
|
||||||
|
|
||||||
muted =
|
muted =
|
||||||
thread_muted? ||
|
thread_muted? ||
|
||||||
UserRelationship.exists?(
|
UserRelationship.exists?(
|
||||||
user_relationships_opt,
|
get_in(opts, [:relationships, :user_relationships]),
|
||||||
:mute,
|
:mute,
|
||||||
opts[:for],
|
opts[:for],
|
||||||
user,
|
user,
|
||||||
|
@ -313,8 +323,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
AccountView.render("show.json", %{
|
AccountView.render("show.json", %{
|
||||||
user: user,
|
user: user,
|
||||||
for: opts[:for],
|
for: opts[:for],
|
||||||
user_relationships: user_relationships_opt,
|
relationships: opts[:relationships]
|
||||||
following_relationships: opts[:following_relationships]
|
|
||||||
}),
|
}),
|
||||||
in_reply_to_id: reply_to && to_string(reply_to.id),
|
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),
|
in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
|
||||||
|
|
|
@ -68,7 +68,6 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
|
||||||
"account" => %{
|
"account" => %{
|
||||||
"acct" => "repeated",
|
"acct" => "repeated",
|
||||||
"pleroma" => %{
|
"pleroma" => %{
|
||||||
# This part does not match correctly
|
|
||||||
"relationship" => %{"following" => false, "followed_by" => true}
|
"relationship" => %{"following" => false, "followed_by" => true}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue