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
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})
58 if opts[:with_muted] do
61 where(query, [n, a], a.actor not in ^user.muted_notifications)
62 |> where([n, a], a.actor not in ^user.blocks)
65 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.domain_blocks
67 |> join(:left, [n, a], tm in Pleroma.ThreadMute,
68 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
70 |> where([n, a, o, tm], is_nil(tm.user_id))
74 def for_user(user, opts \\ %{}) do
76 |> for_user_query(opts)
77 |> Pagination.fetch_paginated(opts)
81 Returns notifications for user received since given date.
85 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
86 [%Pleroma.Notification{}, %Pleroma.Notification{}]
88 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
91 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
92 def for_user_since(user, date) do
93 from(n in for_user_query(user),
94 where: n.updated_at > ^date
99 def set_read_up_to(%{id: user_id} = _user, id) do
103 where: n.user_id == ^user_id,
105 where: n.seen == false,
109 updated_at: ^NaiveDateTime.utc_now()
112 # Ideally we would preload object and activities here
113 # but Ecto does not support preloads in update_all
117 {_, notification_ids} = Repo.update_all(query, [])
120 |> where([n], n.id in ^notification_ids)
121 |> join(:inner, [n], activity in assoc(n, :activity))
122 |> join(:left, [n, a], object in Object,
125 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
130 |> preload([n, a, o], activity: {a, object: o})
134 def read_one(%User{} = user, notification_id) do
135 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
137 |> changeset(%{seen: true})
142 def get(%{id: user_id} = _user, id) do
147 join: activity in assoc(n, :activity),
148 preload: [activity: activity]
151 notification = Repo.one(query)
154 %{user_id: ^user_id} ->
158 {:error, "Cannot get notification"}
163 from(n in Notification, where: n.user_id == ^user.id)
167 def destroy_multiple(%{id: user_id} = _user, ids) do
168 from(n in Notification,
170 where: n.user_id == ^user_id
175 def dismiss(%{id: user_id} = _user, id) do
176 notification = Repo.get(Notification, id)
179 %{user_id: ^user_id} ->
180 Repo.delete(notification)
183 {:error, "Cannot dismiss notification"}
187 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
188 object = Object.normalize(activity)
190 unless object && object.data["type"] == "Answer" do
191 users = get_notified_from_activity(activity)
192 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
199 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
200 when type in ["Like", "Announce", "Follow"] do
201 users = get_notified_from_activity(activity)
202 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
206 def create_notifications(_), do: {:ok, []}
208 # TODO move to sql, too.
209 def create_notification(%Activity{} = activity, %User{} = user) do
210 unless skip?(activity, user) do
211 notification = %Notification{user_id: user.id, activity: activity}
212 {:ok, notification} = Repo.insert(notification)
214 ["user", "user:notification"]
215 |> Streamer.stream(notification)
217 Push.send(notification)
222 def get_notified_from_activity(activity, local_only \\ true)
224 def get_notified_from_activity(
225 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
228 when type in ["Create", "Like", "Announce", "Follow"] do
231 |> Utils.maybe_notify_to_recipients(activity)
232 |> Utils.maybe_notify_mentioned_recipients(activity)
233 |> Utils.maybe_notify_subscribers(activity)
236 User.get_users_from_set(recipients, local_only)
239 def get_notified_from_activity(_, _local_only), do: []
241 @spec skip?(Activity.t(), User.t()) :: boolean()
242 def skip?(activity, user) do
251 |> Enum.any?(&skip?(&1, activity, user))
254 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
255 def skip?(:self, activity, user) do
256 activity.data["actor"] == user.ap_id
262 %{notification_settings: %{"followers" => false}} = user
264 actor = activity.data["actor"]
265 follower = User.get_cached_by_ap_id(actor)
266 User.following?(follower, user)
272 %{notification_settings: %{"non_followers" => false}} = user
274 actor = activity.data["actor"]
275 follower = User.get_cached_by_ap_id(actor)
276 !User.following?(follower, user)
279 def skip?(:follows, activity, %{notification_settings: %{"follows" => false}} = user) do
280 actor = activity.data["actor"]
281 followed = User.get_cached_by_ap_id(actor)
282 User.following?(user, followed)
288 %{notification_settings: %{"non_follows" => false}} = user
290 actor = activity.data["actor"]
291 followed = User.get_cached_by_ap_id(actor)
292 !User.following?(user, followed)
295 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
296 actor = activity.data["actor"]
298 Notification.for_user(user)
300 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
305 def skip?(_, _, _), do: false