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