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, [])
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)
235 User.get_users_from_set(recipients, local_only)
238 def get_notified_from_activity(_, _local_only), do: []
240 @spec skip?(Activity.t(), User.t()) :: boolean()
241 def skip?(activity, user) do
250 |> Enum.any?(&skip?(&1, activity, user))
253 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
254 def skip?(:self, activity, user) do
255 activity.data["actor"] == user.ap_id
261 %{info: %{notification_settings: %{"followers" => false}}} = user
263 actor = activity.data["actor"]
264 follower = User.get_cached_by_ap_id(actor)
265 User.following?(follower, user)
271 %{info: %{notification_settings: %{"non_followers" => false}}} = user
273 actor = activity.data["actor"]
274 follower = User.get_cached_by_ap_id(actor)
275 !User.following?(follower, user)
278 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
279 actor = activity.data["actor"]
280 followed = User.get_cached_by_ap_id(actor)
281 User.following?(user, followed)
287 %{info: %{notification_settings: %{"non_follows" => false}}} = user
289 actor = activity.data["actor"]
290 followed = User.get_cached_by_ap_id(actor)
291 !User.following?(user, followed)
294 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
295 actor = activity.data["actor"]
297 Notification.for_user(user)
299 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
304 def skip?(_, _, _), do: false