Resolve conflicts
[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 |> where(
39 [n, a],
40 fragment(
41 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
42 a.actor
43 )
44 )
45 |> join(:inner, [n], activity in assoc(n, :activity))
46 |> join(:left, [n, a], object in Object,
47 on:
48 fragment(
49 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
50 object.data,
51 a.data
52 )
53 )
54 |> preload([n, a, o], activity: {a, object: o})
55 end
56
57 def for_user(user, opts \\ %{}) do
58 user
59 |> for_user_query()
60 |> Pagination.fetch_paginated(opts)
61 end
62
63 @doc """
64 Returns notifications for user received since given date.
65
66 ## Examples
67
68 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33])
69 [%Pleroma.Notification{}, %Pleroma.Notification{}]
70
71 iex> Pleroma.Notification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33])
72 []
73 """
74 @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()]
75 def for_user_since(user, date) do
76 from(n in for_user_query(user),
77 where: n.updated_at > ^date
78 )
79 |> Repo.all()
80 end
81
82 def set_read_up_to(%{id: user_id} = _user, id) do
83 query =
84 from(
85 n in Notification,
86 where: n.user_id == ^user_id,
87 where: n.id <= ^id,
88 update: [
89 set: [
90 seen: true,
91 updated_at: ^NaiveDateTime.utc_now()
92 ]
93 ]
94 )
95
96 Repo.update_all(query, [])
97 end
98
99 def read_one(%User{} = user, notification_id) do
100 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
101 notification
102 |> changeset(%{seen: true})
103 |> Repo.update()
104 end
105 end
106
107 def get(%{id: user_id} = _user, id) do
108 query =
109 from(
110 n in Notification,
111 where: n.id == ^id,
112 join: activity in assoc(n, :activity),
113 preload: [activity: activity]
114 )
115
116 notification = Repo.one(query)
117
118 case notification do
119 %{user_id: ^user_id} ->
120 {:ok, notification}
121
122 _ ->
123 {:error, "Cannot get notification"}
124 end
125 end
126
127 def clear(user) do
128 from(n in Notification, where: n.user_id == ^user.id)
129 |> Repo.delete_all()
130 end
131
132 def destroy_multiple(%{id: user_id} = _user, ids) do
133 from(n in Notification,
134 where: n.id in ^ids,
135 where: n.user_id == ^user_id
136 )
137 |> Repo.delete_all()
138 end
139
140 def dismiss(%{id: user_id} = _user, id) do
141 notification = Repo.get(Notification, id)
142
143 case notification do
144 %{user_id: ^user_id} ->
145 Repo.delete(notification)
146
147 _ ->
148 {:error, "Cannot dismiss notification"}
149 end
150 end
151
152 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
153 when type in ["Create", "Like", "Announce", "Follow"] do
154 object = Object.normalize(activity)
155
156 unless object && object.data["type"] == "Answer" do
157 users = get_notified_from_activity(activity)
158 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
159 {:ok, notifications}
160 else
161 {:ok, []}
162 end
163 end
164
165 def create_notifications(_), do: {:ok, []}
166
167 # TODO move to sql, too.
168 def create_notification(%Activity{} = activity, %User{} = user) do
169 unless skip?(activity, user) do
170 notification = %Notification{user_id: user.id, activity: activity}
171 {:ok, notification} = Repo.insert(notification)
172 Pleroma.Web.Streamer.stream("user", notification)
173 Pleroma.Web.Push.send(notification)
174 notification
175 end
176 end
177
178 def get_notified_from_activity(activity, local_only \\ true)
179
180 def get_notified_from_activity(
181 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
182 local_only
183 )
184 when type in ["Create", "Like", "Announce", "Follow"] do
185 recipients =
186 []
187 |> Utils.maybe_notify_to_recipients(activity)
188 |> Utils.maybe_notify_mentioned_recipients(activity)
189 |> Utils.maybe_notify_subscribers(activity)
190 |> Enum.uniq()
191
192 User.get_users_from_set(recipients, local_only)
193 end
194
195 def get_notified_from_activity(_, _local_only), do: []
196
197 def skip?(activity, user) do
198 [
199 :self,
200 :blocked,
201 :muted,
202 :followers,
203 :follows,
204 :non_followers,
205 :non_follows,
206 :recently_followed
207 ]
208 |> Enum.any?(&skip?(&1, activity, user))
209 end
210
211 def skip?(:self, activity, user) do
212 activity.data["actor"] == user.ap_id
213 end
214
215 def skip?(:blocked, activity, user) do
216 actor = activity.data["actor"]
217 User.blocks?(user, %{ap_id: actor})
218 end
219
220 def skip?(:muted, activity, user) do
221 actor = activity.data["actor"]
222
223 User.mutes?(user, %{ap_id: actor}) or CommonAPI.thread_muted?(user, activity)
224 end
225
226 def skip?(
227 :followers,
228 activity,
229 %{info: %{notification_settings: %{"followers" => false}}} = user
230 ) do
231 actor = activity.data["actor"]
232 follower = User.get_cached_by_ap_id(actor)
233 User.following?(follower, user)
234 end
235
236 def skip?(
237 :non_followers,
238 activity,
239 %{info: %{notification_settings: %{"non_followers" => false}}} = user
240 ) do
241 actor = activity.data["actor"]
242 follower = User.get_cached_by_ap_id(actor)
243 !User.following?(follower, user)
244 end
245
246 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
247 actor = activity.data["actor"]
248 followed = User.get_cached_by_ap_id(actor)
249 User.following?(user, followed)
250 end
251
252 def skip?(
253 :non_follows,
254 activity,
255 %{info: %{notification_settings: %{"non_follows" => false}}} = user
256 ) do
257 actor = activity.data["actor"]
258 followed = User.get_cached_by_ap_id(actor)
259 !User.following?(user, followed)
260 end
261
262 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
263 actor = activity.data["actor"]
264
265 Notification.for_user(user)
266 |> Enum.any?(fn
267 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
268 _ -> false
269 end)
270 end
271
272 def skip?(_, _, _), do: false
273 end