1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Notification do
10 alias Pleroma.FollowingRelationship
12 alias Pleroma.Notification
14 alias Pleroma.Pagination
16 alias Pleroma.ThreadMute
18 alias Pleroma.Web.CommonAPI
19 alias Pleroma.Web.CommonAPI.Utils
20 alias Pleroma.Web.Push
21 alias Pleroma.Web.Streamer
28 @type t :: %__MODULE__{}
30 @include_muted_option :with_muted
32 schema "notifications" do
33 field(:seen, :boolean, default: false)
34 # This is an enum type in the database. If you add a new notification type,
35 # remember to add a migration to add it to the `notifications_type` enum
38 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
39 belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
44 def update_notification_type(user, activity) do
45 with %__MODULE__{} = notification <-
46 Repo.get_by(__MODULE__, user_id: user.id, activity_id: activity.id) do
49 |> type_from_activity()
52 |> changeset(%{type: type})
57 @spec unread_notifications_count(User.t()) :: integer()
58 def unread_notifications_count(%User{id: user_id}) do
60 where: q.user_id == ^user_id and q.seen == false
62 |> Repo.aggregate(:count, :id)
65 @notification_types ~w{
72 pleroma:emoji_reaction
78 def changeset(%Notification{} = notification, attrs) do
80 |> cast(attrs, [:seen, :type])
81 |> validate_inclusion(:type, @notification_types)
84 @spec last_read_query(User.t()) :: Ecto.Queryable.t()
85 def last_read_query(user) do
86 from(q in Pleroma.Notification,
87 where: q.user_id == ^user.id,
88 where: q.seen == true,
89 select: type(q.id, :string),
95 defp for_user_query_ap_id_opts(user, opts) do
98 if opts[@include_muted_option], do: [], else: [:notification_mute]
100 preloaded_ap_ids = User.outgoing_relationships_ap_ids(user, ap_id_relationships)
102 exclude_blocked_opts = Map.merge(%{blocked_users_ap_ids: preloaded_ap_ids[:block]}, opts)
104 exclude_notification_muted_opts =
105 Map.merge(%{notification_muted_users_ap_ids: preloaded_ap_ids[:notification_mute]}, opts)
107 {exclude_blocked_opts, exclude_notification_muted_opts}
110 def for_user_query(user, opts \\ %{}) do
111 {exclude_blocked_opts, exclude_notification_muted_opts} =
112 for_user_query_ap_id_opts(user, opts)
115 |> where(user_id: ^user.id)
116 |> join(:inner, [n], activity in assoc(n, :activity))
117 |> join(:left, [n, a], object in Object,
120 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
126 |> join(:inner, [_n, a], u in User, on: u.ap_id == a.actor, as: :user_actor)
127 |> preload([n, a, o], activity: {a, object: o})
128 |> where([user_actor: user_actor], user_actor.is_active)
129 |> exclude_notification_muted(user, exclude_notification_muted_opts)
130 |> exclude_blocked(user, exclude_blocked_opts)
131 |> exclude_blockers(user)
132 |> exclude_filtered(user)
133 |> exclude_visibility(opts)
136 # Excludes blocked users and non-followed domain-blocked users
137 defp exclude_blocked(query, user, opts) do
138 blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user)
141 |> where([n, a], a.actor not in ^blocked_ap_ids)
142 |> FollowingRelationship.keep_following_or_not_domain_blocked(user)
145 defp exclude_blockers(query, user) do
146 if Pleroma.Config.get([:activitypub, :blockers_visible]) == true do
149 blocker_ap_ids = User.incoming_relationships_ungrouped_ap_ids(user, [:block])
152 |> where([n, a], a.actor not in ^blocker_ap_ids)
156 defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do
160 defp exclude_notification_muted(query, user, opts) do
161 notification_muted_ap_ids =
162 opts[:notification_muted_users_ap_ids] || User.notification_muted_users_ap_ids(user)
165 |> where([n, a], a.actor not in ^notification_muted_ap_ids)
166 |> join(:left, [n, a], tm in ThreadMute,
167 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
170 |> where([thread_mute: thread_mute], is_nil(thread_mute.user_id))
173 defp exclude_filtered(query, user) do
174 case Pleroma.Filter.compose_regex(user) do
179 from([_n, a, o] in query,
181 fragment("not(?->>'content' ~* ?)", o.data, ^regex) or
182 fragment("?->>'actor' = ?", o.data, ^user.ap_id)
187 @valid_visibilities ~w[direct unlisted public private]
189 defp exclude_visibility(query, %{exclude_visibilities: visibility})
190 when is_list(visibility) do
191 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
193 |> join(:left, [n, a], mutated_activity in Pleroma.Activity,
196 "COALESCE((?->'object')->>'id', ?->>'object')",
201 "COALESCE((?->'object')->>'id', ?->>'object')",
202 mutated_activity.data,
203 mutated_activity.data
205 fragment("(?->>'type' = 'Like' or ?->>'type' = 'Announce')", a.data, a.data) and
206 fragment("?->>'type'", mutated_activity.data) == "Create",
207 as: :mutated_activity
210 [n, a, mutated_activity: mutated_activity],
213 CASE WHEN (?->>'type') = 'Like' or (?->>'type') = 'Announce'
214 THEN (activity_visibility(?, ?, ?) = ANY (?))
215 ELSE (activity_visibility(?, ?, ?) = ANY (?)) END
219 mutated_activity.actor,
220 mutated_activity.recipients,
221 mutated_activity.data,
230 Logger.error("Could not exclude visibility to #{visibility}")
235 defp exclude_visibility(query, %{exclude_visibilities: visibility})
236 when visibility in @valid_visibilities do
237 exclude_visibility(query, [visibility])
240 defp exclude_visibility(query, %{exclude_visibilities: visibility})
241 when visibility not in @valid_visibilities do
242 Logger.error("Could not exclude visibility to #{visibility}")
246 defp exclude_visibility(query, _visibility), do: query
248 def for_user(user, opts \\ %{}) do
250 |> for_user_query(opts)
251 |> Pagination.fetch_paginated(opts)
255 Returns notifications for user received since given date.
259 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
260 [%Pleroma.Notification{}, %Pleroma.Notification{}]
262 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
265 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
266 def for_user_since(user, date) do
267 from(n in for_user_query(user),
268 where: n.updated_at > ^date
273 def set_read_up_to(%{id: user_id} = user, id) do
277 where: n.user_id == ^user_id,
279 where: n.seen == false,
280 # Ideally we would preload object and activities here
281 # but Ecto does not support preloads in update_all
285 {:ok, %{ids: {_, notification_ids}}} =
287 |> Multi.update_all(:ids, query, set: [seen: true, updated_at: NaiveDateTime.utc_now()])
288 |> Marker.multi_set_last_read_id(user, "notifications")
289 |> Repo.transaction()
292 |> where([n], n.id in ^notification_ids)
296 @spec read_one(User.t(), String.t()) ::
297 {:ok, Notification.t()} | {:error, Ecto.Changeset.t()} | nil
298 def read_one(%User{} = user, notification_id) do
299 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
301 |> Multi.update(:update, changeset(notification, %{seen: true}))
302 |> Marker.multi_set_last_read_id(user, "notifications")
303 |> Repo.transaction()
305 {:ok, %{update: notification}} -> {:ok, notification}
306 {:error, :update, changeset, _} -> {:error, changeset}
311 def get(%{id: user_id} = _user, id) do
316 join: activity in assoc(n, :activity),
317 preload: [activity: activity]
320 notification = Repo.one(query)
323 %{user_id: ^user_id} ->
327 {:error, "Cannot get notification"}
332 from(n in Notification, where: n.user_id == ^user.id)
336 def destroy_multiple(%{id: user_id} = _user, ids) do
337 from(n in Notification,
339 where: n.user_id == ^user_id
344 def dismiss(%Pleroma.Activity{} = activity) do
346 |> where([n], n.activity_id == ^activity.id)
349 {_, notifications} -> {:ok, notifications}
350 _ -> {:error, "Cannot dismiss notification"}
354 def dismiss(%{id: user_id} = _user, id) do
355 notification = Repo.get(Notification, id)
358 %{user_id: ^user_id} ->
359 Repo.delete(notification)
362 {:error, "Cannot dismiss notification"}
366 @spec create_notifications(Activity.t(), keyword()) :: {:ok, [Notification.t()] | []}
367 def create_notifications(activity, options \\ [])
369 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity, options) do
370 object = Object.normalize(activity, fetch: false)
372 if object && object.data["type"] == "Answer" do
375 do_create_notifications(activity, options)
379 def create_notifications(%Activity{data: %{"type" => type}} = activity, options)
380 when type in ["Follow", "Like", "Announce", "Move", "EmojiReact", "Flag"] do
381 do_create_notifications(activity, options)
384 def create_notifications(_, _), do: {:ok, []}
386 defp do_create_notifications(%Activity{} = activity, options) do
387 do_send = Keyword.get(options, :do_send, true)
389 {enabled_receivers, disabled_receivers} = get_notified_from_activity(activity)
390 potential_receivers = enabled_receivers ++ disabled_receivers
393 Enum.map(potential_receivers, fn user ->
394 do_send = do_send && user in enabled_receivers
395 create_notification(activity, user, do_send: do_send)
397 |> Enum.reject(&is_nil/1)
402 defp type_from_activity(%{data: %{"type" => type}} = activity) do
405 if Activity.follow_accepted?(activity) do
421 "pleroma:emoji_reaction"
426 # Compatibility with old reactions
428 "pleroma:emoji_reaction"
432 |> type_from_activity_object()
435 raise "No notification type for activity type #{t}"
439 defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention"
441 defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do
442 object = Object.get_by_ap_id(activity.data["object"])
444 case object && object.data["type"] do
445 "ChatMessage" -> "pleroma:chat_mention"
450 # TODO move to sql, too.
451 def create_notification(%Activity{} = activity, %User{} = user, opts \\ []) do
452 do_send = Keyword.get(opts, :do_send, true)
453 type = Keyword.get(opts, :type, type_from_activity(activity))
455 unless skip?(activity, user, opts) do
456 {:ok, %{notification: notification}} =
458 |> Multi.insert(:notification, %Notification{
461 seen: mark_as_read?(activity, user),
464 |> Marker.multi_set_last_read_id(user, "notifications")
465 |> Repo.transaction()
468 Streamer.stream(["user", "user:notification"], notification)
469 Push.send(notification)
476 def create_poll_notifications(%Activity{} = activity) do
477 with %Object{data: %{"type" => "Question", "actor" => actor} = data} <-
478 Object.normalize(activity) do
481 %{"voters" => voters} when is_list(voters) -> voters
486 Enum.reduce([actor | voters], [], fn ap_id, acc ->
487 with %User{local: true} = user <- User.get_by_ap_id(ap_id) do
488 [create_notification(activity, user, type: "poll") | acc]
499 Returns a tuple with 2 elements:
500 {notification-enabled receivers, currently disabled receivers (blocking / [thread] muting)}
502 NOTE: might be called for FAKE Activities, see ActivityPub.Utils.get_notified_from_object/1
504 @spec get_notified_from_activity(Activity.t(), boolean()) :: {list(User.t()), list(User.t())}
505 def get_notified_from_activity(activity, local_only \\ true)
507 def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
508 when type in ["Create", "Like", "Announce", "Follow", "Move", "EmojiReact", "Flag"] do
509 potential_receiver_ap_ids = get_potential_receiver_ap_ids(activity)
511 potential_receivers =
512 User.get_users_from_set(potential_receiver_ap_ids, local_only: local_only)
514 notification_enabled_ap_ids =
515 potential_receiver_ap_ids
516 |> exclude_domain_blocker_ap_ids(activity, potential_receivers)
517 |> exclude_relationship_restricted_ap_ids(activity)
518 |> exclude_thread_muter_ap_ids(activity)
520 notification_enabled_users =
521 Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
523 {notification_enabled_users, potential_receivers -- notification_enabled_users}
526 def get_notified_from_activity(_, _local_only), do: {[], []}
528 # For some activities, only notify the author of the object
529 def get_potential_receiver_ap_ids(%{data: %{"type" => type, "object" => object_id}})
530 when type in ~w{Like Announce EmojiReact} do
531 case Object.get_cached_by_ap_id(object_id) do
532 %Object{data: %{"actor" => actor}} ->
540 def get_potential_receiver_ap_ids(%{data: %{"type" => "Follow", "object" => object_id}}) do
544 def get_potential_receiver_ap_ids(%{data: %{"type" => "Flag", "actor" => actor}}) do
545 (User.all_superusers() |> Enum.map(fn user -> user.ap_id end)) -- [actor]
548 def get_potential_receiver_ap_ids(activity) do
550 |> Utils.maybe_notify_to_recipients(activity)
551 |> Utils.maybe_notify_mentioned_recipients(activity)
552 |> Utils.maybe_notify_subscribers(activity)
553 |> Utils.maybe_notify_followers(activity)
557 @doc "Filters out AP IDs domain-blocking and not following the activity's actor"
558 def exclude_domain_blocker_ap_ids(ap_ids, activity, preloaded_users \\ [])
560 def exclude_domain_blocker_ap_ids([], _activity, _preloaded_users), do: []
562 def exclude_domain_blocker_ap_ids(ap_ids, %Activity{} = activity, preloaded_users) do
563 activity_actor_domain = activity.actor && URI.parse(activity.actor).host
567 |> Enum.map(fn ap_id ->
568 Enum.find(preloaded_users, &(&1.ap_id == ap_id)) ||
569 User.get_cached_by_ap_id(ap_id)
573 domain_blocker_ap_ids = for u <- users, activity_actor_domain in u.domain_blocks, do: u.ap_id
575 domain_blocker_follower_ap_ids =
576 if Enum.any?(domain_blocker_ap_ids) do
578 |> Activity.user_actor()
579 |> FollowingRelationship.followers_ap_ids(domain_blocker_ap_ids)
585 |> Kernel.--(domain_blocker_ap_ids)
586 |> Kernel.++(domain_blocker_follower_ap_ids)
589 @doc "Filters out AP IDs of users basing on their relationships with activity actor user"
590 def exclude_relationship_restricted_ap_ids([], _activity), do: []
592 def exclude_relationship_restricted_ap_ids(ap_ids, %Activity{} = activity) do
593 relationship_restricted_ap_ids =
595 |> Activity.user_actor()
596 |> User.incoming_relationships_ungrouped_ap_ids([
601 Enum.uniq(ap_ids) -- relationship_restricted_ap_ids
604 @doc "Filters out AP IDs of users who mute activity thread"
605 def exclude_thread_muter_ap_ids([], _activity), do: []
607 def exclude_thread_muter_ap_ids(ap_ids, %Activity{} = activity) do
608 thread_muter_ap_ids = ThreadMute.muter_ap_ids(activity.data["context"])
610 Enum.uniq(ap_ids) -- thread_muter_ap_ids
613 def skip?(activity, user, opts \\ [])
615 @spec skip?(Activity.t(), User.t(), Keyword.t()) :: boolean()
616 def skip?(%Activity{} = activity, %User{} = user, opts) do
620 :block_from_strangers,
624 |> Enum.find(&skip?(&1, activity, user, opts))
627 def skip?(_activity, _user, _opts), do: false
629 @spec skip?(atom(), Activity.t(), User.t(), Keyword.t()) :: boolean()
630 def skip?(:self, %Activity{} = activity, %User{} = user, opts) do
632 opts[:type] == "poll" -> false
633 activity.data["actor"] == user.ap_id -> true
638 def skip?(:invisible, %Activity{} = activity, _user, _opts) do
639 actor = activity.data["actor"]
640 user = User.get_cached_by_ap_id(actor)
641 User.invisible?(user)
645 :block_from_strangers,
646 %Activity{} = activity,
647 %User{notification_settings: %{block_from_strangers: true}} = user,
650 actor = activity.data["actor"]
651 follower = User.get_cached_by_ap_id(actor)
654 opts[:type] == "poll" -> false
655 user.ap_id == actor -> false
656 !User.following?(follower, user) -> true
661 # To do: consider defining recency in hours and checking FollowingRelationship with a single SQL
664 %Activity{data: %{"type" => "Follow"}} = activity,
668 actor = activity.data["actor"]
670 Notification.for_user(user)
672 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
677 def skip?(:filtered, %{data: %{"type" => type}}, _user, _opts) when type in ["Follow", "Move"],
680 def skip?(:filtered, activity, user, _opts) do
681 object = Object.normalize(activity, fetch: false)
687 object.data["actor"] == user.ap_id ->
690 not is_nil(regex = Pleroma.Filter.compose_regex(user, :re)) ->
691 Regex.match?(regex, object.data["content"])
698 def skip?(_type, _activity, _user, _opts), do: false
700 def mark_as_read?(activity, target_user) do
701 user = Activity.user_actor(activity)
702 User.mutes_user?(target_user, user) || CommonAPI.thread_muted?(target_user, activity)
705 def for_user_and_activity(user, activity) do
706 from(n in __MODULE__,
707 where: n.user_id == ^user.id,
708 where: n.activity_id == ^activity.id
713 @spec mark_context_as_read(User.t(), String.t()) :: {integer(), nil | [term()]}
714 def mark_context_as_read(%User{id: id}, context) do
717 join: a in assoc(n, :activity),
718 where: n.user_id == ^id,
719 where: n.seen == false,
720 where: fragment("?->>'context'", a.data) == ^context
722 |> Repo.update_all(set: [seen: true])