06f0b6557f1a97095c09f76be58b313134dd02ce
[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.Streamer
12 alias Pleroma.Web.TwitterAPI.TwitterAPI
13 import Pleroma.Factory
14
15 describe "create_notifications" do
16 test "notifies someone when they are directly addressed" do
17 user = insert(:user)
18 other_user = insert(:user)
19 third_user = insert(:user)
20
21 {:ok, activity} =
22 TwitterAPI.create_status(user, %{
23 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
24 })
25
26 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
27
28 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
29 assert notified_ids == [other_user.id, third_user.id]
30 assert notification.activity_id == activity.id
31 assert other_notification.activity_id == activity.id
32 end
33
34 test "it creates a notification for subscribed users" do
35 user = insert(:user)
36 subscriber = insert(:user)
37
38 User.subscribe(subscriber, user)
39
40 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
41 {:ok, [notification]} = Notification.create_notifications(status)
42
43 assert notification.user_id == subscriber.id
44 end
45
46 test "does not create a notification for subscribed users if status is a reply" do
47 user = insert(:user)
48 other_user = insert(:user)
49 subscriber = insert(:user)
50
51 User.subscribe(subscriber, other_user)
52
53 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
54
55 {:ok, reply_activity} =
56 CommonAPI.post(other_user, %{
57 "status" => "test reply",
58 "in_reply_to_status_id" => activity.id
59 })
60
61 refute Notification.create_notification(reply_activity, subscriber)
62 end
63 end
64
65 describe "create_notification" do
66 setup do
67 GenServer.start(Streamer, %{}, name: Streamer)
68
69 on_exit(fn ->
70 if pid = Process.whereis(Streamer) do
71 Process.exit(pid, :kill)
72 end
73 end)
74 end
75
76 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
77 user = insert(:user)
78 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
79 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
80 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
81
82 Streamer.add_socket(
83 "user:notification",
84 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
85 )
86
87 activity = insert(:note_activity)
88
89 notify = Notification.create_notification(activity, user)
90 assert notify.user_id == user.id
91 Task.await(task)
92 Task.await(task_user_notification)
93 end
94
95 test "it creates a notification for user if the user blocks the activity author" do
96 activity = insert(:note_activity)
97 author = User.get_cached_by_ap_id(activity.data["actor"])
98 user = insert(:user)
99 {:ok, user} = User.block(user, author)
100
101 assert Notification.create_notification(activity, user)
102 end
103
104 test "it creates a notificatin for the user if the user mutes the activity author" do
105 muter = insert(:user)
106 muted = insert(:user)
107 {:ok, _} = User.mute(muter, muted)
108 muter = Repo.get(User, muter.id)
109 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
110
111 assert Notification.create_notification(activity, muter)
112 end
113
114 test "notification created if user is muted without notifications" do
115 muter = insert(:user)
116 muted = insert(:user)
117
118 {:ok, muter} = User.mute(muter, muted, false)
119
120 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
121
122 assert Notification.create_notification(activity, muter)
123 end
124
125 test "it creates a notification for an activity from a muted thread" do
126 muter = insert(:user)
127 other_user = insert(:user)
128 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
129 CommonAPI.add_mute(muter, activity)
130
131 {:ok, activity} =
132 CommonAPI.post(other_user, %{
133 "status" => "Hi @#{muter.nickname}",
134 "in_reply_to_status_id" => activity.id
135 })
136
137 assert Notification.create_notification(activity, muter)
138 end
139
140 test "it disables notifications from followers" do
141 follower = insert(:user)
142 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
143 User.follow(follower, followed)
144 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
145 refute Notification.create_notification(activity, followed)
146 end
147
148 test "it disables notifications from non-followers" do
149 follower = insert(:user)
150 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
151 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
152 refute Notification.create_notification(activity, followed)
153 end
154
155 test "it disables notifications from people the user follows" do
156 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
157 followed = insert(:user)
158 User.follow(follower, followed)
159 follower = Repo.get(User, follower.id)
160 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
161 refute Notification.create_notification(activity, follower)
162 end
163
164 test "it disables notifications from people the user does not follow" do
165 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
166 followed = insert(:user)
167 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
168 refute Notification.create_notification(activity, follower)
169 end
170
171 test "it doesn't create a notification for user if he is the activity author" do
172 activity = insert(:note_activity)
173 author = User.get_cached_by_ap_id(activity.data["actor"])
174
175 refute Notification.create_notification(activity, author)
176 end
177
178 test "it doesn't create a notification for follow-unfollow-follow chains" do
179 user = insert(:user)
180 followed_user = insert(:user)
181 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
182 Notification.create_notification(activity, followed_user)
183 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
184 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
185 refute Notification.create_notification(activity_dupe, followed_user)
186 end
187
188 test "it doesn't create a notification for like-unlike-like chains" do
189 user = insert(:user)
190 liked_user = insert(:user)
191 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
192 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
193 Notification.create_notification(fav_status, liked_user)
194 TwitterAPI.unfav(user, status.id)
195 {:ok, dupe} = TwitterAPI.fav(user, status.id)
196 refute Notification.create_notification(dupe, liked_user)
197 end
198
199 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
200 user = insert(:user)
201 retweeted_user = insert(:user)
202
203 {:ok, status} =
204 TwitterAPI.create_status(retweeted_user, %{
205 "status" => "Send dupe notifications to the shadow realm"
206 })
207
208 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
209 Notification.create_notification(retweeted_activity, retweeted_user)
210 TwitterAPI.unrepeat(user, status.id)
211 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
212 refute Notification.create_notification(dupe, retweeted_user)
213 end
214
215 test "it doesn't create duplicate notifications for follow+subscribed users" do
216 user = insert(:user)
217 subscriber = insert(:user)
218
219 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
220 User.subscribe(subscriber, user)
221 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
222 {:ok, [_notif]} = Notification.create_notifications(status)
223 end
224
225 test "it doesn't create subscription notifications if the recipient cannot see the status" do
226 user = insert(:user)
227 subscriber = insert(:user)
228
229 User.subscribe(subscriber, user)
230
231 {:ok, status} =
232 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
233
234 assert {:ok, []} == Notification.create_notifications(status)
235 end
236 end
237
238 describe "get notification" do
239 test "it gets a notification that belongs to the user" do
240 user = insert(:user)
241 other_user = insert(:user)
242
243 {:ok, activity} =
244 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
245
246 {:ok, [notification]} = Notification.create_notifications(activity)
247 {:ok, notification} = Notification.get(other_user, notification.id)
248
249 assert notification.user_id == other_user.id
250 end
251
252 test "it returns error if the notification doesn't belong to the user" do
253 user = insert(:user)
254 other_user = insert(:user)
255
256 {:ok, activity} =
257 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
258
259 {:ok, [notification]} = Notification.create_notifications(activity)
260 {:error, _notification} = Notification.get(user, notification.id)
261 end
262 end
263
264 describe "dismiss notification" do
265 test "it dismisses a notification that belongs to the user" do
266 user = insert(:user)
267 other_user = insert(:user)
268
269 {:ok, activity} =
270 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
271
272 {:ok, [notification]} = Notification.create_notifications(activity)
273 {:ok, notification} = Notification.dismiss(other_user, notification.id)
274
275 assert notification.user_id == other_user.id
276 end
277
278 test "it returns error if the notification doesn't belong to the user" do
279 user = insert(:user)
280 other_user = insert(:user)
281
282 {:ok, activity} =
283 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
284
285 {:ok, [notification]} = Notification.create_notifications(activity)
286 {:error, _notification} = Notification.dismiss(user, notification.id)
287 end
288 end
289
290 describe "clear notification" do
291 test "it clears all notifications belonging to the user" do
292 user = insert(:user)
293 other_user = insert(:user)
294 third_user = insert(:user)
295
296 {:ok, activity} =
297 TwitterAPI.create_status(user, %{
298 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
299 })
300
301 {:ok, _notifs} = Notification.create_notifications(activity)
302
303 {:ok, activity} =
304 TwitterAPI.create_status(user, %{
305 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
306 })
307
308 {:ok, _notifs} = Notification.create_notifications(activity)
309 Notification.clear(other_user)
310
311 assert Notification.for_user(other_user) == []
312 assert Notification.for_user(third_user) != []
313 end
314 end
315
316 describe "set_read_up_to()" do
317 test "it sets all notifications as read up to a specified notification ID" do
318 user = insert(:user)
319 other_user = insert(:user)
320
321 {:ok, _activity} =
322 TwitterAPI.create_status(user, %{
323 "status" => "hey @#{other_user.nickname}!"
324 })
325
326 {:ok, _activity} =
327 TwitterAPI.create_status(user, %{
328 "status" => "hey again @#{other_user.nickname}!"
329 })
330
331 [n2, n1] = notifs = Notification.for_user(other_user)
332 assert length(notifs) == 2
333
334 assert n2.id > n1.id
335
336 {:ok, _activity} =
337 TwitterAPI.create_status(user, %{
338 "status" => "hey yet again @#{other_user.nickname}!"
339 })
340
341 Notification.set_read_up_to(other_user, n2.id)
342
343 [n3, n2, n1] = Notification.for_user(other_user)
344
345 assert n1.seen == true
346 assert n2.seen == true
347 assert n3.seen == false
348 end
349 end
350
351 describe "notification target determination" do
352 test "it sends notifications to addressed users in new messages" do
353 user = insert(:user)
354 other_user = insert(:user)
355
356 {:ok, activity} =
357 CommonAPI.post(user, %{
358 "status" => "hey @#{other_user.nickname}!"
359 })
360
361 assert other_user in Notification.get_notified_from_activity(activity)
362 end
363
364 test "it sends notifications to mentioned users in new messages" do
365 user = insert(:user)
366 other_user = insert(:user)
367
368 create_activity = %{
369 "@context" => "https://www.w3.org/ns/activitystreams",
370 "type" => "Create",
371 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
372 "actor" => user.ap_id,
373 "object" => %{
374 "type" => "Note",
375 "content" => "message with a Mention tag, but no explicit tagging",
376 "tag" => [
377 %{
378 "type" => "Mention",
379 "href" => other_user.ap_id,
380 "name" => other_user.nickname
381 }
382 ],
383 "attributedTo" => user.ap_id
384 }
385 }
386
387 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
388
389 assert other_user in Notification.get_notified_from_activity(activity)
390 end
391
392 test "it does not send notifications to users who are only cc in new messages" do
393 user = insert(:user)
394 other_user = insert(:user)
395
396 create_activity = %{
397 "@context" => "https://www.w3.org/ns/activitystreams",
398 "type" => "Create",
399 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
400 "cc" => [other_user.ap_id],
401 "actor" => user.ap_id,
402 "object" => %{
403 "type" => "Note",
404 "content" => "hi everyone",
405 "attributedTo" => user.ap_id
406 }
407 }
408
409 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
410
411 assert other_user not in Notification.get_notified_from_activity(activity)
412 end
413
414 test "it does not send notification to mentioned users in likes" do
415 user = insert(:user)
416 other_user = insert(:user)
417 third_user = insert(:user)
418
419 {:ok, activity_one} =
420 CommonAPI.post(user, %{
421 "status" => "hey @#{other_user.nickname}!"
422 })
423
424 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
425
426 assert other_user not in Notification.get_notified_from_activity(activity_two)
427 end
428
429 test "it does not send notification to mentioned users in announces" do
430 user = insert(:user)
431 other_user = insert(:user)
432 third_user = insert(:user)
433
434 {:ok, activity_one} =
435 CommonAPI.post(user, %{
436 "status" => "hey @#{other_user.nickname}!"
437 })
438
439 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
440
441 assert other_user not in Notification.get_notified_from_activity(activity_two)
442 end
443 end
444
445 describe "notification lifecycle" do
446 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
447 user = insert(:user)
448 other_user = insert(:user)
449
450 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
451
452 assert Enum.empty?(Notification.for_user(user))
453
454 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
455
456 assert length(Notification.for_user(user)) == 1
457
458 {:ok, _} = CommonAPI.delete(activity.id, user)
459
460 assert Enum.empty?(Notification.for_user(user))
461 end
462
463 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
464 user = insert(:user)
465 other_user = insert(:user)
466
467 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
468
469 assert Enum.empty?(Notification.for_user(user))
470
471 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
472
473 assert length(Notification.for_user(user)) == 1
474
475 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
476
477 assert Enum.empty?(Notification.for_user(user))
478 end
479
480 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
481 user = insert(:user)
482 other_user = insert(:user)
483
484 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
485
486 assert Enum.empty?(Notification.for_user(user))
487
488 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
489
490 assert length(Notification.for_user(user)) == 1
491
492 {:ok, _} = CommonAPI.delete(activity.id, user)
493
494 assert Enum.empty?(Notification.for_user(user))
495 end
496
497 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
498 user = insert(:user)
499 other_user = insert(:user)
500
501 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
502
503 assert Enum.empty?(Notification.for_user(user))
504
505 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
506
507 assert length(Notification.for_user(user)) == 1
508
509 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
510
511 assert Enum.empty?(Notification.for_user(user))
512 end
513
514 test "liking an activity which is already deleted does not generate a notification" do
515 user = insert(:user)
516 other_user = insert(:user)
517
518 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
519
520 assert Enum.empty?(Notification.for_user(user))
521
522 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
523
524 assert Enum.empty?(Notification.for_user(user))
525
526 {:error, _} = CommonAPI.favorite(activity.id, other_user)
527
528 assert Enum.empty?(Notification.for_user(user))
529 end
530
531 test "repeating an activity which is already deleted does not generate a notification" do
532 user = insert(:user)
533 other_user = insert(:user)
534
535 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
536
537 assert Enum.empty?(Notification.for_user(user))
538
539 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
540
541 assert Enum.empty?(Notification.for_user(user))
542
543 {:error, _} = CommonAPI.repeat(activity.id, other_user)
544
545 assert Enum.empty?(Notification.for_user(user))
546 end
547
548 test "replying to a deleted post without tagging does not generate a notification" do
549 user = insert(:user)
550 other_user = insert(:user)
551
552 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
553 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
554
555 {:ok, _reply_activity} =
556 CommonAPI.post(other_user, %{
557 "status" => "test reply",
558 "in_reply_to_status_id" => activity.id
559 })
560
561 assert Enum.empty?(Notification.for_user(user))
562 end
563 end
564
565 describe "for_user" do
566 test "it returns notifications for muted user without notifications" do
567 user = insert(:user)
568 muted = insert(:user)
569 {:ok, user} = User.mute(user, muted, false)
570
571 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
572
573 assert length(Notification.for_user(user)) == 1
574 end
575
576 test "it doesn't return notifications for muted user with notifications" do
577 user = insert(:user)
578 muted = insert(:user)
579 {:ok, user} = User.mute(user, muted)
580
581 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
582
583 assert Notification.for_user(user) == []
584 end
585
586 test "it doesn't return notifications for blocked user" do
587 user = insert(:user)
588 blocked = insert(:user)
589 {:ok, user} = User.block(user, blocked)
590
591 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
592
593 assert Notification.for_user(user) == []
594 end
595
596 test "it doesn't return notificatitons for blocked domain" do
597 user = insert(:user)
598 blocked = insert(:user, ap_id: "http://some-domain.com")
599 {:ok, user} = User.block_domain(user, "some-domain.com")
600
601 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
602
603 assert Notification.for_user(user) == []
604 end
605
606 test "it doesn't return notifications for muted thread" do
607 user = insert(:user)
608 another_user = insert(:user)
609
610 {:ok, activity} =
611 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
612
613 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
614 assert Notification.for_user(user) == []
615 end
616
617 test "it returns notifications for muted user with notifications and with_muted parameter" do
618 user = insert(:user)
619 muted = insert(:user)
620 {:ok, user} = User.mute(user, muted)
621
622 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
623
624 assert length(Notification.for_user(user, %{with_muted: true})) == 1
625 end
626
627 test "it returns notifications for blocked user and with_muted parameter" do
628 user = insert(:user)
629 blocked = insert(:user)
630 {:ok, user} = User.block(user, blocked)
631
632 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
633
634 assert length(Notification.for_user(user, %{with_muted: true})) == 1
635 end
636
637 test "it returns notificatitons for blocked domain and with_muted parameter" do
638 user = insert(:user)
639 blocked = insert(:user, ap_id: "http://some-domain.com")
640 {:ok, user} = User.block_domain(user, "some-domain.com")
641
642 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
643
644 assert length(Notification.for_user(user, %{with_muted: true})) == 1
645 end
646
647 test "it returns notifications for muted thread with_muted parameter" do
648 user = insert(:user)
649 another_user = insert(:user)
650
651 {:ok, activity} =
652 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
653
654 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
655 assert length(Notification.for_user(user, %{with_muted: true})) == 1
656 end
657 end
658 end