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