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 schema "notifications" do
22 field(:seen, :boolean, default: false)
23 belongs_to(:user, User, type: Pleroma.FlakeId)
24 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
29 def changeset(%Notification{} = notification, attrs) do
31 |> cast(attrs, [:seen])
34 def for_user_query(user, opts) do
37 |> where(user_id: ^user.id)
41 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
45 |> join(:inner, [n], activity in assoc(n, :activity))
46 |> join(:left, [n, a], object in Object,
49 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
54 |> preload([n, a, o], activity: {a, object: o})
56 if opts[:with_muted] do
59 where(query, [n, a], a.actor not in ^user.info.muted_notifications)
60 |> where([n, a], a.actor not in ^user.info.blocks)
63 fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.info.domain_blocks
65 |> join(:left, [n, a], tm in Pleroma.ThreadMute,
66 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
68 |> where([n, a, o, tm], is_nil(tm.user_id))
72 def for_user(user, opts \\ %{}) do
74 |> for_user_query(opts)
75 |> Pagination.fetch_paginated(opts)
78 def set_read_up_to(%{id: user_id} = _user, id) do
82 where: n.user_id == ^user_id,
89 Repo.update_all(query, [])
92 def read_one(%User{} = user, notification_id) do
93 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
95 |> changeset(%{seen: true})
100 def get(%{id: user_id} = _user, id) do
105 join: activity in assoc(n, :activity),
106 preload: [activity: activity]
109 notification = Repo.one(query)
112 %{user_id: ^user_id} ->
116 {:error, "Cannot get notification"}
121 from(n in Notification, where: n.user_id == ^user.id)
125 def destroy_multiple(%{id: user_id} = _user, ids) do
126 from(n in Notification,
128 where: n.user_id == ^user_id
133 def dismiss(%{id: user_id} = _user, id) do
134 notification = Repo.get(Notification, id)
137 %{user_id: ^user_id} ->
138 Repo.delete(notification)
141 {:error, "Cannot dismiss notification"}
145 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
146 object = Object.normalize(activity)
148 unless object && object.data["type"] == "Answer" do
149 users = get_notified_from_activity(activity)
150 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
157 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
158 when type in ["Like", "Announce", "Follow"] do
159 users = get_notified_from_activity(activity)
160 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
164 def create_notifications(_), do: {:ok, []}
166 # TODO move to sql, too.
167 def create_notification(%Activity{} = activity, %User{} = user) do
168 unless skip?(activity, user) do
169 notification = %Notification{user_id: user.id, activity: activity}
170 {:ok, notification} = Repo.insert(notification)
171 Streamer.stream("user", notification)
172 Streamer.stream("user:notification", notification)
173 Push.send(notification)
178 def get_notified_from_activity(activity, local_only \\ true)
180 def get_notified_from_activity(
181 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
184 when type in ["Create", "Like", "Announce", "Follow"] do
187 |> Utils.maybe_notify_to_recipients(activity)
188 |> Utils.maybe_notify_mentioned_recipients(activity)
189 |> Utils.maybe_notify_subscribers(activity)
192 User.get_users_from_set(recipients, local_only)
195 def get_notified_from_activity(_, _local_only), do: []
197 @spec skip?(Activity.t(), User.t()) :: boolean()
198 def skip?(activity, user) do
207 |> Enum.any?(&skip?(&1, activity, user))
210 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
211 def skip?(:self, activity, user) do
212 activity.data["actor"] == user.ap_id
218 %{info: %{notification_settings: %{"followers" => false}}} = user
220 actor = activity.data["actor"]
221 follower = User.get_cached_by_ap_id(actor)
222 User.following?(follower, user)
228 %{info: %{notification_settings: %{"non_followers" => false}}} = user
230 actor = activity.data["actor"]
231 follower = User.get_cached_by_ap_id(actor)
232 !User.following?(follower, user)
235 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
236 actor = activity.data["actor"]
237 followed = User.get_cached_by_ap_id(actor)
238 User.following?(user, followed)
244 %{info: %{notification_settings: %{"non_follows" => false}}} = user
246 actor = activity.data["actor"]
247 followed = User.get_cached_by_ap_id(actor)
248 !User.following?(user, followed)
251 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
252 actor = activity.data["actor"]
254 Notification.for_user(user)
256 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
261 def skip?(_, _, _), do: false