1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Notification do
9 alias Pleroma.FollowingRelationship
11 alias Pleroma.Notification
13 alias Pleroma.Pagination
15 alias Pleroma.ThreadMute
17 alias Pleroma.Web.CommonAPI.Utils
18 alias Pleroma.Web.Push
19 alias Pleroma.Web.Streamer
26 @type t :: %__MODULE__{}
28 @include_muted_option :with_muted
30 schema "notifications" do
31 field(:seen, :boolean, default: false)
32 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
33 belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
38 def changeset(%Notification{} = notification, attrs) do
40 |> cast(attrs, [:seen])
43 defp for_user_query_ap_id_opts(user, opts) do
46 if opts[@include_muted_option], do: [], else: [:notification_mute]
48 preloaded_ap_ids = User.outgoing_relationships_ap_ids(user, ap_id_relationships)
50 exclude_blocked_opts = Map.merge(%{blocked_users_ap_ids: preloaded_ap_ids[:block]}, opts)
52 exclude_notification_muted_opts =
53 Map.merge(%{notification_muted_users_ap_ids: preloaded_ap_ids[:notification_mute]}, opts)
55 {exclude_blocked_opts, exclude_notification_muted_opts}
58 def for_user_query(user, opts \\ %{}) do
59 {exclude_blocked_opts, exclude_notification_muted_opts} =
60 for_user_query_ap_id_opts(user, opts)
63 |> where(user_id: ^user.id)
67 "? not in (SELECT ap_id FROM users WHERE deactivated = 'true')",
71 |> join(:inner, [n], activity in assoc(n, :activity))
72 |> join(:left, [n, a], object in Object,
75 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
80 |> preload([n, a, o], activity: {a, object: o})
81 |> exclude_notification_muted(user, exclude_notification_muted_opts)
82 |> exclude_blocked(user, exclude_blocked_opts)
83 |> exclude_visibility(opts)
86 # Excludes blocked users and non-followed domain-blocked users
87 defp exclude_blocked(query, user, opts) do
88 blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user)
91 |> where([n, a], a.actor not in ^blocked_ap_ids)
92 |> FollowingRelationship.keep_following_or_not_domain_blocked(user)
95 defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do
99 defp exclude_notification_muted(query, user, opts) do
100 notification_muted_ap_ids =
101 opts[:notification_muted_users_ap_ids] || User.notification_muted_users_ap_ids(user)
104 |> where([n, a], a.actor not in ^notification_muted_ap_ids)
105 |> join(:left, [n, a], tm in ThreadMute,
106 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
108 |> where([n, a, o, tm], is_nil(tm.user_id))
111 @valid_visibilities ~w[direct unlisted public private]
113 defp exclude_visibility(query, %{exclude_visibilities: visibility})
114 when is_list(visibility) do
115 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
117 |> join(:left, [n, a], mutated_activity in Pleroma.Activity,
119 fragment("?->>'context'", a.data) ==
120 fragment("?->>'context'", mutated_activity.data) and
121 fragment("(?->>'type' = 'Like' or ?->>'type' = 'Announce')", a.data, a.data) and
122 fragment("?->>'type'", mutated_activity.data) == "Create",
123 as: :mutated_activity
126 [n, a, mutated_activity: mutated_activity],
129 CASE WHEN (?->>'type') = 'Like' or (?->>'type') = 'Announce'
130 THEN (activity_visibility(?, ?, ?) = ANY (?))
131 ELSE (activity_visibility(?, ?, ?) = ANY (?)) END
135 mutated_activity.actor,
136 mutated_activity.recipients,
137 mutated_activity.data,
146 Logger.error("Could not exclude visibility to #{visibility}")
151 defp exclude_visibility(query, %{exclude_visibilities: visibility})
152 when visibility in @valid_visibilities do
153 exclude_visibility(query, [visibility])
156 defp exclude_visibility(query, %{exclude_visibilities: visibility})
157 when visibility not in @valid_visibilities do
158 Logger.error("Could not exclude visibility to #{visibility}")
162 defp exclude_visibility(query, _visibility), do: query
164 def for_user(user, opts \\ %{}) do
166 |> for_user_query(opts)
167 |> Pagination.fetch_paginated(opts)
171 Returns notifications for user received since given date.
175 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
176 [%Pleroma.Notification{}, %Pleroma.Notification{}]
178 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
181 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
182 def for_user_since(user, date) do
183 from(n in for_user_query(user),
184 where: n.updated_at > ^date
189 def set_read_up_to(%{id: user_id} = _user, id) do
193 where: n.user_id == ^user_id,
195 where: n.seen == false,
199 updated_at: ^NaiveDateTime.utc_now()
202 # Ideally we would preload object and activities here
203 # but Ecto does not support preloads in update_all
207 {_, notification_ids} = Repo.update_all(query, [])
210 |> where([n], n.id in ^notification_ids)
211 |> join(:inner, [n], activity in assoc(n, :activity))
212 |> join(:left, [n, a], object in Object,
215 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
220 |> preload([n, a, o], activity: {a, object: o})
224 def read_one(%User{} = user, notification_id) do
225 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
227 |> changeset(%{seen: true})
232 def get(%{id: user_id} = _user, id) do
237 join: activity in assoc(n, :activity),
238 preload: [activity: activity]
241 notification = Repo.one(query)
244 %{user_id: ^user_id} ->
248 {:error, "Cannot get notification"}
253 from(n in Notification, where: n.user_id == ^user.id)
257 def destroy_multiple(%{id: user_id} = _user, ids) do
258 from(n in Notification,
260 where: n.user_id == ^user_id
265 def dismiss(%{id: user_id} = _user, id) do
266 notification = Repo.get(Notification, id)
269 %{user_id: ^user_id} ->
270 Repo.delete(notification)
273 {:error, "Cannot dismiss notification"}
277 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
278 object = Object.normalize(activity)
280 if object && object.data["type"] == "Answer" do
283 do_create_notifications(activity)
287 def create_notifications(%Activity{data: %{"type" => type}} = activity)
288 when type in ["Like", "Announce", "Follow", "Move", "EmojiReact"] do
289 do_create_notifications(activity)
292 def create_notifications(_), do: {:ok, []}
294 defp do_create_notifications(%Activity{} = activity) do
295 {enabled_receivers, disabled_receivers} = get_notified_from_activity(activity)
296 potential_receivers = enabled_receivers ++ disabled_receivers
299 Enum.map(potential_receivers, fn user ->
300 do_send = user in enabled_receivers
301 create_notification(activity, user, do_send)
307 # TODO move to sql, too.
308 def create_notification(%Activity{} = activity, %User{} = user, do_send \\ true) do
309 unless skip?(activity, user) do
310 notification = %Notification{user_id: user.id, activity: activity}
311 {:ok, notification} = Repo.insert(notification)
314 Streamer.stream(["user", "user:notification"], notification)
315 Push.send(notification)
323 Returns a tuple with 2 elements:
324 {notification-enabled receivers, currently disabled receivers (blocking / [thread] muting)}
326 NOTE: might be called for FAKE Activities, see ActivityPub.Utils.get_notified_from_object/1
328 @spec get_notified_from_activity(Activity.t(), boolean()) :: {list(User.t()), list(User.t())}
329 def get_notified_from_activity(activity, local_only \\ true)
331 def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
332 when type in ["Create", "Like", "Announce", "Follow", "Move", "EmojiReact"] do
333 potential_receiver_ap_ids =
335 |> Utils.maybe_notify_to_recipients(activity)
336 |> Utils.maybe_notify_mentioned_recipients(activity)
337 |> Utils.maybe_notify_subscribers(activity)
338 |> Utils.maybe_notify_followers(activity)
341 potential_receivers = User.get_users_from_set(potential_receiver_ap_ids, local_only)
343 notification_enabled_ap_ids =
344 potential_receiver_ap_ids
345 |> exclude_domain_blocker_ap_ids(activity, potential_receivers)
346 |> exclude_relationship_restricted_ap_ids(activity)
347 |> exclude_thread_muter_ap_ids(activity)
349 notification_enabled_users =
350 Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
352 {notification_enabled_users, potential_receivers -- notification_enabled_users}
355 def get_notified_from_activity(_, _local_only), do: {[], []}
357 @doc "Filters out AP IDs of users who domain-block and not follow activity actor"
358 def exclude_domain_blocker_ap_ids(ap_ids, activity, preloaded_users \\ [])
360 def exclude_domain_blocker_ap_ids([], _activity, _preloaded_users), do: []
362 def exclude_domain_blocker_ap_ids(ap_ids, %Activity{} = activity, preloaded_users) do
363 activity_actor_domain = activity.actor && URI.parse(activity.actor).host
367 |> Enum.map(fn ap_id ->
368 Enum.find(preloaded_users, &(&1.ap_id == ap_id)) ||
369 User.get_cached_by_ap_id(ap_id)
373 domain_blocker_ap_ids = for u <- users, activity_actor_domain in u.domain_blocks, do: u.ap_id
375 domain_blocker_follower_ap_ids =
376 if Enum.any?(domain_blocker_ap_ids) do
378 |> Activity.user_actor()
379 |> FollowingRelationship.followers_ap_ids(domain_blocker_ap_ids)
385 |> Kernel.--(domain_blocker_ap_ids)
386 |> Kernel.++(domain_blocker_follower_ap_ids)
389 @doc "Filters out AP IDs of users basing on their relationships with activity actor user"
390 def exclude_relationship_restricted_ap_ids([], _activity), do: []
392 def exclude_relationship_restricted_ap_ids(ap_ids, %Activity{} = activity) do
393 relationship_restricted_ap_ids =
395 |> Activity.user_actor()
396 |> User.incoming_relationships_ungrouped_ap_ids([
401 Enum.uniq(ap_ids) -- relationship_restricted_ap_ids
404 @doc "Filters out AP IDs of users who mute activity thread"
405 def exclude_thread_muter_ap_ids([], _activity), do: []
407 def exclude_thread_muter_ap_ids(ap_ids, %Activity{} = activity) do
408 thread_muter_ap_ids = ThreadMute.muter_ap_ids(activity.data["context"])
410 Enum.uniq(ap_ids) -- thread_muter_ap_ids
413 @spec skip?(Activity.t(), User.t()) :: boolean()
414 def skip?(%Activity{} = activity, %User{} = user) do
423 |> Enum.find(&skip?(&1, activity, user))
426 def skip?(_, _), do: false
428 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
429 def skip?(:self, %Activity{} = activity, %User{} = user) do
430 activity.data["actor"] == user.ap_id
435 %Activity{} = activity,
436 %User{notification_settings: %{followers: false}} = user
438 actor = activity.data["actor"]
439 follower = User.get_cached_by_ap_id(actor)
440 User.following?(follower, user)
445 %Activity{} = activity,
446 %User{notification_settings: %{non_followers: false}} = user
448 actor = activity.data["actor"]
449 follower = User.get_cached_by_ap_id(actor)
450 !User.following?(follower, user)
455 %Activity{} = activity,
456 %User{notification_settings: %{follows: false}} = user
458 actor = activity.data["actor"]
459 followed = User.get_cached_by_ap_id(actor)
460 User.following?(user, followed)
465 %Activity{} = activity,
466 %User{notification_settings: %{non_follows: false}} = user
468 actor = activity.data["actor"]
469 followed = User.get_cached_by_ap_id(actor)
470 !User.following?(user, followed)
473 # To do: consider defining recency in hours and checking FollowingRelationship with a single SQL
474 def skip?(:recently_followed, %Activity{data: %{"type" => "Follow"}} = activity, %User{} = user) do
475 actor = activity.data["actor"]
477 Notification.for_user(user)
479 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
484 def skip?(_, _, _), do: false