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
21 @type t :: %__MODULE__{}
23 schema "notifications" do
24 field(:seen, :boolean, default: false)
25 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
26 belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
31 def changeset(%Notification{} = notification, attrs) do
33 |> cast(attrs, [:seen])
36 def for_user_query(user, opts \\ []) do
38 |> where(user_id: ^user.id)
42 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
46 |> join(:inner, [n], activity in assoc(n, :activity))
47 |> join(:left, [n, a], object in Object,
50 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
55 |> preload([n, a, o], activity: {a, object: o})
56 |> exclude_muted(user, opts)
57 |> exclude_blocked(user)
60 defp exclude_blocked(query, user) do
62 |> where([n, a], a.actor not in ^user.info.blocks)
65 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.info.domain_blocks
69 defp exclude_muted(query, _, %{with_muted: true}) do
73 defp exclude_muted(query, user, _opts) do
75 |> where([n, a], a.actor not in ^user.info.muted_notifications)
76 |> join(:left, [n, a], tm in Pleroma.ThreadMute,
77 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
79 |> where([n, a, o, tm], is_nil(tm.user_id))
82 def for_user(user, opts \\ %{}) do
84 |> for_user_query(opts)
85 |> Pagination.fetch_paginated(opts)
89 Returns notifications for user received since given date.
93 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
94 [%Pleroma.Notification{}, %Pleroma.Notification{}]
96 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
99 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
100 def for_user_since(user, date) do
101 from(n in for_user_query(user),
102 where: n.updated_at > ^date
107 def set_read_up_to(%{id: user_id} = _user, id) do
111 where: n.user_id == ^user_id,
113 where: n.seen == false,
117 updated_at: ^NaiveDateTime.utc_now()
120 # Ideally we would preload object and activities here
121 # but Ecto does not support preloads in update_all
125 {_, notification_ids} = Repo.update_all(query, [])
128 |> where([n], n.id in ^notification_ids)
129 |> join(:inner, [n], activity in assoc(n, :activity))
130 |> join(:left, [n, a], object in Object,
133 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
138 |> preload([n, a, o], activity: {a, object: o})
142 def read_one(%User{} = user, notification_id) do
143 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
145 |> changeset(%{seen: true})
150 def get(%{id: user_id} = _user, id) do
155 join: activity in assoc(n, :activity),
156 preload: [activity: activity]
159 notification = Repo.one(query)
162 %{user_id: ^user_id} ->
166 {:error, "Cannot get notification"}
171 from(n in Notification, where: n.user_id == ^user.id)
175 def destroy_multiple(%{id: user_id} = _user, ids) do
176 from(n in Notification,
178 where: n.user_id == ^user_id
183 def dismiss(%{id: user_id} = _user, id) do
184 notification = Repo.get(Notification, id)
187 %{user_id: ^user_id} ->
188 Repo.delete(notification)
191 {:error, "Cannot dismiss notification"}
195 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
196 object = Object.normalize(activity)
198 unless object && object.data["type"] == "Answer" do
199 users = get_notified_from_activity(activity)
200 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
207 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
208 when type in ["Like", "Announce", "Follow"] do
209 users = get_notified_from_activity(activity)
210 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
214 def create_notifications(_), do: {:ok, []}
216 # TODO move to sql, too.
217 def create_notification(%Activity{} = activity, %User{} = user) do
218 unless skip?(activity, user) do
219 notification = %Notification{user_id: user.id, activity: activity}
220 {:ok, notification} = Repo.insert(notification)
222 ["user", "user:notification"]
223 |> Streamer.stream(notification)
225 Push.send(notification)
230 def get_notified_from_activity(activity, local_only \\ true)
232 def get_notified_from_activity(
233 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
236 when type in ["Create", "Like", "Announce", "Follow"] do
239 |> Utils.maybe_notify_to_recipients(activity)
240 |> Utils.maybe_notify_mentioned_recipients(activity)
241 |> Utils.maybe_notify_subscribers(activity)
244 User.get_users_from_set(recipients, local_only)
247 def get_notified_from_activity(_, _local_only), do: []
249 @spec skip?(Activity.t(), User.t()) :: boolean()
250 def skip?(activity, user) do
259 |> Enum.any?(&skip?(&1, activity, user))
262 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
263 def skip?(:self, activity, user) do
264 activity.data["actor"] == user.ap_id
270 %{info: %{notification_settings: %{"followers" => false}}} = user
272 actor = activity.data["actor"]
273 follower = User.get_cached_by_ap_id(actor)
274 User.following?(follower, user)
280 %{info: %{notification_settings: %{"non_followers" => false}}} = user
282 actor = activity.data["actor"]
283 follower = User.get_cached_by_ap_id(actor)
284 !User.following?(follower, user)
287 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
288 actor = activity.data["actor"]
289 followed = User.get_cached_by_ap_id(actor)
290 User.following?(user, followed)
296 %{info: %{notification_settings: %{"non_follows" => false}}} = user
298 actor = activity.data["actor"]
299 followed = User.get_cached_by_ap_id(actor)
300 !User.following?(user, followed)
303 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
304 actor = activity.data["actor"]
306 Notification.for_user(user)
308 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
313 def skip?(_, _, _), do: false