X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fnotification.ex;h=3ef3b3f58bf94f9752d806038b63e0c058896c20;hb=51ce0813d351cad900966aea49a6f6d17d33f964;hp=f37e7ec671ee88c39848cc92a7f715621766999e;hpb=27cd1374e3c1768a1a0b8abb40d5b56e6e021d2d;p=akkoma
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index f37e7ec67..3ef3b3f58 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Notification do
@@ -21,6 +21,8 @@ defmodule Pleroma.Notification do
@type t :: %__MODULE__{}
+ @include_muted_option :with_muted
+
schema "notifications" do
field(:seen, :boolean, default: false)
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
@@ -34,7 +36,25 @@ defmodule Pleroma.Notification do
|> cast(attrs, [:seen])
end
- def for_user_query(user, opts \\ []) do
+ defp for_user_query_ap_id_opts(user, opts) do
+ ap_id_relations =
+ [:block] ++
+ if opts[@include_muted_option], do: [], else: [:notification_mute]
+
+ preloaded_ap_ids = User.outgoing_relations_ap_ids(user, ap_id_relations)
+
+ exclude_blocked_opts = Map.merge(%{blocked_users_ap_ids: preloaded_ap_ids[:block]}, opts)
+
+ exclude_notification_muted_opts =
+ Map.merge(%{notification_muted_users_ap_ids: preloaded_ap_ids[:notification_mute]}, opts)
+
+ {exclude_blocked_opts, exclude_notification_muted_opts}
+ end
+
+ def for_user_query(user, opts \\ %{}) do
+ {exclude_blocked_opts, exclude_notification_muted_opts} =
+ for_user_query_ap_id_opts(user, opts)
+
Notification
|> where(user_id: ^user.id)
|> where(
@@ -54,27 +74,32 @@ defmodule Pleroma.Notification do
)
)
|> preload([n, a, o], activity: {a, object: o})
- |> exclude_muted(user, opts)
- |> exclude_blocked(user)
+ |> exclude_notification_muted(user, exclude_notification_muted_opts)
+ |> exclude_blocked(user, exclude_blocked_opts)
|> exclude_visibility(opts)
end
- defp exclude_blocked(query, user) do
+ defp exclude_blocked(query, user, opts) do
+ blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user)
+
query
- |> where([n, a], a.actor not in ^user.blocks)
+ |> where([n, a], a.actor not in ^blocked_ap_ids)
|> where(
[n, a],
fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.domain_blocks
)
end
- defp exclude_muted(query, _, %{with_muted: true}) do
+ defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do
query
end
- defp exclude_muted(query, user, _opts) do
+ defp exclude_notification_muted(query, user, opts) do
+ notification_muted_ap_ids =
+ opts[:notification_muted_users_ap_ids] || User.notification_muted_users_ap_ids(user)
+
query
- |> where([n, a], a.actor not in ^user.muted_notifications)
+ |> where([n, a], a.actor not in ^notification_muted_ap_ids)
|> join(:left, [n, a], tm in Pleroma.ThreadMute,
on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
)
@@ -87,10 +112,28 @@ defmodule Pleroma.Notification do
when is_list(visibility) do
if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
query
+ |> join(:left, [n, a], mutated_activity in Pleroma.Activity,
+ on:
+ fragment("?->>'context'", a.data) ==
+ fragment("?->>'context'", mutated_activity.data) and
+ fragment("(?->>'type' = 'Like' or ?->>'type' = 'Announce')", a.data, a.data) and
+ fragment("?->>'type'", mutated_activity.data) == "Create",
+ as: :mutated_activity
+ )
|> where(
- [n, a],
+ [n, a, mutated_activity: mutated_activity],
not fragment(
- "activity_visibility(?, ?, ?) = ANY (?)",
+ """
+ CASE WHEN (?->>'type') = 'Like' or (?->>'type') = 'Announce'
+ THEN (activity_visibility(?, ?, ?) = ANY (?))
+ ELSE (activity_visibility(?, ?, ?) = ANY (?)) END
+ """,
+ a.data,
+ a.data,
+ mutated_activity.actor,
+ mutated_activity.recipients,
+ mutated_activity.data,
+ ^visibility,
a.actor,
a.recipients,
a.data,
@@ -105,17 +148,7 @@ defmodule Pleroma.Notification do
defp exclude_visibility(query, %{exclude_visibilities: visibility})
when visibility in @valid_visibilities do
- query
- |> where(
- [n, a],
- not fragment(
- "activity_visibility(?, ?, ?) = (?)",
- a.actor,
- a.recipients,
- a.data,
- ^visibility
- )
- )
+ exclude_visibility(query, [visibility])
end
defp exclude_visibility(query, %{exclude_visibilities: visibility})
@@ -252,7 +285,7 @@ defmodule Pleroma.Notification do
end
def create_notifications(%Activity{data: %{"type" => type}} = activity)
- when type in ["Like", "Announce", "Follow", "Move"] do
+ when type in ["Like", "Announce", "Follow", "Move", "EmojiReact"] do
notifications =
activity
|> get_notified_from_activity()
@@ -280,7 +313,7 @@ defmodule Pleroma.Notification do
def get_notified_from_activity(activity, local_only \\ true)
def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
- when type in ["Create", "Like", "Announce", "Follow", "Move"] do
+ when type in ["Create", "Like", "Announce", "Follow", "Move", "EmojiReact"] do
[]
|> Utils.maybe_notify_to_recipients(activity)
|> Utils.maybe_notify_mentioned_recipients(activity)
@@ -313,7 +346,7 @@ defmodule Pleroma.Notification do
def skip?(
:followers,
activity,
- %{notification_settings: %{"followers" => false}} = user
+ %{notification_settings: %{followers: false}} = user
) do
actor = activity.data["actor"]
follower = User.get_cached_by_ap_id(actor)
@@ -323,14 +356,14 @@ defmodule Pleroma.Notification do
def skip?(
:non_followers,
activity,
- %{notification_settings: %{"non_followers" => false}} = user
+ %{notification_settings: %{non_followers: false}} = user
) do
actor = activity.data["actor"]
follower = User.get_cached_by_ap_id(actor)
!User.following?(follower, user)
end
- def skip?(:follows, activity, %{notification_settings: %{"follows" => false}} = user) do
+ def skip?(:follows, activity, %{notification_settings: %{follows: false}} = user) do
actor = activity.data["actor"]
followed = User.get_cached_by_ap_id(actor)
User.following?(user, followed)
@@ -339,7 +372,7 @@ defmodule Pleroma.Notification do
def skip?(
:non_follows,
activity,
- %{notification_settings: %{"non_follows" => false}} = user
+ %{notification_settings: %{non_follows: false}} = user
) do
actor = activity.data["actor"]
followed = User.get_cached_by_ap_id(actor)