Merge develop
[akkoma] / lib / pleroma / notification.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Notification do
6 use Ecto.Schema
7
8 alias Pleroma.Activity
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Pagination
12 alias Pleroma.Repo
13 alias Pleroma.User
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.CommonAPI.Utils
16
17 import Ecto.Query
18 import Ecto.Changeset
19
20 @type t :: %__MODULE__{}
21
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)
26
27 timestamps()
28 end
29
30 def changeset(%Notification{} = notification, attrs) do
31 notification
32 |> cast(attrs, [:seen])
33 end
34
35 def for_user_query(user) do
36 Notification
37 |> where(user_id: ^user.id)
38 |> join(:inner, [n], activity in assoc(n, :activity))
39 |> join(:left, [n, a], object in Object,
40 on:
41 fragment(
42 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
43 object.data,
44 a.data
45 )
46 )
47 |> preload([n, a, o], activity: {a, object: o})
48 end
49
50 def for_user(user, opts \\ %{}) do
51 user
52 |> for_user_query()
53 |> Pagination.fetch_paginated(opts)
54 end
55
56 @doc """
57 Returns notifications for user received since given date.
58
59 ## Examples
60
61 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
62 [%Pleroma.Notification{}, %Pleroma.Notification{}]
63
64 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
65 []
66 """
67 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
68 def for_user_since(user, date) do
69 from(n in for_user_query(user),
70 where: n.updated_at > ^date
71 )
72 |> Repo.all()
73 end
74
75 def set_read_up_to(%{id: user_id} = _user, id) do
76 query =
77 from(
78 n in Notification,
79 where: n.user_id == ^user_id,
80 where: n.id <= ^id,
81 update: [
82 set: [
83 seen: true,
84 updated_at: ^NaiveDateTime.utc_now()
85 ]
86 ]
87 )
88
89 Repo.update_all(query, [])
90 end
91
92 def read_one(%User{} = user, notification_id) do
93 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
94 notification
95 |> changeset(%{seen: true})
96 |> Repo.update()
97 end
98 end
99
100 def get(%{id: user_id} = _user, id) do
101 query =
102 from(
103 n in Notification,
104 where: n.id == ^id,
105 join: activity in assoc(n, :activity),
106 preload: [activity: activity]
107 )
108
109 notification = Repo.one(query)
110
111 case notification do
112 %{user_id: ^user_id} ->
113 {:ok, notification}
114
115 _ ->
116 {:error, "Cannot get notification"}
117 end
118 end
119
120 def clear(user) do
121 from(n in Notification, where: n.user_id == ^user.id)
122 |> Repo.delete_all()
123 end
124
125 def destroy_multiple(%{id: user_id} = _user, ids) do
126 from(n in Notification,
127 where: n.id in ^ids,
128 where: n.user_id == ^user_id
129 )
130 |> Repo.delete_all()
131 end
132
133 def dismiss(%{id: user_id} = _user, id) do
134 notification = Repo.get(Notification, id)
135
136 case notification do
137 %{user_id: ^user_id} ->
138 Repo.delete(notification)
139
140 _ ->
141 {:error, "Cannot dismiss notification"}
142 end
143 end
144
145 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
146 when type in ["Create", "Like", "Announce", "Follow"] do
147 users = get_notified_from_activity(activity)
148
149 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
150 {:ok, notifications}
151 end
152
153 def create_notifications(_), do: {:ok, []}
154
155 # TODO move to sql, too.
156 def create_notification(%Activity{} = activity, %User{} = user) do
157 unless skip?(activity, user) do
158 notification = %Notification{user_id: user.id, activity: activity}
159 {:ok, notification} = Repo.insert(notification)
160 Pleroma.Web.Streamer.stream("user", notification)
161 Pleroma.Web.Push.send(notification)
162 notification
163 end
164 end
165
166 def get_notified_from_activity(activity, local_only \\ true)
167
168 def get_notified_from_activity(
169 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
170 local_only
171 )
172 when type in ["Create", "Like", "Announce", "Follow"] do
173 recipients =
174 []
175 |> Utils.maybe_notify_to_recipients(activity)
176 |> Utils.maybe_notify_mentioned_recipients(activity)
177 |> Utils.maybe_notify_subscribers(activity)
178 |> Enum.uniq()
179
180 User.get_users_from_set(recipients, local_only)
181 end
182
183 def get_notified_from_activity(_, _local_only), do: []
184
185 def skip?(activity, user) do
186 [:self, :blocked, :local, :muted, :followers, :follows, :recently_followed]
187 |> Enum.any?(&skip?(&1, activity, user))
188 end
189
190 def skip?(:self, activity, user) do
191 activity.data["actor"] == user.ap_id
192 end
193
194 def skip?(:blocked, activity, user) do
195 actor = activity.data["actor"]
196 User.blocks?(user, %{ap_id: actor})
197 end
198
199 def skip?(:local, %{local: true}, %{info: %{notification_settings: %{"local" => false}}}),
200 do: true
201
202 def skip?(:local, %{local: false}, %{info: %{notification_settings: %{"remote" => false}}}),
203 do: true
204
205 def skip?(:muted, activity, user) do
206 actor = activity.data["actor"]
207
208 User.mutes?(user, %{ap_id: actor}) or CommonAPI.thread_muted?(user, activity)
209 end
210
211 def skip?(
212 :followers,
213 activity,
214 %{info: %{notification_settings: %{"followers" => false}}} = user
215 ) do
216 actor = activity.data["actor"]
217 follower = User.get_cached_by_ap_id(actor)
218 User.following?(follower, user)
219 end
220
221 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
222 actor = activity.data["actor"]
223 followed = User.get_cached_by_ap_id(actor)
224 User.following?(user, followed)
225 end
226
227 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
228 actor = activity.data["actor"]
229
230 Notification.for_user(user)
231 |> Enum.any?(fn
232 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
233 _ -> false
234 end)
235 end
236
237 def skip?(_, _, _), do: false
238 end