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.SubscriptionNotification do
10 alias Pleroma.Pagination
12 alias Pleroma.SubscriptionNotification
14 alias Pleroma.Web.CommonAPI.Utils
15 alias Pleroma.Web.Push
16 alias Pleroma.Web.Streamer
21 @type t :: %__MODULE__{}
23 schema "subscription_notifications" do
24 belongs_to(:user, User, type: Pleroma.FlakeId)
25 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
30 def changeset(%SubscriptionNotification{} = notification, attrs) do
31 cast(notification, attrs, [])
34 def for_user_query(user, opts \\ []) do
36 SubscriptionNotification
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)
79 Returns notifications for user received since given date.
83 iex> Pleroma.SubscriptionNotification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
84 [%Pleroma.SubscriptionNotification{}, %Pleroma.SubscriptionNotification{}]
86 iex> Pleroma.SubscriptionNotification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
89 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
90 def for_user_since(user, date) do
91 from(n in for_user_query(user),
92 where: n.updated_at > ^date
97 def clear_up_to(%{id: user_id} = _user, id) do
99 n in SubscriptionNotification,
100 where: n.user_id == ^user_id,
103 |> Repo.delete_all([])
106 def get(%{id: user_id} = _user, id) do
109 n in SubscriptionNotification,
111 join: activity in assoc(n, :activity),
112 preload: [activity: activity]
115 notification = Repo.one(query)
118 %{user_id: ^user_id} ->
122 {:error, "Cannot get notification"}
127 from(n in SubscriptionNotification, where: n.user_id == ^user.id)
131 def destroy_multiple(%{id: user_id} = _user, ids) do
132 from(n in SubscriptionNotification,
134 where: n.user_id == ^user_id
139 def dismiss(%{id: user_id} = _user, id) do
140 notification = Repo.get(SubscriptionNotification, id)
143 %{user_id: ^user_id} ->
144 Repo.delete(notification)
147 {:error, "Cannot dismiss notification"}
151 def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
152 object = Object.normalize(activity)
154 unless object && object.data["type"] == "Answer" do
155 users = get_notified_from_activity(activity)
156 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
163 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
164 when type in ["Like", "Announce", "Follow"] do
165 users = get_notified_from_activity(activity)
166 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
170 def create_notifications(_), do: {:ok, []}
172 # TODO move to sql, too.
173 def create_notification(%Activity{} = activity, %User{} = user) do
174 unless skip?(activity, user) do
175 notification = %SubscriptionNotification{user_id: user.id, activity: activity}
176 {:ok, notification} = Repo.insert(notification)
177 Streamer.stream("user", notification)
178 Streamer.stream("user:subscription_notification", notification)
179 Push.send(notification)
184 def get_notified_from_activity(activity, local_only \\ true)
186 def get_notified_from_activity(
187 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
190 when type in ["Create", "Like", "Announce", "Follow"] do
193 |> Utils.maybe_notify_subscribers(activity)
196 User.get_users_from_set(recipients, local_only)
199 def get_notified_from_activity(_, _local_only), do: []
201 @spec skip?(Activity.t(), User.t()) :: boolean()
202 def skip?(activity, user) do
211 |> Enum.any?(&skip?(&1, activity, user))
214 @spec skip?(atom(), Activity.t(), User.t()) :: boolean()
215 def skip?(:self, activity, user) do
216 activity.data["actor"] == user.ap_id
222 %{info: %{notification_settings: %{"followers" => false}}} = user
224 actor = activity.data["actor"]
225 follower = User.get_cached_by_ap_id(actor)
226 User.following?(follower, user)
232 %{info: %{notification_settings: %{"non_followers" => false}}} = user
234 actor = activity.data["actor"]
235 follower = User.get_cached_by_ap_id(actor)
236 !User.following?(follower, user)
239 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
240 actor = activity.data["actor"]
241 followed = User.get_cached_by_ap_id(actor)
242 User.following?(user, followed)
248 %{info: %{notification_settings: %{"non_follows" => false}}} = user
250 actor = activity.data["actor"]
251 followed = User.get_cached_by_ap_id(actor)
252 !User.following?(user, followed)
255 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
256 actor = activity.data["actor"]
258 SubscriptionNotification.for_user(user)
260 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
265 def skip?(_, _, _), do: false