Format subscriptions related stuff
[akkoma] / test / notification_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.NotificationTest do
6 use Pleroma.DataCase
7 alias Pleroma.Notification
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.Transmogrifier
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.TwitterAPI.TwitterAPI
12 import Pleroma.Factory
13
14 describe "create_notifications" do
15 test "notifies someone when they are directly addressed" do
16 user = insert(:user)
17 other_user = insert(:user)
18 third_user = insert(:user)
19
20 {:ok, activity} =
21 TwitterAPI.create_status(user, %{
22 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
23 })
24
25 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
26
27 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
28 assert notified_ids == [other_user.id, third_user.id]
29 assert notification.activity_id == activity.id
30 assert other_notification.activity_id == activity.id
31 end
32
33 test "it creates a notification for subscribed users" do
34 user = insert(:user)
35 subscriber = insert(:user)
36
37 {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id})
38 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
39 {:ok, [notification]} = Notification.create_notifications(status)
40
41 assert notification.user_id == subscriber.id
42 end
43 end
44
45 describe "create_notification" do
46 test "it doesn't create a notification for user if the user blocks the activity author" do
47 activity = insert(:note_activity)
48 author = User.get_by_ap_id(activity.data["actor"])
49 user = insert(:user)
50 {:ok, user} = User.block(user, author)
51
52 assert nil == Notification.create_notification(activity, user)
53 end
54
55 test "it doesn't create a notification for user if he is the activity author" do
56 activity = insert(:note_activity)
57 author = User.get_by_ap_id(activity.data["actor"])
58
59 assert nil == Notification.create_notification(activity, author)
60 end
61
62 test "it doesn't create a notification for follow-unfollow-follow chains" do
63 user = insert(:user)
64 followed_user = insert(:user)
65 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
66 Notification.create_notification(activity, followed_user)
67 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
68 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
69 assert nil == Notification.create_notification(activity_dupe, followed_user)
70 end
71
72 test "it doesn't create a notification for like-unlike-like chains" do
73 user = insert(:user)
74 liked_user = insert(:user)
75 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
76 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
77 Notification.create_notification(fav_status, liked_user)
78 TwitterAPI.unfav(user, status.id)
79 {:ok, dupe} = TwitterAPI.fav(user, status.id)
80 assert nil == Notification.create_notification(dupe, liked_user)
81 end
82
83 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
84 user = insert(:user)
85 retweeted_user = insert(:user)
86
87 {:ok, status} =
88 TwitterAPI.create_status(retweeted_user, %{
89 "status" => "Send dupe notifications to the shadow realm"
90 })
91
92 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
93 Notification.create_notification(retweeted_activity, retweeted_user)
94 TwitterAPI.unrepeat(user, status.id)
95 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
96 assert nil == Notification.create_notification(dupe, retweeted_user)
97 end
98
99 test "it doesn't create duplicate notifications for follow+subscribed users" do
100 user = insert(:user)
101 subscriber = insert(:user)
102
103 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
104 {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id})
105 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
106 {:ok, [_notif]} = Notification.create_notifications(status)
107 end
108 end
109
110 describe "get notification" do
111 test "it gets a notification that belongs to the user" do
112 user = insert(:user)
113 other_user = insert(:user)
114
115 {:ok, activity} =
116 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
117
118 {:ok, [notification]} = Notification.create_notifications(activity)
119 {:ok, notification} = Notification.get(other_user, notification.id)
120
121 assert notification.user_id == other_user.id
122 end
123
124 test "it returns error if the notification doesn't belong to the user" do
125 user = insert(:user)
126 other_user = insert(:user)
127
128 {:ok, activity} =
129 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
130
131 {:ok, [notification]} = Notification.create_notifications(activity)
132 {:error, _notification} = Notification.get(user, notification.id)
133 end
134 end
135
136 describe "dismiss notification" do
137 test "it dismisses a notification that belongs to the user" do
138 user = insert(:user)
139 other_user = insert(:user)
140
141 {:ok, activity} =
142 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
143
144 {:ok, [notification]} = Notification.create_notifications(activity)
145 {:ok, notification} = Notification.dismiss(other_user, notification.id)
146
147 assert notification.user_id == other_user.id
148 end
149
150 test "it returns error if the notification doesn't belong to the user" do
151 user = insert(:user)
152 other_user = insert(:user)
153
154 {:ok, activity} =
155 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
156
157 {:ok, [notification]} = Notification.create_notifications(activity)
158 {:error, _notification} = Notification.dismiss(user, notification.id)
159 end
160 end
161
162 describe "clear notification" do
163 test "it clears all notifications belonging to the user" do
164 user = insert(:user)
165 other_user = insert(:user)
166 third_user = insert(:user)
167
168 {:ok, activity} =
169 TwitterAPI.create_status(user, %{
170 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
171 })
172
173 {:ok, _notifs} = Notification.create_notifications(activity)
174
175 {:ok, activity} =
176 TwitterAPI.create_status(user, %{
177 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
178 })
179
180 {:ok, _notifs} = Notification.create_notifications(activity)
181 Notification.clear(other_user)
182
183 assert Notification.for_user(other_user) == []
184 assert Notification.for_user(third_user) != []
185 end
186 end
187
188 describe "set_read_up_to()" do
189 test "it sets all notifications as read up to a specified notification ID" do
190 user = insert(:user)
191 other_user = insert(:user)
192
193 {:ok, _activity} =
194 TwitterAPI.create_status(user, %{
195 "status" => "hey @#{other_user.nickname}!"
196 })
197
198 {:ok, _activity} =
199 TwitterAPI.create_status(user, %{
200 "status" => "hey again @#{other_user.nickname}!"
201 })
202
203 [n2, n1] = notifs = Notification.for_user(other_user)
204 assert length(notifs) == 2
205
206 assert n2.id > n1.id
207
208 {:ok, _activity} =
209 TwitterAPI.create_status(user, %{
210 "status" => "hey yet again @#{other_user.nickname}!"
211 })
212
213 Notification.set_read_up_to(other_user, n2.id)
214
215 [n3, n2, n1] = Notification.for_user(other_user)
216
217 assert n1.seen == true
218 assert n2.seen == true
219 assert n3.seen == false
220 end
221 end
222
223 describe "notification target determination" do
224 test "it sends notifications to addressed users in new messages" do
225 user = insert(:user)
226 other_user = insert(:user)
227
228 {:ok, activity} =
229 CommonAPI.post(user, %{
230 "status" => "hey @#{other_user.nickname}!"
231 })
232
233 assert other_user in Notification.get_notified_from_activity(activity)
234 end
235
236 test "it sends notifications to mentioned users in new messages" do
237 user = insert(:user)
238 other_user = insert(:user)
239
240 create_activity = %{
241 "@context" => "https://www.w3.org/ns/activitystreams",
242 "type" => "Create",
243 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
244 "actor" => user.ap_id,
245 "object" => %{
246 "type" => "Note",
247 "content" => "message with a Mention tag, but no explicit tagging",
248 "tag" => [
249 %{
250 "type" => "Mention",
251 "href" => other_user.ap_id,
252 "name" => other_user.nickname
253 }
254 ],
255 "attributedTo" => user.ap_id
256 }
257 }
258
259 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
260
261 assert other_user in Notification.get_notified_from_activity(activity)
262 end
263
264 test "it does not send notifications to users who are only cc in new messages" do
265 user = insert(:user)
266 other_user = insert(:user)
267
268 create_activity = %{
269 "@context" => "https://www.w3.org/ns/activitystreams",
270 "type" => "Create",
271 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
272 "cc" => [other_user.ap_id],
273 "actor" => user.ap_id,
274 "object" => %{
275 "type" => "Note",
276 "content" => "hi everyone",
277 "attributedTo" => user.ap_id
278 }
279 }
280
281 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
282
283 assert other_user not in Notification.get_notified_from_activity(activity)
284 end
285
286 test "it does not send notification to mentioned users in likes" do
287 user = insert(:user)
288 other_user = insert(:user)
289 third_user = insert(:user)
290
291 {:ok, activity_one} =
292 CommonAPI.post(user, %{
293 "status" => "hey @#{other_user.nickname}!"
294 })
295
296 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
297
298 assert other_user not in Notification.get_notified_from_activity(activity_two)
299 end
300
301 test "it does not send notification to mentioned users in announces" do
302 user = insert(:user)
303 other_user = insert(:user)
304 third_user = insert(:user)
305
306 {:ok, activity_one} =
307 CommonAPI.post(user, %{
308 "status" => "hey @#{other_user.nickname}!"
309 })
310
311 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
312
313 assert other_user not in Notification.get_notified_from_activity(activity_two)
314 end
315 end
316
317 describe "notification lifecycle" do
318 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
319 user = insert(:user)
320 other_user = insert(:user)
321
322 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
323
324 assert Enum.empty?(Notification.for_user(user))
325
326 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
327
328 assert length(Notification.for_user(user)) == 1
329
330 {:ok, _} = CommonAPI.delete(activity.id, user)
331
332 assert Enum.empty?(Notification.for_user(user))
333 end
334
335 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
336 user = insert(:user)
337 other_user = insert(:user)
338
339 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
340
341 assert Enum.empty?(Notification.for_user(user))
342
343 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
344
345 assert length(Notification.for_user(user)) == 1
346
347 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
348
349 assert Enum.empty?(Notification.for_user(user))
350 end
351
352 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
353 user = insert(:user)
354 other_user = insert(:user)
355
356 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
357
358 assert Enum.empty?(Notification.for_user(user))
359
360 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
361
362 assert length(Notification.for_user(user)) == 1
363
364 {:ok, _} = CommonAPI.delete(activity.id, user)
365
366 assert Enum.empty?(Notification.for_user(user))
367 end
368
369 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
370 user = insert(:user)
371 other_user = insert(:user)
372
373 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
374
375 assert Enum.empty?(Notification.for_user(user))
376
377 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
378
379 assert length(Notification.for_user(user)) == 1
380
381 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
382
383 assert Enum.empty?(Notification.for_user(user))
384 end
385
386 test "liking an activity which is already deleted does not generate a notification" do
387 user = insert(:user)
388 other_user = insert(:user)
389
390 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
391
392 assert Enum.empty?(Notification.for_user(user))
393
394 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
395
396 assert Enum.empty?(Notification.for_user(user))
397
398 {:error, _} = CommonAPI.favorite(activity.id, other_user)
399
400 assert Enum.empty?(Notification.for_user(user))
401 end
402
403 test "repeating an activity which is already deleted does not generate a notification" do
404 user = insert(:user)
405 other_user = insert(:user)
406
407 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
408
409 assert Enum.empty?(Notification.for_user(user))
410
411 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
412
413 assert Enum.empty?(Notification.for_user(user))
414
415 {:error, _} = CommonAPI.repeat(activity.id, other_user)
416
417 assert Enum.empty?(Notification.for_user(user))
418 end
419
420 test "replying to a deleted post without tagging does not generate a notification" do
421 user = insert(:user)
422 other_user = insert(:user)
423
424 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
425 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
426
427 {:ok, _reply_activity} =
428 CommonAPI.post(other_user, %{
429 "status" => "test reply",
430 "in_reply_to_status_id" => activity.id
431 })
432
433 assert Enum.empty?(Notification.for_user(user))
434 end
435 end
436 end