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