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
64 |> where([n, a], a.actor not in ^user.blocks)
67 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.domain_blocks
71 defp exclude_muted(query, _, %{with_muted: true}) do
75 defp exclude_muted(query, user, _opts) do
77 |> where([n, a], a.actor not in ^user.muted_notifications)
78 |> join(:left, [n, a], tm in Pleroma.ThreadMute,
79 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
81 |> where([n, a, o, tm], is_nil(tm.user_id))
84 @valid_visibilities ~w[direct unlisted public private]
86 defp exclude_visibility(query, %{exclude_visibilities: visibility})
87 when is_list(visibility) do
88 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
93 "activity_visibility(?, ?, ?) = ANY (?)",
101 Logger.error("Could not exclude visibility to #{visibility}")
106 defp exclude_visibility(query, %{exclude_visibilities: visibility})
107 when visibility in @valid_visibilities do
112 "activity_visibility(?, ?, ?) = (?)",
121 defp exclude_visibility(query, %{exclude_visibilities: visibility})
122 when visibility not in @valid_visibilities do
123 Logger.error("Could not exclude visibility to #{visibility}")
127 defp exclude_visibility(query, _visibility), do: query
129 def for_user(user, opts \\ %{}) do
131 |> for_user_query(opts)
132 |> Pagination.fetch_paginated(opts)
136 Returns notifications for user received since given date.
140 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
141 [%Pleroma.Notification{}, %Pleroma.Notification{}]
143 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
146 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
147 def for_user_since(user, date) do
148 from(n in for_user_query(user),
149 where: n.updated_at > ^date
154 def set_read_up_to(%{id: user_id} = _user, id) do
158 where: n.user_id == ^user_id,
160 where: n.seen == false,
164 updated_at: ^NaiveDateTime.utc_now()
167 # Ideally we would preload object and activities here
168 # but Ecto does not support preloads in update_all
172 {_, notification_ids} = Repo.update_all(query, [])
175 |> where([n], n.id in ^notification_ids)
176 |> join(:inner, [n], activity in assoc(n, :activity))
177 |> join(:left, [n, a], object in Object,
180 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
185 |> preload([n, a, o], activity: {a, object: o})
189 def read_one(%User{} = user, notification_id) do
190 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
192 |> changeset(%{seen: true})
197 def get(%{id: user_id} = _user, id) do
202 join: activity in assoc(n, :activity),
203 preload: [activity: activity]
206 notification = Repo.one(query)
209 %{user_id: ^user_id} ->
213 {:error, "Cannot get notification"}
218 from(n in Notification, where: n.user_id == ^user.id)
222 def destroy_multiple(%{id: user_id} = _user, ids) do
223 from(n in Notification,
225 where: n.user_id == ^user_id
230 def dismiss(%{id: user_id} = _user, id) do
231 notification = Repo.get(Notification, id)
234 %{user_id: ^user_id} ->
235 Repo.delete(notification)
238 {:error, "Cannot dismiss notification"}
242 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
243 object = Object.normalize(activity)
245 unless object && object.data["type"] == "Answer" do
246 users = get_notified_from_activity(activity)
247 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
254 def create_notifications(%Activity{data: %{"type" => type}} = activity)
255 when type in ["Like", "Announce", "Follow", "Move"] do
258 |> get_notified_from_activity()
259 |> Enum.map(&create_notification(activity, &1))
264 def create_notifications(_), do: {:ok, []}
266 # TODO move to sql, too.
267 def create_notification(%Activity{} = activity, %User{} = user) do
268 unless skip?(activity, user) do
269 notification = %Notification{user_id: user.id, activity: activity}
270 {:ok, notification} = Repo.insert(notification)
272 ["user", "user:notification"]
273 |> Streamer.stream(notification)
275 Push.send(notification)
280 def get_notified_from_activity(activity, local_only \\ true)
282 def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
283 when type in ["Create", "Like", "Announce", "Follow", "Move"] do
285 |> Utils.maybe_notify_to_recipients(activity)
286 |> Utils.maybe_notify_mentioned_recipients(activity)
287 |> Utils.maybe_notify_subscribers(activity)
288 |> Utils.maybe_notify_followers(activity)
290 |> User.get_users_from_set(local_only)
293 def get_notified_from_activity(_, _local_only), do: []
295 @spec skip?(Activity.t(), User.t()) :: boolean()
296 def skip?(activity, user) do
305 |> Enum.any?(&skip?(&1, activity, user))
308 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
309 def skip?(:self, activity, user) do
310 activity.data["actor"] == user.ap_id
316 %{notification_settings: %{"followers" => false}} = user
318 actor = activity.data["actor"]
319 follower = User.get_cached_by_ap_id(actor)
320 User.following?(follower, user)
326 %{notification_settings: %{"non_followers" => false}} = user
328 actor = activity.data["actor"]
329 follower = User.get_cached_by_ap_id(actor)
330 !User.following?(follower, user)
333 def skip?(:follows, activity, %{notification_settings: %{"follows" => false}} = user) do
334 actor = activity.data["actor"]
335 followed = User.get_cached_by_ap_id(actor)
336 User.following?(user, followed)
342 %{notification_settings: %{"non_follows" => false}} = user
344 actor = activity.data["actor"]
345 followed = User.get_cached_by_ap_id(actor)
346 !User.following?(user, followed)
349 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
350 actor = activity.data["actor"]
352 Notification.for_user(user)
354 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
359 def skip?(_, _, _), do: false