Notifications: filter on users rather than activities

This commit is contained in:
Matthieu Rakotojaona 2024-03-13 13:46:37 +01:00
parent d5b64846ed
commit a48f5f860e
3 changed files with 9 additions and 8 deletions

View file

@ -0,0 +1 @@
Notifications: improve performance by filtering on users table instead of activities table

View file

@ -241,13 +241,13 @@ defmodule Pleroma.FollowingRelationship do
end end
@doc """ @doc """
For a query with joined activity, For a query with joined activity's actor,
keeps rows where activity's actor is followed by user -or- is NOT domain-blocked by user. keeps rows where actor is followed by user -or- is NOT domain-blocked by user.
""" """
def keep_following_or_not_domain_blocked(query, user) do def keep_following_or_not_domain_blocked(query, user) do
where( where(
query, query,
[_, activity], [_, user_actor: user_actor],
fragment( fragment(
# "(actor's domain NOT in domain_blocks) OR (actor IS in followed AP IDs)" # "(actor's domain NOT in domain_blocks) OR (actor IS in followed AP IDs)"
""" """
@ -255,9 +255,9 @@ defmodule Pleroma.FollowingRelationship do
? = ANY(SELECT ap_id FROM users AS u INNER JOIN following_relationships AS fr ? = ANY(SELECT ap_id FROM users AS u INNER JOIN following_relationships AS fr
ON u.id = fr.following_id WHERE fr.follower_id = ? AND fr.state = ?) ON u.id = fr.following_id WHERE fr.follower_id = ? AND fr.state = ?)
""", """,
activity.actor, user_actor.ap_id,
^user.domain_blocks, ^user.domain_blocks,
activity.actor, user_actor.ap_id,
^User.binary_id(user.id), ^User.binary_id(user.id),
^accept_state_code() ^accept_state_code()
) )

View file

@ -137,7 +137,7 @@ defmodule Pleroma.Notification do
blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user) blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user)
query query
|> where([n, a], a.actor not in ^blocked_ap_ids) |> where([..., user_actor: user_actor], user_actor.ap_id not in ^blocked_ap_ids)
|> FollowingRelationship.keep_following_or_not_domain_blocked(user) |> FollowingRelationship.keep_following_or_not_domain_blocked(user)
end end
@ -148,7 +148,7 @@ defmodule Pleroma.Notification do
blocker_ap_ids = User.incoming_relationships_ungrouped_ap_ids(user, [:block]) blocker_ap_ids = User.incoming_relationships_ungrouped_ap_ids(user, [:block])
query query
|> where([n, a], a.actor not in ^blocker_ap_ids) |> where([..., user_actor: user_actor], user_actor.ap_id not in ^blocker_ap_ids)
end end
end end
@ -161,7 +161,7 @@ defmodule Pleroma.Notification do
opts[:notification_muted_users_ap_ids] || User.notification_muted_users_ap_ids(user) opts[:notification_muted_users_ap_ids] || User.notification_muted_users_ap_ids(user)
query query
|> where([n, a], a.actor not in ^notification_muted_ap_ids) |> where([..., user_actor: user_actor], user_actor.ap_id not in ^notification_muted_ap_ids)
|> join(:left, [n, a], tm in ThreadMute, |> join(:left, [n, a], tm in ThreadMute,
on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data), on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
as: :thread_mute as: :thread_mute