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
20 schema "notifications" do
21 field(:seen, :boolean, default: false)
22 belongs_to(:user, User, type: Pleroma.FlakeId)
23 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
28 def changeset(%Notification{} = notification, attrs) do
30 |> cast(attrs, [:seen])
33 def for_user_query(user) do
35 |> where(user_id: ^user.id)
39 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
43 |> join(:inner, [n], activity in assoc(n, :activity))
44 |> join(:left, [n, a], object in Object,
47 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
52 |> preload([n, a, o], activity: {a, object: o})
55 def for_user(user, opts \\ %{}) do
58 |> Pagination.fetch_paginated(opts)
61 def set_read_up_to(%{id: user_id} = _user, id) do
65 where: n.user_id == ^user_id,
72 Repo.update_all(query, [])
75 def read_one(%User{} = user, notification_id) do
76 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
78 |> changeset(%{seen: true})
83 def get(%{id: user_id} = _user, id) do
88 join: activity in assoc(n, :activity),
89 preload: [activity: activity]
92 notification = Repo.one(query)
95 %{user_id: ^user_id} ->
99 {:error, "Cannot get notification"}
104 from(n in Notification, where: n.user_id == ^user.id)
108 def destroy_multiple(%{id: user_id} = _user, ids) do
109 from(n in Notification,
111 where: n.user_id == ^user_id
116 def dismiss(%{id: user_id} = _user, id) do
117 notification = Repo.get(Notification, id)
120 %{user_id: ^user_id} ->
121 Repo.delete(notification)
124 {:error, "Cannot dismiss notification"}
128 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
129 when type in ["Create", "Like", "Announce", "Follow"] do
130 object = Object.normalize(activity)
132 unless object && object.data["type"] == "Answer" do
133 users = get_notified_from_activity(activity)
134 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
141 def create_notifications(_), do: {:ok, []}
143 # TODO move to sql, too.
144 def create_notification(%Activity{} = activity, %User{} = user) do
145 unless skip?(activity, user) do
146 notification = %Notification{user_id: user.id, activity: activity}
147 {:ok, notification} = Repo.insert(notification)
148 Pleroma.Web.Streamer.stream("user", notification)
149 Pleroma.Web.Push.send(notification)
154 def get_notified_from_activity(activity, local_only \\ true)
156 def get_notified_from_activity(
157 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
160 when type in ["Create", "Like", "Announce", "Follow"] do
163 |> Utils.maybe_notify_to_recipients(activity)
164 |> Utils.maybe_notify_mentioned_recipients(activity)
165 |> Utils.maybe_notify_subscribers(activity)
168 User.get_users_from_set(recipients, local_only)
171 def get_notified_from_activity(_, _local_only), do: []
173 def skip?(activity, user) do
184 |> Enum.any?(&skip?(&1, activity, user))
187 def skip?(:self, activity, user) do
188 activity.data["actor"] == user.ap_id
191 def skip?(:blocked, activity, user) do
192 actor = activity.data["actor"]
193 User.blocks?(user, %{ap_id: actor})
196 def skip?(:muted, activity, user) do
197 actor = activity.data["actor"]
199 User.mutes?(user, %{ap_id: actor}) or CommonAPI.thread_muted?(user, activity)
205 %{info: %{notification_settings: %{"followers" => false}}} = user
207 actor = activity.data["actor"]
208 follower = User.get_cached_by_ap_id(actor)
209 User.following?(follower, user)
215 %{info: %{notification_settings: %{"non_followers" => false}}} = user
217 actor = activity.data["actor"]
218 follower = User.get_cached_by_ap_id(actor)
219 !User.following?(follower, user)
222 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
223 actor = activity.data["actor"]
224 followed = User.get_cached_by_ap_id(actor)
225 User.following?(user, followed)
231 %{info: %{notification_settings: %{"non_follows" => false}}} = user
233 actor = activity.data["actor"]
234 followed = User.get_cached_by_ap_id(actor)
235 !User.following?(user, followed)
238 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
239 actor = activity.data["actor"]
241 Notification.for_user(user)
243 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
248 def skip?(_, _, _), do: false