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
15 alias Pleroma.Web.CommonAPI.Utils
16 alias Pleroma.Web.Push
17 alias Pleroma.Web.Streamer
22 @type t :: %__MODULE__{}
24 schema "notifications" do
25 field(:seen, :boolean, default: false)
26 belongs_to(:user, User, type: Pleroma.FlakeId)
27 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
32 def changeset(%Notification{} = notification, attrs) do
34 |> cast(attrs, [:seen])
37 def for_user_query(user) do
39 |> where(user_id: ^user.id)
43 "? not in (SELECT ap_id FROM users WHERE info->'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})
59 def for_user(user, opts \\ %{}) do
62 |> Pagination.fetch_paginated(opts)
66 Returns notifications for user received since given date.
70 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
71 [%Pleroma.Notification{}, %Pleroma.Notification{}]
73 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
76 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
77 def for_user_since(user, date) do
78 from(n in for_user_query(user),
79 where: n.updated_at > ^date
84 def set_read_up_to(%{id: user_id} = _user, id) do
88 where: n.user_id == ^user_id,
93 updated_at: ^NaiveDateTime.utc_now()
98 Repo.update_all(query, [])
101 def read_one(%User{} = user, notification_id) do
102 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
104 |> changeset(%{seen: true})
109 def get(%{id: user_id} = _user, id) do
114 join: activity in assoc(n, :activity),
115 preload: [activity: activity]
118 notification = Repo.one(query)
121 %{user_id: ^user_id} ->
125 {:error, "Cannot get notification"}
130 from(n in Notification, where: n.user_id == ^user.id)
134 def destroy_multiple(%{id: user_id} = _user, ids) do
135 from(n in Notification,
137 where: n.user_id == ^user_id
142 def dismiss(%{id: user_id} = _user, id) do
143 notification = Repo.get(Notification, id)
146 %{user_id: ^user_id} ->
147 Repo.delete(notification)
150 {:error, "Cannot dismiss notification"}
154 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
155 object = Object.normalize(activity)
157 unless object && object.data["type"] == "Answer" do
158 users = get_notified_from_activity(activity)
159 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
166 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
167 when type in ["Like", "Announce", "Follow"] do
168 users = get_notified_from_activity(activity)
169 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
173 def create_notifications(_), do: {:ok, []}
175 # TODO move to sql, too.
176 def create_notification(%Activity{} = activity, %User{} = user) do
177 unless skip?(activity, user) do
178 notification = %Notification{user_id: user.id, activity: activity}
179 {:ok, notification} = Repo.insert(notification)
180 Streamer.stream("user", notification)
181 Streamer.stream("user:notification", notification)
182 Push.send(notification)
187 def get_notified_from_activity(activity, local_only \\ true)
189 def get_notified_from_activity(
190 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
193 when type in ["Create", "Like", "Announce", "Follow"] do
196 |> Utils.maybe_notify_to_recipients(activity)
197 |> Utils.maybe_notify_mentioned_recipients(activity)
198 |> Utils.maybe_notify_subscribers(activity)
201 User.get_users_from_set(recipients, local_only)
204 def get_notified_from_activity(_, _local_only), do: []
206 def skip?(activity, user) do
217 |> Enum.any?(&skip?(&1, activity, user))
220 def skip?(:self, activity, user) do
221 activity.data["actor"] == user.ap_id
224 def skip?(:blocked, activity, user) do
225 actor = activity.data["actor"]
226 User.blocks?(user, %{ap_id: actor})
229 def skip?(:muted, activity, user) do
230 actor = activity.data["actor"]
232 User.mutes?(user, %{ap_id: actor}) or CommonAPI.thread_muted?(user, activity)
238 %{info: %{notification_settings: %{"followers" => false}}} = user
240 actor = activity.data["actor"]
241 follower = User.get_cached_by_ap_id(actor)
242 User.following?(follower, user)
248 %{info: %{notification_settings: %{"non_followers" => false}}} = user
250 actor = activity.data["actor"]
251 follower = User.get_cached_by_ap_id(actor)
252 !User.following?(follower, user)
255 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
256 actor = activity.data["actor"]
257 followed = User.get_cached_by_ap_id(actor)
258 User.following?(user, followed)
264 %{info: %{notification_settings: %{"non_follows" => false}}} = user
266 actor = activity.data["actor"]
267 followed = User.get_cached_by_ap_id(actor)
268 !User.following?(user, followed)
271 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
272 actor = activity.data["actor"]
274 Notification.for_user(user)
276 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
281 def skip?(_, _, _), do: false