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