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_blocked(user)
59 |> exclude_visibility(opts)
62 defp exclude_blocked(query, user) do
63 blocked_ap_ids = User.blocked_ap_ids(user)
66 |> where([n, a], a.actor not in ^blocked_ap_ids)
69 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.domain_blocks
73 defp exclude_muted(query, _, %{with_muted: true}) do
77 defp exclude_muted(query, user, _opts) do
79 |> where([n, a], a.actor not in ^user.muted_notifications)
80 |> join(:left, [n, a], tm in Pleroma.ThreadMute,
81 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
83 |> where([n, a, o, tm], is_nil(tm.user_id))
86 @valid_visibilities ~w[direct unlisted public private]
88 defp exclude_visibility(query, %{exclude_visibilities: visibility})
89 when is_list(visibility) do
90 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
95 "activity_visibility(?, ?, ?) = ANY (?)",
103 Logger.error("Could not exclude visibility to #{visibility}")
108 defp exclude_visibility(query, %{exclude_visibilities: visibility})
109 when visibility in @valid_visibilities do
114 "activity_visibility(?, ?, ?) = (?)",
123 defp exclude_visibility(query, %{exclude_visibilities: visibility})
124 when visibility not in @valid_visibilities do
125 Logger.error("Could not exclude visibility to #{visibility}")
129 defp exclude_visibility(query, _visibility), do: query
131 def for_user(user, opts \\ %{}) do
133 |> for_user_query(opts)
134 |> Pagination.fetch_paginated(opts)
138 Returns notifications for user received since given date.
142 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
143 [%Pleroma.Notification{}, %Pleroma.Notification{}]
145 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
148 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
149 def for_user_since(user, date) do
150 from(n in for_user_query(user),
151 where: n.updated_at > ^date
156 def set_read_up_to(%{id: user_id} = _user, id) do
160 where: n.user_id == ^user_id,
162 where: n.seen == false,
166 updated_at: ^NaiveDateTime.utc_now()
169 # Ideally we would preload object and activities here
170 # but Ecto does not support preloads in update_all
174 {_, notification_ids} = Repo.update_all(query, [])
177 |> where([n], n.id in ^notification_ids)
178 |> join(:inner, [n], activity in assoc(n, :activity))
179 |> join(:left, [n, a], object in Object,
182 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
187 |> preload([n, a, o], activity: {a, object: o})
191 def read_one(%User{} = user, notification_id) do
192 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
194 |> changeset(%{seen: true})
199 def get(%{id: user_id} = _user, id) do
204 join: activity in assoc(n, :activity),
205 preload: [activity: activity]
208 notification = Repo.one(query)
211 %{user_id: ^user_id} ->
215 {:error, "Cannot get notification"}
220 from(n in Notification, where: n.user_id == ^user.id)
224 def destroy_multiple(%{id: user_id} = _user, ids) do
225 from(n in Notification,
227 where: n.user_id == ^user_id
232 def dismiss(%{id: user_id} = _user, id) do
233 notification = Repo.get(Notification, id)
236 %{user_id: ^user_id} ->
237 Repo.delete(notification)
240 {:error, "Cannot dismiss notification"}
244 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
245 object = Object.normalize(activity)
247 unless object && object.data["type"] == "Answer" do
248 users = get_notified_from_activity(activity)
249 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
256 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
257 when type in ["Like", "Announce", "Follow"] do
258 users = get_notified_from_activity(activity)
259 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
263 def create_notifications(_), do: {:ok, []}
265 # TODO move to sql, too.
266 def create_notification(%Activity{} = activity, %User{} = user) do
267 unless skip?(activity, user) do
268 notification = %Notification{user_id: user.id, activity: activity}
269 {:ok, notification} = Repo.insert(notification)
271 ["user", "user:notification"]
272 |> Streamer.stream(notification)
274 Push.send(notification)
279 def get_notified_from_activity(activity, local_only \\ true)
281 def get_notified_from_activity(
282 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
285 when type in ["Create", "Like", "Announce", "Follow"] do
288 |> Utils.maybe_notify_to_recipients(activity)
289 |> Utils.maybe_notify_mentioned_recipients(activity)
290 |> Utils.maybe_notify_subscribers(activity)
293 User.get_users_from_set(recipients, local_only)
296 def get_notified_from_activity(_, _local_only), do: []
298 @spec skip?(Activity.t(), User.t()) :: boolean()
299 def skip?(activity, user) do
308 |> Enum.any?(&skip?(&1, activity, user))
311 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
312 def skip?(:self, activity, user) do
313 activity.data["actor"] == user.ap_id
319 %{notification_settings: %{"followers" => false}} = user
321 actor = activity.data["actor"]
322 follower = User.get_cached_by_ap_id(actor)
323 User.following?(follower, user)
329 %{notification_settings: %{"non_followers" => false}} = user
331 actor = activity.data["actor"]
332 follower = User.get_cached_by_ap_id(actor)
333 !User.following?(follower, user)
336 def skip?(:follows, activity, %{notification_settings: %{"follows" => false}} = user) do
337 actor = activity.data["actor"]
338 followed = User.get_cached_by_ap_id(actor)
339 User.following?(user, followed)
345 %{notification_settings: %{"non_follows" => false}} = user
347 actor = activity.data["actor"]
348 followed = User.get_cached_by_ap_id(actor)
349 !User.following?(user, followed)
352 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
353 actor = activity.data["actor"]
355 Notification.for_user(user)
357 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
362 def skip?(_, _, _), do: false