caa9419348d2810d40a1fa6fa8df634ee1958033
[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(third_user, activity_one.id)
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
613 test "it returns domain-blocking recipient in disabled recipients list" do
614 blocked_domain = "blocked.domain"
615 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
616 other_user = insert(:user)
617
618 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
619
620 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
621
622 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
623
624 assert [] == enabled_receivers
625 assert [other_user] == disabled_receivers
626 end
627 end
628
629 describe "notification lifecycle" do
630 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
631 user = insert(:user)
632 other_user = insert(:user)
633
634 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
635
636 assert Enum.empty?(Notification.for_user(user))
637
638 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
639
640 assert length(Notification.for_user(user)) == 1
641
642 {:ok, _} = CommonAPI.delete(activity.id, user)
643
644 assert Enum.empty?(Notification.for_user(user))
645 end
646
647 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
648 user = insert(:user)
649 other_user = insert(:user)
650
651 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
652
653 assert Enum.empty?(Notification.for_user(user))
654
655 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
656
657 assert length(Notification.for_user(user)) == 1
658
659 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
660
661 assert Enum.empty?(Notification.for_user(user))
662 end
663
664 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
665 user = insert(:user)
666 other_user = insert(:user)
667
668 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
669
670 assert Enum.empty?(Notification.for_user(user))
671
672 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
673
674 assert length(Notification.for_user(user)) == 1
675
676 {:ok, _} = CommonAPI.delete(activity.id, user)
677
678 assert Enum.empty?(Notification.for_user(user))
679 end
680
681 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
682 user = insert(:user)
683 other_user = insert(:user)
684
685 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
686
687 assert Enum.empty?(Notification.for_user(user))
688
689 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
690
691 assert length(Notification.for_user(user)) == 1
692
693 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
694
695 assert Enum.empty?(Notification.for_user(user))
696 end
697
698 test "liking an activity which is already deleted does not generate a notification" do
699 user = insert(:user)
700 other_user = insert(:user)
701
702 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
703
704 assert Enum.empty?(Notification.for_user(user))
705
706 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
707
708 assert Enum.empty?(Notification.for_user(user))
709
710 {:error, :not_found} = CommonAPI.favorite(other_user, activity.id)
711
712 assert Enum.empty?(Notification.for_user(user))
713 end
714
715 test "repeating an activity which is already deleted does not generate a notification" do
716 user = insert(:user)
717 other_user = insert(:user)
718
719 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
720
721 assert Enum.empty?(Notification.for_user(user))
722
723 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
724
725 assert Enum.empty?(Notification.for_user(user))
726
727 {:error, _} = CommonAPI.repeat(activity.id, other_user)
728
729 assert Enum.empty?(Notification.for_user(user))
730 end
731
732 test "replying to a deleted post without tagging does not generate a notification" do
733 user = insert(:user)
734 other_user = insert(:user)
735
736 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
737 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
738
739 {:ok, _reply_activity} =
740 CommonAPI.post(other_user, %{
741 "status" => "test reply",
742 "in_reply_to_status_id" => activity.id
743 })
744
745 assert Enum.empty?(Notification.for_user(user))
746 end
747
748 test "notifications are deleted if a local user is deleted" do
749 user = insert(:user)
750 other_user = insert(:user)
751
752 {:ok, _activity} =
753 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
754
755 refute Enum.empty?(Notification.for_user(other_user))
756
757 {:ok, job} = User.delete(user)
758 ObanHelpers.perform(job)
759
760 assert Enum.empty?(Notification.for_user(other_user))
761 end
762
763 test "notifications are deleted if a remote user is deleted" do
764 remote_user = insert(:user)
765 local_user = insert(:user)
766
767 dm_message = %{
768 "@context" => "https://www.w3.org/ns/activitystreams",
769 "type" => "Create",
770 "actor" => remote_user.ap_id,
771 "id" => remote_user.ap_id <> "/activities/test",
772 "to" => [local_user.ap_id],
773 "cc" => [],
774 "object" => %{
775 "type" => "Note",
776 "content" => "Hello!",
777 "tag" => [
778 %{
779 "type" => "Mention",
780 "href" => local_user.ap_id,
781 "name" => "@#{local_user.nickname}"
782 }
783 ],
784 "to" => [local_user.ap_id],
785 "cc" => [],
786 "attributedTo" => remote_user.ap_id
787 }
788 }
789
790 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
791
792 refute Enum.empty?(Notification.for_user(local_user))
793
794 delete_user_message = %{
795 "@context" => "https://www.w3.org/ns/activitystreams",
796 "id" => remote_user.ap_id <> "/activities/delete",
797 "actor" => remote_user.ap_id,
798 "type" => "Delete",
799 "object" => remote_user.ap_id
800 }
801
802 remote_user_url = remote_user.ap_id
803
804 Tesla.Mock.mock(fn
805 %{method: :get, url: ^remote_user_url} ->
806 %Tesla.Env{status: 404, body: ""}
807 end)
808
809 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
810 ObanHelpers.perform_all()
811
812 assert Enum.empty?(Notification.for_user(local_user))
813 end
814
815 @tag capture_log: true
816 test "move activity generates a notification" do
817 %{ap_id: old_ap_id} = old_user = insert(:user)
818 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
819 follower = insert(:user)
820 other_follower = insert(:user, %{allow_following_move: false})
821
822 User.follow(follower, old_user)
823 User.follow(other_follower, old_user)
824
825 old_user_url = old_user.ap_id
826
827 body =
828 File.read!("test/fixtures/users_mock/localhost.json")
829 |> String.replace("{{nickname}}", old_user.nickname)
830 |> Jason.encode!()
831
832 Tesla.Mock.mock(fn
833 %{method: :get, url: ^old_user_url} ->
834 %Tesla.Env{status: 200, body: body}
835 end)
836
837 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
838 ObanHelpers.perform_all()
839
840 assert [
841 %{
842 activity: %{
843 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
844 }
845 }
846 ] = Notification.for_user(follower)
847
848 assert [
849 %{
850 activity: %{
851 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
852 }
853 }
854 ] = Notification.for_user(other_follower)
855 end
856 end
857
858 describe "for_user" do
859 test "it returns notifications for muted user without notifications" do
860 user = insert(:user)
861 muted = insert(:user)
862 {:ok, _user_relationships} = User.mute(user, muted, false)
863
864 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
865
866 assert length(Notification.for_user(user)) == 1
867 end
868
869 test "it doesn't return notifications for muted user with notifications" do
870 user = insert(:user)
871 muted = insert(:user)
872 {:ok, _user_relationships} = User.mute(user, muted)
873
874 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
875
876 assert Notification.for_user(user) == []
877 end
878
879 test "it doesn't return notifications for blocked user" do
880 user = insert(:user)
881 blocked = insert(:user)
882 {:ok, _user_relationship} = User.block(user, blocked)
883
884 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
885
886 assert Notification.for_user(user) == []
887 end
888
889 test "it doesn't return notifications for blocked domain" do
890 user = insert(:user)
891 blocked = insert(:user, ap_id: "http://some-domain.com")
892 {:ok, user} = User.block_domain(user, "some-domain.com")
893
894 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
895
896 assert Notification.for_user(user) == []
897 end
898
899 test "it doesn't return notifications for muted thread" do
900 user = insert(:user)
901 another_user = insert(:user)
902
903 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
904
905 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
906 assert Notification.for_user(user) == []
907 end
908
909 test "it returns notifications from a muted user when with_muted is set" do
910 user = insert(:user)
911 muted = insert(:user)
912 {:ok, _user_relationships} = User.mute(user, muted)
913
914 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
915
916 assert length(Notification.for_user(user, %{with_muted: true})) == 1
917 end
918
919 test "it doesn't return notifications from a blocked user when with_muted is set" do
920 user = insert(:user)
921 blocked = insert(:user)
922 {:ok, _user_relationship} = User.block(user, blocked)
923
924 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
925
926 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
927 end
928
929 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
930 user = insert(:user)
931 blocked = insert(:user, ap_id: "http://some-domain.com")
932 {:ok, user} = User.block_domain(user, "some-domain.com")
933
934 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
935
936 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
937 end
938
939 test "it returns notifications from muted threads when with_muted is set" do
940 user = insert(:user)
941 another_user = insert(:user)
942
943 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
944
945 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
946 assert length(Notification.for_user(user, %{with_muted: true})) == 1
947 end
948 end
949 end