1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.NotificationTest do
11 alias Pleroma.FollowingRelationship
12 alias Pleroma.Notification
13 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.MastodonAPI.NotificationView
18 alias Pleroma.Web.Push
19 alias Pleroma.Web.Streamer
21 describe "create_notifications" do
22 test "creates a notification for an emoji reaction" do
24 other_user = insert(:user)
26 {:ok, activity} = CommonAPI.post(user, %{"status" => "yeah"})
27 {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
29 {:ok, [notification]} = Notification.create_notifications(activity)
31 assert notification.user_id == user.id
34 test "notifies someone when they are directly addressed" do
36 other_user = insert(:user)
37 third_user = insert(:user)
40 CommonAPI.post(user, %{
41 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
44 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
46 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
47 assert notified_ids == [other_user.id, third_user.id]
48 assert notification.activity_id == activity.id
49 assert other_notification.activity_id == activity.id
52 test "it creates a notification for subscribed users" do
54 subscriber = insert(:user)
56 User.subscribe(subscriber, user)
58 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
59 {:ok, [notification]} = Notification.create_notifications(status)
61 assert notification.user_id == subscriber.id
64 test "does not create a notification for subscribed users if status is a reply" do
66 other_user = insert(:user)
67 subscriber = insert(:user)
69 User.subscribe(subscriber, other_user)
71 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
73 {:ok, _reply_activity} =
74 CommonAPI.post(other_user, %{
75 "status" => "test reply",
76 "in_reply_to_status_id" => activity.id
79 user_notifications = Notification.for_user(user)
80 assert length(user_notifications) == 1
82 subscriber_notifications = Notification.for_user(subscriber)
83 assert Enum.empty?(subscriber_notifications)
87 describe "CommonApi.post/2 notification-related functionality" do
88 test_with_mock "creates but does NOT send notification to blocker user",
93 blocker = insert(:user)
94 {:ok, _user_relationship} = User.block(blocker, user)
96 {:ok, _activity} = CommonAPI.post(user, %{"status" => "hey @#{blocker.nickname}!"})
98 blocker_id = blocker.id
99 assert [%Notification{user_id: ^blocker_id}] = Repo.all(Notification)
100 refute called(Push.send(:_))
103 test_with_mock "creates but does NOT send notification to notification-muter user",
108 muter = insert(:user)
109 {:ok, _user_relationships} = User.mute(muter, user)
111 {:ok, _activity} = CommonAPI.post(user, %{"status" => "hey @#{muter.nickname}!"})
114 assert [%Notification{user_id: ^muter_id}] = Repo.all(Notification)
115 refute called(Push.send(:_))
118 test_with_mock "creates but does NOT send notification to thread-muter user",
123 thread_muter = insert(:user)
125 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{thread_muter.nickname}!"})
127 {:ok, _} = CommonAPI.add_mute(thread_muter, activity)
129 {:ok, _same_context_activity} =
130 CommonAPI.post(user, %{
131 "status" => "hey-hey-hey @#{thread_muter.nickname}!",
132 "in_reply_to_status_id" => activity.id
135 [pre_mute_notification, post_mute_notification] =
136 Repo.all(from(n in Notification, where: n.user_id == ^thread_muter.id, order_by: n.id))
138 pre_mute_notification_id = pre_mute_notification.id
139 post_mute_notification_id = post_mute_notification.id
144 %Notification{id: ^pre_mute_notification_id} -> true
153 %Notification{id: ^post_mute_notification_id} -> true
161 describe "create_notification" do
162 @tag needs_streamer: true
163 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
168 Streamer.add_socket("user", user)
169 assert_receive {:render_with_user, _, _, _}, 4_000
172 task_user_notification =
174 Streamer.add_socket("user:notification", user)
175 assert_receive {:render_with_user, _, _, _}, 4_000
178 activity = insert(:note_activity)
180 notify = Notification.create_notification(activity, user)
181 assert notify.user_id == user.id
183 Task.await(task_user_notification)
186 test "it creates a notification for user if the user blocks the activity author" do
187 activity = insert(:note_activity)
188 author = User.get_cached_by_ap_id(activity.data["actor"])
190 {:ok, _user_relationship} = User.block(user, author)
192 assert Notification.create_notification(activity, user)
195 test "it creates a notification for the user if the user mutes the activity author" do
196 muter = insert(:user)
197 muted = insert(:user)
198 {:ok, _} = User.mute(muter, muted)
199 muter = Repo.get(User, muter.id)
200 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
202 assert Notification.create_notification(activity, muter)
205 test "notification created if user is muted without notifications" do
206 muter = insert(:user)
207 muted = insert(:user)
209 {:ok, _user_relationships} = User.mute(muter, muted, false)
211 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
213 assert Notification.create_notification(activity, muter)
216 test "it creates a notification for an activity from a muted thread" do
217 muter = insert(:user)
218 other_user = insert(:user)
219 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
220 CommonAPI.add_mute(muter, activity)
223 CommonAPI.post(other_user, %{
224 "status" => "Hi @#{muter.nickname}",
225 "in_reply_to_status_id" => activity.id
228 assert Notification.create_notification(activity, muter)
231 test "it disables notifications from followers" do
232 follower = insert(:user)
235 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
237 User.follow(follower, followed)
238 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
239 refute Notification.create_notification(activity, followed)
242 test "it disables notifications from non-followers" do
243 follower = insert(:user)
247 notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
250 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
251 refute Notification.create_notification(activity, followed)
254 test "it disables notifications from people the user follows" do
256 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
258 followed = insert(:user)
259 User.follow(follower, followed)
260 follower = Repo.get(User, follower.id)
261 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
262 refute Notification.create_notification(activity, follower)
265 test "it disables notifications from people the user does not follow" do
267 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
269 followed = insert(:user)
270 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
271 refute Notification.create_notification(activity, follower)
274 test "it doesn't create a notification for user if he is the activity author" do
275 activity = insert(:note_activity)
276 author = User.get_cached_by_ap_id(activity.data["actor"])
278 refute Notification.create_notification(activity, author)
281 test "it doesn't create duplicate notifications for follow+subscribed users" do
283 subscriber = insert(:user)
285 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
286 User.subscribe(subscriber, user)
287 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
288 {:ok, [_notif]} = Notification.create_notifications(status)
291 test "it doesn't create subscription notifications if the recipient cannot see the status" do
293 subscriber = insert(:user)
295 User.subscribe(subscriber, user)
297 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
299 assert {:ok, []} == Notification.create_notifications(status)
303 describe "follow / follow_request notifications" do
304 test "it creates `follow` notification for approved Follow activity" do
306 followed_user = insert(:user, locked: false)
308 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
309 assert FollowingRelationship.following?(user, followed_user)
310 assert [notification] = Notification.for_user(followed_user)
312 assert %{type: "follow"} =
313 NotificationView.render("show.json", %{
314 notification: notification,
319 test "it creates `follow_request` notification for pending Follow activity" do
321 followed_user = insert(:user, locked: true)
323 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
324 refute FollowingRelationship.following?(user, followed_user)
325 assert [notification] = Notification.for_user(followed_user)
327 render_opts = %{notification: notification, for: followed_user}
328 assert %{type: "follow_request"} = NotificationView.render("show.json", render_opts)
330 # After request is accepted, the same notification is rendered with type "follow":
331 assert {:ok, _} = CommonAPI.accept_follow_request(user, followed_user)
333 notification_id = notification.id
334 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
335 assert %{type: "follow"} = NotificationView.render("show.json", render_opts)
338 test "it doesn't create a notification for follow-unfollow-follow chains" do
340 followed_user = insert(:user, locked: false)
342 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
343 assert FollowingRelationship.following?(user, followed_user)
344 assert [notification] = Notification.for_user(followed_user)
346 CommonAPI.unfollow(user, followed_user)
347 {:ok, _, _, _activity_dupe} = CommonAPI.follow(user, followed_user)
349 notification_id = notification.id
350 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
353 test "dismisses the notification on follow request rejection" do
354 user = insert(:user, locked: true)
355 follower = insert(:user)
356 {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, user)
357 assert [notification] = Notification.for_user(user)
358 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
359 assert [] = Notification.for_user(user)
363 describe "get notification" do
364 test "it gets a notification that belongs to the user" do
366 other_user = insert(:user)
368 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
370 {:ok, [notification]} = Notification.create_notifications(activity)
371 {:ok, notification} = Notification.get(other_user, notification.id)
373 assert notification.user_id == other_user.id
376 test "it returns error if the notification doesn't belong to the user" do
378 other_user = insert(:user)
380 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
382 {:ok, [notification]} = Notification.create_notifications(activity)
383 {:error, _notification} = Notification.get(user, notification.id)
387 describe "dismiss notification" do
388 test "it dismisses a notification that belongs to the user" do
390 other_user = insert(:user)
392 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
394 {:ok, [notification]} = Notification.create_notifications(activity)
395 {:ok, notification} = Notification.dismiss(other_user, notification.id)
397 assert notification.user_id == other_user.id
400 test "it returns error if the notification doesn't belong to the user" do
402 other_user = insert(:user)
404 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
406 {:ok, [notification]} = Notification.create_notifications(activity)
407 {:error, _notification} = Notification.dismiss(user, notification.id)
411 describe "clear notification" do
412 test "it clears all notifications belonging to the user" do
414 other_user = insert(:user)
415 third_user = insert(:user)
418 CommonAPI.post(user, %{
419 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
422 {:ok, _notifs} = Notification.create_notifications(activity)
425 CommonAPI.post(user, %{
426 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
429 {:ok, _notifs} = Notification.create_notifications(activity)
430 Notification.clear(other_user)
432 assert Notification.for_user(other_user) == []
433 assert Notification.for_user(third_user) != []
437 describe "set_read_up_to()" do
438 test "it sets all notifications as read up to a specified notification ID" do
440 other_user = insert(:user)
443 CommonAPI.post(user, %{
444 "status" => "hey @#{other_user.nickname}!"
448 CommonAPI.post(user, %{
449 "status" => "hey again @#{other_user.nickname}!"
452 [n2, n1] = notifs = Notification.for_user(other_user)
453 assert length(notifs) == 2
458 CommonAPI.post(user, %{
459 "status" => "hey yet again @#{other_user.nickname}!"
462 Notification.set_read_up_to(other_user, n2.id)
464 [n3, n2, n1] = Notification.for_user(other_user)
466 assert n1.seen == true
467 assert n2.seen == true
468 assert n3.seen == false
472 describe "for_user_since/2" do
473 defp days_ago(days) do
475 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
476 -days * 60 * 60 * 24,
481 test "Returns recent notifications" do
482 user1 = insert(:user)
483 user2 = insert(:user)
485 Enum.each(0..10, fn i ->
487 CommonAPI.post(user1, %{
488 "status" => "hey ##{i} @#{user2.nickname}!"
492 {old, new} = Enum.split(Notification.for_user(user2), 5)
494 Enum.each(old, fn notification ->
496 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
497 |> Pleroma.Repo.update!()
500 recent_notifications_ids =
502 |> Notification.for_user_since(
503 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
507 Enum.each(old, fn %{id: id} ->
508 refute id in recent_notifications_ids
511 Enum.each(new, fn %{id: id} ->
512 assert id in recent_notifications_ids
517 describe "notification target determination / get_notified_from_activity/2" do
518 test "it sends notifications to addressed users in new messages" do
520 other_user = insert(:user)
523 CommonAPI.post(user, %{
524 "status" => "hey @#{other_user.nickname}!"
527 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
529 assert other_user in enabled_receivers
532 test "it sends notifications to mentioned users in new messages" do
534 other_user = insert(:user)
537 "@context" => "https://www.w3.org/ns/activitystreams",
539 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
540 "actor" => user.ap_id,
543 "content" => "message with a Mention tag, but no explicit tagging",
547 "href" => other_user.ap_id,
548 "name" => other_user.nickname
551 "attributedTo" => user.ap_id
555 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
557 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
559 assert other_user in enabled_receivers
562 test "it does not send notifications to users who are only cc in new messages" do
564 other_user = insert(:user)
567 "@context" => "https://www.w3.org/ns/activitystreams",
569 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
570 "cc" => [other_user.ap_id],
571 "actor" => user.ap_id,
574 "content" => "hi everyone",
575 "attributedTo" => user.ap_id
579 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
581 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
583 assert other_user not in enabled_receivers
586 test "it does not send notification to mentioned users in likes" do
588 other_user = insert(:user)
589 third_user = insert(:user)
591 {:ok, activity_one} =
592 CommonAPI.post(user, %{
593 "status" => "hey @#{other_user.nickname}!"
596 {:ok, activity_two} = CommonAPI.favorite(third_user, activity_one.id)
598 {enabled_receivers, _disabled_receivers} =
599 Notification.get_notified_from_activity(activity_two)
601 assert other_user not in enabled_receivers
604 test "it does not send notification to mentioned users in announces" do
606 other_user = insert(:user)
607 third_user = insert(:user)
609 {:ok, activity_one} =
610 CommonAPI.post(user, %{
611 "status" => "hey @#{other_user.nickname}!"
614 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
616 {enabled_receivers, _disabled_receivers} =
617 Notification.get_notified_from_activity(activity_two)
619 assert other_user not in enabled_receivers
622 test "it returns blocking recipient in disabled recipients list" do
624 other_user = insert(:user)
625 {:ok, _user_relationship} = User.block(other_user, user)
627 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
629 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
631 assert [] == enabled_receivers
632 assert [other_user] == disabled_receivers
635 test "it returns notification-muting recipient in disabled recipients list" do
637 other_user = insert(:user)
638 {:ok, _user_relationships} = User.mute(other_user, user)
640 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
642 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
644 assert [] == enabled_receivers
645 assert [other_user] == disabled_receivers
648 test "it returns thread-muting recipient in disabled recipients list" do
650 other_user = insert(:user)
652 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
654 {:ok, _} = CommonAPI.add_mute(other_user, activity)
656 {:ok, same_context_activity} =
657 CommonAPI.post(user, %{
658 "status" => "hey-hey-hey @#{other_user.nickname}!",
659 "in_reply_to_status_id" => activity.id
662 {enabled_receivers, disabled_receivers} =
663 Notification.get_notified_from_activity(same_context_activity)
665 assert [other_user] == disabled_receivers
666 refute other_user in enabled_receivers
669 test "it returns non-following domain-blocking recipient in disabled recipients list" do
670 blocked_domain = "blocked.domain"
671 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
672 other_user = insert(:user)
674 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
676 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
678 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
680 assert [] == enabled_receivers
681 assert [other_user] == disabled_receivers
684 test "it returns following domain-blocking recipient in enabled recipients list" do
685 blocked_domain = "blocked.domain"
686 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
687 other_user = insert(:user)
689 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
690 {:ok, other_user} = User.follow(other_user, user)
692 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
694 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
696 assert [other_user] == enabled_receivers
697 assert [] == disabled_receivers
701 describe "notification lifecycle" do
702 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
704 other_user = insert(:user)
706 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
708 assert Enum.empty?(Notification.for_user(user))
710 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
712 assert length(Notification.for_user(user)) == 1
714 {:ok, _} = CommonAPI.delete(activity.id, user)
716 assert Enum.empty?(Notification.for_user(user))
719 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
721 other_user = insert(:user)
723 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
725 assert Enum.empty?(Notification.for_user(user))
727 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
729 assert length(Notification.for_user(user)) == 1
731 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
733 assert Enum.empty?(Notification.for_user(user))
736 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
738 other_user = insert(:user)
740 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
742 assert Enum.empty?(Notification.for_user(user))
744 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
746 assert length(Notification.for_user(user)) == 1
748 {:ok, _} = CommonAPI.delete(activity.id, user)
750 assert Enum.empty?(Notification.for_user(user))
753 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
755 other_user = insert(:user)
757 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
759 assert Enum.empty?(Notification.for_user(user))
761 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
763 assert length(Notification.for_user(user)) == 1
765 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
767 assert Enum.empty?(Notification.for_user(user))
770 test "liking an activity which is already deleted does not generate a notification" do
772 other_user = insert(:user)
774 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
776 assert Enum.empty?(Notification.for_user(user))
778 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
780 assert Enum.empty?(Notification.for_user(user))
782 {:error, :not_found} = CommonAPI.favorite(other_user, activity.id)
784 assert Enum.empty?(Notification.for_user(user))
787 test "repeating an activity which is already deleted does not generate a notification" do
789 other_user = insert(:user)
791 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
793 assert Enum.empty?(Notification.for_user(user))
795 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
797 assert Enum.empty?(Notification.for_user(user))
799 {:error, _} = CommonAPI.repeat(activity.id, other_user)
801 assert Enum.empty?(Notification.for_user(user))
804 test "replying to a deleted post without tagging does not generate a notification" do
806 other_user = insert(:user)
808 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
809 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
811 {:ok, _reply_activity} =
812 CommonAPI.post(other_user, %{
813 "status" => "test reply",
814 "in_reply_to_status_id" => activity.id
817 assert Enum.empty?(Notification.for_user(user))
820 test "notifications are deleted if a local user is deleted" do
822 other_user = insert(:user)
825 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
827 refute Enum.empty?(Notification.for_user(other_user))
829 {:ok, job} = User.delete(user)
830 ObanHelpers.perform(job)
832 assert Enum.empty?(Notification.for_user(other_user))
835 test "notifications are deleted if a remote user is deleted" do
836 remote_user = insert(:user)
837 local_user = insert(:user)
840 "@context" => "https://www.w3.org/ns/activitystreams",
842 "actor" => remote_user.ap_id,
843 "id" => remote_user.ap_id <> "/activities/test",
844 "to" => [local_user.ap_id],
848 "content" => "Hello!",
852 "href" => local_user.ap_id,
853 "name" => "@#{local_user.nickname}"
856 "to" => [local_user.ap_id],
858 "attributedTo" => remote_user.ap_id
862 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
864 refute Enum.empty?(Notification.for_user(local_user))
866 delete_user_message = %{
867 "@context" => "https://www.w3.org/ns/activitystreams",
868 "id" => remote_user.ap_id <> "/activities/delete",
869 "actor" => remote_user.ap_id,
871 "object" => remote_user.ap_id
874 remote_user_url = remote_user.ap_id
877 %{method: :get, url: ^remote_user_url} ->
878 %Tesla.Env{status: 404, body: ""}
881 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
882 ObanHelpers.perform_all()
884 assert Enum.empty?(Notification.for_user(local_user))
887 @tag capture_log: true
888 test "move activity generates a notification" do
889 %{ap_id: old_ap_id} = old_user = insert(:user)
890 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
891 follower = insert(:user)
892 other_follower = insert(:user, %{allow_following_move: false})
894 User.follow(follower, old_user)
895 User.follow(other_follower, old_user)
897 old_user_url = old_user.ap_id
900 File.read!("test/fixtures/users_mock/localhost.json")
901 |> String.replace("{{nickname}}", old_user.nickname)
905 %{method: :get, url: ^old_user_url} ->
906 %Tesla.Env{status: 200, body: body}
909 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
910 ObanHelpers.perform_all()
915 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
918 ] = Notification.for_user(follower)
923 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
926 ] = Notification.for_user(other_follower)
930 describe "for_user" do
931 test "it returns notifications for muted user without notifications" do
933 muted = insert(:user)
934 {:ok, _user_relationships} = User.mute(user, muted, false)
936 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
938 assert length(Notification.for_user(user)) == 1
941 test "it doesn't return notifications for muted user with notifications" do
943 muted = insert(:user)
944 {:ok, _user_relationships} = User.mute(user, muted)
946 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
948 assert Notification.for_user(user) == []
951 test "it doesn't return notifications for blocked user" do
953 blocked = insert(:user)
954 {:ok, _user_relationship} = User.block(user, blocked)
956 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
958 assert Notification.for_user(user) == []
961 test "it doesn't return notifications for domain-blocked non-followed user" do
963 blocked = insert(:user, ap_id: "http://some-domain.com")
964 {:ok, user} = User.block_domain(user, "some-domain.com")
966 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
968 assert Notification.for_user(user) == []
971 test "it returns notifications for domain-blocked but followed user" do
973 blocked = insert(:user, ap_id: "http://some-domain.com")
975 {:ok, user} = User.block_domain(user, "some-domain.com")
976 {:ok, _} = User.follow(user, blocked)
978 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
980 assert length(Notification.for_user(user)) == 1
983 test "it doesn't return notifications for muted thread" do
985 another_user = insert(:user)
987 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
989 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
990 assert Notification.for_user(user) == []
993 test "it returns notifications from a muted user when with_muted is set" do
995 muted = insert(:user)
996 {:ok, _user_relationships} = User.mute(user, muted)
998 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
1000 assert length(Notification.for_user(user, %{with_muted: true})) == 1
1003 test "it doesn't return notifications from a blocked user when with_muted is set" do
1004 user = insert(:user)
1005 blocked = insert(:user)
1006 {:ok, _user_relationship} = User.block(user, blocked)
1008 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1010 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1013 test "when with_muted is set, " <>
1014 "it doesn't return notifications from a domain-blocked non-followed user" do
1015 user = insert(:user)
1016 blocked = insert(:user, ap_id: "http://some-domain.com")
1017 {:ok, user} = User.block_domain(user, "some-domain.com")
1019 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1021 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1024 test "it returns notifications from muted threads when with_muted is set" do
1025 user = insert(:user)
1026 another_user = insert(:user)
1028 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
1030 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
1031 assert length(Notification.for_user(user, %{with_muted: true})) == 1