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
16 alias Pleroma.Web.Push
17 alias Pleroma.Web.Streamer
22 schema "notifications" do
23 field(:seen, :boolean, default: false)
24 belongs_to(:user, User, type: Pleroma.FlakeId)
25 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
30 def changeset(%Notification{} = notification, attrs) do
32 |> cast(attrs, [:seen])
35 def for_user_query(user) 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})
57 def for_user(user, opts \\ %{}) do
60 |> Pagination.fetch_paginated(opts)
63 def set_read_up_to(%{id: user_id} = _user, id) do
67 where: n.user_id == ^user_id,
74 Repo.update_all(query, [])
77 def read_one(%User{} = user, notification_id) do
78 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
80 |> changeset(%{seen: true})
85 def get(%{id: user_id} = _user, id) do
90 join: activity in assoc(n, :activity),
91 preload: [activity: activity]
94 notification = Repo.one(query)
97 %{user_id: ^user_id} ->
101 {:error, "Cannot get notification"}
106 from(n in Notification, where: n.user_id == ^user.id)
110 def destroy_multiple(%{id: user_id} = _user, ids) do
111 from(n in Notification,
113 where: n.user_id == ^user_id
118 def dismiss(%{id: user_id} = _user, id) do
119 notification = Repo.get(Notification, id)
122 %{user_id: ^user_id} ->
123 Repo.delete(notification)
126 {:error, "Cannot dismiss notification"}
130 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
131 when type in ["Create", "Like", "Announce", "Follow"] do
132 object = Object.normalize(activity)
134 unless object && object.data["type"] == "Answer" do
135 users = get_notified_from_activity(activity)
136 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
143 def create_notifications(_), do: {:ok, []}
145 # TODO move to sql, too.
146 def create_notification(%Activity{} = activity, %User{} = user) do
147 unless skip?(activity, user) do
148 notification = %Notification{user_id: user.id, activity: activity}
149 {:ok, notification} = Repo.insert(notification)
150 Streamer.stream("user", notification)
151 Streamer.stream("user:notification", notification)
152 Push.send(notification)
157 def get_notified_from_activity(activity, local_only \\ true)
159 def get_notified_from_activity(
160 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
163 when type in ["Create", "Like", "Announce", "Follow"] do
166 |> Utils.maybe_notify_to_recipients(activity)
167 |> Utils.maybe_notify_mentioned_recipients(activity)
168 |> Utils.maybe_notify_subscribers(activity)
171 User.get_users_from_set(recipients, local_only)
174 def get_notified_from_activity(_, _local_only), do: []
176 def skip?(activity, user) do
187 |> Enum.any?(&skip?(&1, activity, user))
190 def skip?(:self, activity, user) do
191 activity.data["actor"] == user.ap_id
194 def skip?(:blocked, activity, user) do
195 actor = activity.data["actor"]
196 User.blocks?(user, %{ap_id: actor})
199 def skip?(:muted, activity, user) do
200 actor = activity.data["actor"]
202 User.mutes?(user, %{ap_id: actor}) or CommonAPI.thread_muted?(user, activity)
208 %{info: %{notification_settings: %{"followers" => false}}} = user
210 actor = activity.data["actor"]
211 follower = User.get_cached_by_ap_id(actor)
212 User.following?(follower, user)
218 %{info: %{notification_settings: %{"non_followers" => false}}} = user
220 actor = activity.data["actor"]
221 follower = User.get_cached_by_ap_id(actor)
222 !User.following?(follower, user)
225 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
226 actor = activity.data["actor"]
227 followed = User.get_cached_by_ap_id(actor)
228 User.following?(user, followed)
234 %{info: %{notification_settings: %{"non_follows" => false}}} = user
236 actor = activity.data["actor"]
237 followed = User.get_cached_by_ap_id(actor)
238 !User.following?(user, followed)
241 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
242 actor = activity.data["actor"]
244 Notification.for_user(user)
246 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
251 def skip?(_, _, _), do: false