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: Pleroma.FlakeId)
26 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
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 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})
58 if opts[:with_muted] do
61 where(query, [n, a], a.actor not in ^user.info.muted_notifications)
62 |> where([n, a], a.actor not in ^user.info.blocks)
65 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.info.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, [])
119 from(n in Notification, where: n.id in ^notification_ids)
120 |> join(:inner, [n], activity in assoc(n, :activity))
121 |> join(:left, [n, a], object in Object,
124 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
129 |> preload([n, a, o], activity: {a, object: o})
133 def read_one(%User{} = user, notification_id) do
134 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
136 |> changeset(%{seen: true})
141 def get(%{id: user_id} = _user, id) do
146 join: activity in assoc(n, :activity),
147 preload: [activity: activity]
150 notification = Repo.one(query)
153 %{user_id: ^user_id} ->
157 {:error, "Cannot get notification"}
162 from(n in Notification, where: n.user_id == ^user.id)
166 def destroy_multiple(%{id: user_id} = _user, ids) do
167 from(n in Notification,
169 where: n.user_id == ^user_id
174 def dismiss(%{id: user_id} = _user, id) do
175 notification = Repo.get(Notification, id)
178 %{user_id: ^user_id} ->
179 Repo.delete(notification)
182 {:error, "Cannot dismiss notification"}
186 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
187 object = Object.normalize(activity)
189 unless object && object.data["type"] == "Answer" do
190 users = get_notified_from_activity(activity)
191 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
198 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
199 when type in ["Like", "Announce", "Follow"] do
200 users = get_notified_from_activity(activity)
201 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
205 def create_notifications(_), do: {:ok, []}
207 # TODO move to sql, too.
208 def create_notification(%Activity{} = activity, %User{} = user) do
209 unless skip?(activity, user) do
210 notification = %Notification{user_id: user.id, activity: activity}
211 {:ok, notification} = Repo.insert(notification)
212 Streamer.stream("user", notification)
213 Streamer.stream("user:notification", notification)
214 Push.send(notification)
219 def get_notified_from_activity(activity, local_only \\ true)
221 def get_notified_from_activity(
222 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
225 when type in ["Create", "Like", "Announce", "Follow"] do
228 |> Utils.maybe_notify_to_recipients(activity)
229 |> Utils.maybe_notify_mentioned_recipients(activity)
230 |> Utils.maybe_notify_subscribers(activity)
233 User.get_users_from_set(recipients, local_only)
236 def get_notified_from_activity(_, _local_only), do: []
238 @spec skip?(Activity.t(), User.t()) :: boolean()
239 def skip?(activity, user) do
248 |> Enum.any?(&skip?(&1, activity, user))
251 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
252 def skip?(:self, activity, user) do
253 activity.data["actor"] == user.ap_id
259 %{info: %{notification_settings: %{"followers" => false}}} = user
261 actor = activity.data["actor"]
262 follower = User.get_cached_by_ap_id(actor)
263 User.following?(follower, user)
269 %{info: %{notification_settings: %{"non_followers" => false}}} = user
271 actor = activity.data["actor"]
272 follower = User.get_cached_by_ap_id(actor)
273 !User.following?(follower, user)
276 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
277 actor = activity.data["actor"]
278 followed = User.get_cached_by_ap_id(actor)
279 User.following?(user, followed)
285 %{info: %{notification_settings: %{"non_follows" => false}}} = user
287 actor = activity.data["actor"]
288 followed = User.get_cached_by_ap_id(actor)
289 !User.following?(user, followed)
292 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
293 actor = activity.data["actor"]
295 Notification.for_user(user)
297 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
302 def skip?(_, _, _), do: false