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,
108 updated_at: ^NaiveDateTime.utc_now()
113 Repo.update_all(query, [])
116 def read_one(%User{} = user, notification_id) do
117 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
119 |> changeset(%{seen: true})
124 def get(%{id: user_id} = _user, id) do
129 join: activity in assoc(n, :activity),
130 preload: [activity: activity]
133 notification = Repo.one(query)
136 %{user_id: ^user_id} ->
140 {:error, "Cannot get notification"}
145 from(n in Notification, where: n.user_id == ^user.id)
149 def destroy_multiple(%{id: user_id} = _user, ids) do
150 from(n in Notification,
152 where: n.user_id == ^user_id
157 def dismiss(%{id: user_id} = _user, id) do
158 notification = Repo.get(Notification, id)
161 %{user_id: ^user_id} ->
162 Repo.delete(notification)
165 {:error, "Cannot dismiss notification"}
169 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
170 object = Object.normalize(activity)
172 unless object && object.data["type"] == "Answer" do
173 users = get_notified_from_activity(activity)
174 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
181 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
182 when type in ["Like", "Announce", "Follow"] do
183 users = get_notified_from_activity(activity)
184 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
188 def create_notifications(_), do: {:ok, []}
190 # TODO move to sql, too.
191 def create_notification(%Activity{} = activity, %User{} = user) do
192 unless skip?(activity, user) do
193 notification = %Notification{user_id: user.id, activity: activity}
194 {:ok, notification} = Repo.insert(notification)
195 Streamer.stream("user", notification)
196 Streamer.stream("user:notification", notification)
197 Push.send(notification)
202 def get_notified_from_activity(activity, local_only \\ true)
204 def get_notified_from_activity(
205 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
208 when type in ["Create", "Like", "Announce", "Follow"] do
211 |> Utils.maybe_notify_to_recipients(activity)
212 |> Utils.maybe_notify_mentioned_recipients(activity)
213 |> Utils.maybe_notify_subscribers(activity)
216 User.get_users_from_set(recipients, local_only)
219 def get_notified_from_activity(_, _local_only), do: []
221 @spec skip?(Activity.t(), User.t()) :: boolean()
222 def skip?(activity, user) do
231 |> Enum.any?(&skip?(&1, activity, user))
234 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
235 def skip?(:self, activity, user) do
236 activity.data["actor"] == user.ap_id
242 %{info: %{notification_settings: %{"followers" => false}}} = user
244 actor = activity.data["actor"]
245 follower = User.get_cached_by_ap_id(actor)
246 User.following?(follower, user)
252 %{info: %{notification_settings: %{"non_followers" => false}}} = user
254 actor = activity.data["actor"]
255 follower = User.get_cached_by_ap_id(actor)
256 !User.following?(follower, user)
259 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
260 actor = activity.data["actor"]
261 followed = User.get_cached_by_ap_id(actor)
262 User.following?(user, followed)
268 %{info: %{notification_settings: %{"non_follows" => false}}} = user
270 actor = activity.data["actor"]
271 followed = User.get_cached_by_ap_id(actor)
272 !User.following?(user, followed)
275 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
276 actor = activity.data["actor"]
278 Notification.for_user(user)
280 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
285 def skip?(_, _, _), do: false