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
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 @spec last_read_query(User.t()) :: Ecto.Queryable.t()
44 def last_read_query(user) do
45 from(q in Pleroma.Notification,
46 where: q.user_id == ^user.id,
47 where: q.seen == true,
48 select: type(q.id, :string),
54 defp for_user_query_ap_id_opts(user, opts) do
57 if opts[@include_muted_option], do: [], else: [:notification_mute]
59 preloaded_ap_ids = User.outgoing_relationships_ap_ids(user, ap_id_relationships)
61 exclude_blocked_opts = Map.merge(%{blocked_users_ap_ids: preloaded_ap_ids[:block]}, opts)
63 exclude_notification_muted_opts =
64 Map.merge(%{notification_muted_users_ap_ids: preloaded_ap_ids[:notification_mute]}, opts)
66 {exclude_blocked_opts, exclude_notification_muted_opts}
69 def for_user_query(user, opts \\ %{}) do
70 {exclude_blocked_opts, exclude_notification_muted_opts} =
71 for_user_query_ap_id_opts(user, opts)
74 |> where(user_id: ^user.id)
78 "? not in (SELECT ap_id FROM users WHERE deactivated = 'true')",
82 |> join(:inner, [n], activity in assoc(n, :activity))
83 |> join(:left, [n, a], object in Object,
86 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
91 |> preload([n, a, o], activity: {a, object: o})
92 |> exclude_notification_muted(user, exclude_notification_muted_opts)
93 |> exclude_blocked(user, exclude_blocked_opts)
94 |> exclude_visibility(opts)
97 defp exclude_blocked(query, user, opts) do
98 blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user)
101 |> where([n, a], a.actor not in ^blocked_ap_ids)
104 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.domain_blocks
108 defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do
112 defp exclude_notification_muted(query, user, opts) do
113 notification_muted_ap_ids =
114 opts[:notification_muted_users_ap_ids] || User.notification_muted_users_ap_ids(user)
117 |> where([n, a], a.actor not in ^notification_muted_ap_ids)
118 |> join(:left, [n, a], tm in ThreadMute,
119 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
121 |> where([n, a, o, tm], is_nil(tm.user_id))
124 @valid_visibilities ~w[direct unlisted public private]
126 defp exclude_visibility(query, %{exclude_visibilities: visibility})
127 when is_list(visibility) do
128 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
130 |> join(:left, [n, a], mutated_activity in Pleroma.Activity,
132 fragment("?->>'context'", a.data) ==
133 fragment("?->>'context'", mutated_activity.data) and
134 fragment("(?->>'type' = 'Like' or ?->>'type' = 'Announce')", a.data, a.data) and
135 fragment("?->>'type'", mutated_activity.data) == "Create",
136 as: :mutated_activity
139 [n, a, mutated_activity: mutated_activity],
142 CASE WHEN (?->>'type') = 'Like' or (?->>'type') = 'Announce'
143 THEN (activity_visibility(?, ?, ?) = ANY (?))
144 ELSE (activity_visibility(?, ?, ?) = ANY (?)) END
148 mutated_activity.actor,
149 mutated_activity.recipients,
150 mutated_activity.data,
159 Logger.error("Could not exclude visibility to #{visibility}")
164 defp exclude_visibility(query, %{exclude_visibilities: visibility})
165 when visibility in @valid_visibilities do
166 exclude_visibility(query, [visibility])
169 defp exclude_visibility(query, %{exclude_visibilities: visibility})
170 when visibility not in @valid_visibilities do
171 Logger.error("Could not exclude visibility to #{visibility}")
175 defp exclude_visibility(query, _visibility), do: query
177 def for_user(user, opts \\ %{}) do
179 |> for_user_query(opts)
180 |> Pagination.fetch_paginated(opts)
184 Returns notifications for user received since given date.
188 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
189 [%Pleroma.Notification{}, %Pleroma.Notification{}]
191 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
194 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
195 def for_user_since(user, date) do
196 from(n in for_user_query(user),
197 where: n.updated_at > ^date
202 def set_read_up_to(%{id: user_id} = user, id) do
206 where: n.user_id == ^user_id,
208 where: n.seen == false,
209 # Ideally we would preload object and activities here
210 # but Ecto does not support preloads in update_all
214 {:ok, %{ids: {_, notification_ids}}} =
216 |> Multi.update_all(:ids, query, set: [seen: true, updated_at: NaiveDateTime.utc_now()])
217 |> Marker.multi_set_last_read_id(user, "notifications")
218 |> Repo.transaction()
221 |> where([n], n.id in ^notification_ids)
222 |> join(:inner, [n], activity in assoc(n, :activity))
223 |> join(:left, [n, a], object in Object,
226 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
231 |> preload([n, a, o], activity: {a, object: o})
235 @spec read_one(User.t(), String.t()) ::
236 {:ok, Notification.t()} | {:error, Ecto.Changeset.t()} | nil
237 def read_one(%User{} = user, notification_id) do
238 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
240 |> Multi.update(:update, changeset(notification, %{seen: true}))
241 |> Marker.multi_set_last_read_id(user, "notifications")
242 |> Repo.transaction()
244 {:ok, %{update: notification}} -> {:ok, notification}
245 {:error, :update, changeset, _} -> {:error, changeset}
250 def get(%{id: user_id} = _user, id) do
255 join: activity in assoc(n, :activity),
256 preload: [activity: activity]
259 notification = Repo.one(query)
262 %{user_id: ^user_id} ->
266 {:error, "Cannot get notification"}
271 from(n in Notification, where: n.user_id == ^user.id)
275 def destroy_multiple(%{id: user_id} = _user, ids) do
276 from(n in Notification,
278 where: n.user_id == ^user_id
283 def dismiss(%{id: user_id} = _user, id) do
284 notification = Repo.get(Notification, id)
287 %{user_id: ^user_id} ->
288 Repo.delete(notification)
291 {:error, "Cannot dismiss notification"}
295 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
296 object = Object.normalize(activity)
298 if object && object.data["type"] == "Answer" do
301 do_create_notifications(activity)
305 def create_notifications(%Activity{data: %{"type" => type}} = activity)
306 when type in ["Like", "Announce", "Follow", "Move", "EmojiReact"] do
307 do_create_notifications(activity)
310 def create_notifications(_), do: {:ok, []}
312 defp do_create_notifications(%Activity{} = activity) do
313 {enabled_receivers, disabled_receivers} = get_notified_from_activity(activity)
314 potential_receivers = enabled_receivers ++ disabled_receivers
317 Enum.map(potential_receivers, fn user ->
318 do_send = user in enabled_receivers
319 create_notification(activity, user, do_send)
325 # TODO move to sql, too.
326 def create_notification(%Activity{} = activity, %User{} = user, do_send \\ true) do
327 unless skip?(activity, user) do
328 {:ok, %{notification: notification}} =
330 |> Multi.insert(:notification, %Notification{user_id: user.id, activity: activity})
331 |> Marker.multi_set_last_read_id(user, "notifications")
332 |> Repo.transaction()
335 Streamer.stream(["user", "user:notification"], notification)
336 Push.send(notification)
344 Returns a tuple with 2 elements:
345 {enabled notification receivers, currently disabled receivers (blocking / [thread] muting)}
347 NOTE: might be called for FAKE Activities, see ActivityPub.Utils.get_notified_from_object/1
349 def get_notified_from_activity(activity, local_only \\ true)
351 def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
352 when type in ["Create", "Like", "Announce", "Follow", "Move", "EmojiReact"] do
353 potential_receiver_ap_ids =
355 |> Utils.maybe_notify_to_recipients(activity)
356 |> Utils.maybe_notify_mentioned_recipients(activity)
357 |> Utils.maybe_notify_subscribers(activity)
358 |> Utils.maybe_notify_followers(activity)
361 # Since even subscribers and followers can mute / thread-mute, filtering all above AP IDs
362 notification_enabled_ap_ids =
363 potential_receiver_ap_ids
364 |> exclude_relationship_restricted_ap_ids(activity)
365 |> exclude_thread_muter_ap_ids(activity)
367 potential_receivers =
368 potential_receiver_ap_ids
370 |> User.get_users_from_set(local_only)
372 notification_enabled_users =
373 Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
375 {notification_enabled_users, potential_receivers -- notification_enabled_users}
378 def get_notified_from_activity(_, _local_only), do: {[], []}
380 @doc "Filters out AP IDs of users basing on their relationships with activity actor user"
381 def exclude_relationship_restricted_ap_ids([], _activity), do: []
383 def exclude_relationship_restricted_ap_ids(ap_ids, %Activity{} = activity) do
384 relationship_restricted_ap_ids =
386 |> Activity.user_actor()
387 |> User.incoming_relationships_ungrouped_ap_ids([
392 Enum.uniq(ap_ids) -- relationship_restricted_ap_ids
395 @doc "Filters out AP IDs of users who mute activity thread"
396 def exclude_thread_muter_ap_ids([], _activity), do: []
398 def exclude_thread_muter_ap_ids(ap_ids, %Activity{} = activity) do
399 thread_muter_ap_ids = ThreadMute.muter_ap_ids(activity.data["context"])
401 Enum.uniq(ap_ids) -- thread_muter_ap_ids
404 @spec skip?(Activity.t(), User.t()) :: boolean()
405 def skip?(%Activity{} = activity, %User{} = user) do
414 |> Enum.find(&skip?(&1, activity, user))
417 def skip?(_, _), do: false
419 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
420 def skip?(:self, %Activity{} = activity, %User{} = user) do
421 activity.data["actor"] == user.ap_id
426 %Activity{} = activity,
427 %User{notification_settings: %{followers: false}} = user
429 actor = activity.data["actor"]
430 follower = User.get_cached_by_ap_id(actor)
431 User.following?(follower, user)
436 %Activity{} = activity,
437 %User{notification_settings: %{non_followers: false}} = user
439 actor = activity.data["actor"]
440 follower = User.get_cached_by_ap_id(actor)
441 !User.following?(follower, user)
446 %Activity{} = activity,
447 %User{notification_settings: %{follows: false}} = user
449 actor = activity.data["actor"]
450 followed = User.get_cached_by_ap_id(actor)
451 User.following?(user, followed)
456 %Activity{} = activity,
457 %User{notification_settings: %{non_follows: false}} = user
459 actor = activity.data["actor"]
460 followed = User.get_cached_by_ap_id(actor)
461 !User.following?(user, followed)
464 # To do: consider defining recency in hours and checking FollowingRelationship with a single SQL
465 def skip?(:recently_followed, %Activity{data: %{"type" => "Follow"}} = activity, %User{} = user) do
466 actor = activity.data["actor"]
468 Notification.for_user(user)
470 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
475 def skip?(_, _, _), do: false