1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Notification do
9 alias Pleroma.Notification
11 alias Pleroma.Pagination
14 alias Pleroma.Web.CommonAPI.Utils
15 alias Pleroma.Web.Push
16 alias Pleroma.Web.Streamer
22 @type t :: %__MODULE__{}
24 schema "notifications" do
25 field(:seen, :boolean, default: false)
26 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
27 belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
32 def changeset(%Notification{} = notification, attrs) do
34 |> cast(attrs, [:seen])
37 def for_user_query(user, opts \\ []) do
39 |> where(user_id: ^user.id)
43 "? not in (SELECT ap_id FROM users WHERE deactivated = 'true')",
47 |> join(:inner, [n], activity in assoc(n, :activity))
48 |> join(:left, [n, a], object in Object,
51 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
56 |> preload([n, a, o], activity: {a, object: o})
57 |> exclude_muted(user, opts)
58 |> exclude_visibility(opts)
61 defp exclude_muted(query, _, %{with_muted: true}) do
65 defp exclude_muted(query, user, _opts) do
67 |> where([n, a], a.actor not in ^user.muted_notifications)
68 |> where([n, a], a.actor not in ^user.blocks)
71 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.domain_blocks
73 |> join(:left, [n, a], tm in Pleroma.ThreadMute,
74 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
76 |> where([n, a, o, tm], is_nil(tm.user_id))
79 @valid_visibilities ~w[direct unlisted public private]
81 defp exclude_visibility(query, %{exclude_visibilities: visibility})
82 when is_list(visibility) do
83 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
88 "activity_visibility(?, ?, ?) = ANY (?)",
96 Logger.error("Could not exclude visibility to #{visibility}")
101 defp exclude_visibility(query, %{exclude_visibilities: visibility})
102 when visibility in @valid_visibilities do
107 "activity_visibility(?, ?, ?) = (?)",
116 defp exclude_visibility(query, %{exclude_visibilities: visibility})
117 when visibility not in @valid_visibilities do
118 Logger.error("Could not exclude visibility to #{visibility}")
122 defp exclude_visibility(query, _visibility), do: query
124 def for_user(user, opts \\ %{}) do
126 |> for_user_query(opts)
127 |> Pagination.fetch_paginated(opts)
131 Returns notifications for user received since given date.
135 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
136 [%Pleroma.Notification{}, %Pleroma.Notification{}]
138 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
141 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
142 def for_user_since(user, date) do
143 from(n in for_user_query(user),
144 where: n.updated_at > ^date
149 def set_read_up_to(%{id: user_id} = _user, id) do
153 where: n.user_id == ^user_id,
155 where: n.seen == false,
159 updated_at: ^NaiveDateTime.utc_now()
162 # Ideally we would preload object and activities here
163 # but Ecto does not support preloads in update_all
167 {_, notification_ids} = Repo.update_all(query, [])
170 |> where([n], n.id in ^notification_ids)
171 |> join(:inner, [n], activity in assoc(n, :activity))
172 |> join(:left, [n, a], object in Object,
175 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
180 |> preload([n, a, o], activity: {a, object: o})
184 def read_one(%User{} = user, notification_id) do
185 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
187 |> changeset(%{seen: true})
192 def get(%{id: user_id} = _user, id) do
197 join: activity in assoc(n, :activity),
198 preload: [activity: activity]
201 notification = Repo.one(query)
204 %{user_id: ^user_id} ->
208 {:error, "Cannot get notification"}
213 from(n in Notification, where: n.user_id == ^user.id)
217 def destroy_multiple(%{id: user_id} = _user, ids) do
218 from(n in Notification,
220 where: n.user_id == ^user_id
225 def dismiss(%{id: user_id} = _user, id) do
226 notification = Repo.get(Notification, id)
229 %{user_id: ^user_id} ->
230 Repo.delete(notification)
233 {:error, "Cannot dismiss notification"}
237 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
238 object = Object.normalize(activity)
240 unless object && object.data["type"] == "Answer" do
241 users = get_notified_from_activity(activity)
242 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
249 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
250 when type in ["Like", "Announce", "Follow"] do
251 users = get_notified_from_activity(activity)
252 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
256 def create_notifications(_), do: {:ok, []}
258 # TODO move to sql, too.
259 def create_notification(%Activity{} = activity, %User{} = user) do
260 unless skip?(activity, user) do
261 notification = %Notification{user_id: user.id, activity: activity}
262 {:ok, notification} = Repo.insert(notification)
264 ["user", "user:notification"]
265 |> Streamer.stream(notification)
267 Push.send(notification)
272 def get_notified_from_activity(activity, local_only \\ true)
274 def get_notified_from_activity(
275 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
278 when type in ["Create", "Like", "Announce", "Follow"] do
281 |> Utils.maybe_notify_to_recipients(activity)
282 |> Utils.maybe_notify_mentioned_recipients(activity)
283 |> Utils.maybe_notify_subscribers(activity)
286 User.get_users_from_set(recipients, local_only)
289 def get_notified_from_activity(_, _local_only), do: []
291 @spec skip?(Activity.t(), User.t()) :: boolean()
292 def skip?(activity, user) do
301 |> Enum.any?(&skip?(&1, activity, user))
304 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
305 def skip?(:self, activity, user) do
306 activity.data["actor"] == user.ap_id
312 %{notification_settings: %{"followers" => false}} = user
314 actor = activity.data["actor"]
315 follower = User.get_cached_by_ap_id(actor)
316 User.following?(follower, user)
322 %{notification_settings: %{"non_followers" => false}} = user
324 actor = activity.data["actor"]
325 follower = User.get_cached_by_ap_id(actor)
326 !User.following?(follower, user)
329 def skip?(:follows, activity, %{notification_settings: %{"follows" => false}} = user) do
330 actor = activity.data["actor"]
331 followed = User.get_cached_by_ap_id(actor)
332 User.following?(user, followed)
338 %{notification_settings: %{"non_follows" => false}} = user
340 actor = activity.data["actor"]
341 followed = User.get_cached_by_ap_id(actor)
342 !User.following?(user, followed)
345 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
346 actor = activity.data["actor"]
348 Notification.for_user(user)
350 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
355 def skip?(_, _, _), do: false