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