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
165 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
166 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
167 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
171 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
174 activity = insert(:note_activity)
176 notify = Notification.create_notification(activity, user)
177 assert notify.user_id == user.id
179 Task.await(task_user_notification)
182 test "it creates a notification for user if the user blocks the activity author" do
183 activity = insert(:note_activity)
184 author = User.get_cached_by_ap_id(activity.data["actor"])
186 {:ok, _user_relationship} = User.block(user, author)
188 assert Notification.create_notification(activity, user)
191 test "it creates a notification for the user if the user mutes the activity author" do
192 muter = insert(:user)
193 muted = insert(:user)
194 {:ok, _} = User.mute(muter, muted)
195 muter = Repo.get(User, muter.id)
196 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
198 assert Notification.create_notification(activity, muter)
201 test "notification created if user is muted without notifications" do
202 muter = insert(:user)
203 muted = insert(:user)
205 {:ok, _user_relationships} = User.mute(muter, muted, false)
207 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
209 assert Notification.create_notification(activity, muter)
212 test "it creates a notification for an activity from a muted thread" do
213 muter = insert(:user)
214 other_user = insert(:user)
215 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
216 CommonAPI.add_mute(muter, activity)
219 CommonAPI.post(other_user, %{
220 "status" => "Hi @#{muter.nickname}",
221 "in_reply_to_status_id" => activity.id
224 assert Notification.create_notification(activity, muter)
227 test "it disables notifications from followers" do
228 follower = insert(:user)
231 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
233 User.follow(follower, followed)
234 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
235 refute Notification.create_notification(activity, followed)
238 test "it disables notifications from non-followers" do
239 follower = insert(:user)
243 notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
246 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
247 refute Notification.create_notification(activity, followed)
250 test "it disables notifications from people the user follows" do
252 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
254 followed = insert(:user)
255 User.follow(follower, followed)
256 follower = Repo.get(User, follower.id)
257 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
258 refute Notification.create_notification(activity, follower)
261 test "it disables notifications from people the user does not follow" do
263 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
265 followed = insert(:user)
266 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
267 refute Notification.create_notification(activity, follower)
270 test "it doesn't create a notification for user if he is the activity author" do
271 activity = insert(:note_activity)
272 author = User.get_cached_by_ap_id(activity.data["actor"])
274 refute Notification.create_notification(activity, author)
277 test "it doesn't create duplicate notifications for follow+subscribed users" do
279 subscriber = insert(:user)
281 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
282 User.subscribe(subscriber, user)
283 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
284 {:ok, [_notif]} = Notification.create_notifications(status)
287 test "it doesn't create subscription notifications if the recipient cannot see the status" do
289 subscriber = insert(:user)
291 User.subscribe(subscriber, user)
293 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
295 assert {:ok, []} == Notification.create_notifications(status)
299 describe "follow / follow_request notifications" do
300 test "it creates `follow` notification for approved Follow activity" do
302 followed_user = insert(:user, locked: false)
304 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
305 assert FollowingRelationship.following?(user, followed_user)
306 assert [notification] = Notification.for_user(followed_user)
308 assert %{type: "follow"} =
309 NotificationView.render("show.json", %{
310 notification: notification,
315 test "it creates `follow_request` notification for pending Follow activity" do
317 followed_user = insert(:user, locked: true)
319 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
320 refute FollowingRelationship.following?(user, followed_user)
321 assert [notification] = Notification.for_user(followed_user)
323 render_opts = %{notification: notification, for: followed_user}
324 assert %{type: "follow_request"} = NotificationView.render("show.json", render_opts)
326 # After request is accepted, the same notification is rendered with type "follow":
327 assert {:ok, _} = CommonAPI.accept_follow_request(user, followed_user)
329 notification_id = notification.id
330 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
331 assert %{type: "follow"} = NotificationView.render("show.json", render_opts)
334 test "it doesn't create a notification for follow-unfollow-follow chains" do
336 followed_user = insert(:user, locked: false)
338 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
339 assert FollowingRelationship.following?(user, followed_user)
340 assert [notification] = Notification.for_user(followed_user)
342 CommonAPI.unfollow(user, followed_user)
343 {:ok, _, _, _activity_dupe} = CommonAPI.follow(user, followed_user)
345 notification_id = notification.id
346 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
349 test "dismisses the notification on follow request rejection" do
350 user = insert(:user, locked: true)
351 follower = insert(:user)
352 {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, user)
353 assert [notification] = Notification.for_user(user)
354 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
355 assert [] = Notification.for_user(user)
359 describe "get notification" do
360 test "it gets a notification that belongs to the user" do
362 other_user = insert(:user)
364 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
366 {:ok, [notification]} = Notification.create_notifications(activity)
367 {:ok, notification} = Notification.get(other_user, notification.id)
369 assert notification.user_id == other_user.id
372 test "it returns error if the notification doesn't belong to the user" do
374 other_user = insert(:user)
376 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
378 {:ok, [notification]} = Notification.create_notifications(activity)
379 {:error, _notification} = Notification.get(user, notification.id)
383 describe "dismiss notification" do
384 test "it dismisses a notification that belongs to the user" do
386 other_user = insert(:user)
388 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
390 {:ok, [notification]} = Notification.create_notifications(activity)
391 {:ok, notification} = Notification.dismiss(other_user, notification.id)
393 assert notification.user_id == other_user.id
396 test "it returns error if the notification doesn't belong to the user" do
398 other_user = insert(:user)
400 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
402 {:ok, [notification]} = Notification.create_notifications(activity)
403 {:error, _notification} = Notification.dismiss(user, notification.id)
407 describe "clear notification" do
408 test "it clears all notifications belonging to the user" do
410 other_user = insert(:user)
411 third_user = insert(:user)
414 CommonAPI.post(user, %{
415 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
418 {:ok, _notifs} = Notification.create_notifications(activity)
421 CommonAPI.post(user, %{
422 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
425 {:ok, _notifs} = Notification.create_notifications(activity)
426 Notification.clear(other_user)
428 assert Notification.for_user(other_user) == []
429 assert Notification.for_user(third_user) != []
433 describe "set_read_up_to()" do
434 test "it sets all notifications as read up to a specified notification ID" do
436 other_user = insert(:user)
439 CommonAPI.post(user, %{
440 "status" => "hey @#{other_user.nickname}!"
444 CommonAPI.post(user, %{
445 "status" => "hey again @#{other_user.nickname}!"
448 [n2, n1] = notifs = Notification.for_user(other_user)
449 assert length(notifs) == 2
454 CommonAPI.post(user, %{
455 "status" => "hey yet again @#{other_user.nickname}!"
458 Notification.set_read_up_to(other_user, n2.id)
460 [n3, n2, n1] = Notification.for_user(other_user)
462 assert n1.seen == true
463 assert n2.seen == true
464 assert n3.seen == false
468 describe "for_user_since/2" do
469 defp days_ago(days) do
471 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
472 -days * 60 * 60 * 24,
477 test "Returns recent notifications" do
478 user1 = insert(:user)
479 user2 = insert(:user)
481 Enum.each(0..10, fn i ->
483 CommonAPI.post(user1, %{
484 "status" => "hey ##{i} @#{user2.nickname}!"
488 {old, new} = Enum.split(Notification.for_user(user2), 5)
490 Enum.each(old, fn notification ->
492 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
493 |> Pleroma.Repo.update!()
496 recent_notifications_ids =
498 |> Notification.for_user_since(
499 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
503 Enum.each(old, fn %{id: id} ->
504 refute id in recent_notifications_ids
507 Enum.each(new, fn %{id: id} ->
508 assert id in recent_notifications_ids
513 describe "notification target determination / get_notified_from_activity/2" do
514 test "it sends notifications to addressed users in new messages" do
516 other_user = insert(:user)
519 CommonAPI.post(user, %{
520 "status" => "hey @#{other_user.nickname}!"
523 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
525 assert other_user in enabled_receivers
528 test "it sends notifications to mentioned users in new messages" do
530 other_user = insert(:user)
533 "@context" => "https://www.w3.org/ns/activitystreams",
535 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
536 "actor" => user.ap_id,
539 "content" => "message with a Mention tag, but no explicit tagging",
543 "href" => other_user.ap_id,
544 "name" => other_user.nickname
547 "attributedTo" => user.ap_id
551 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
553 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
555 assert other_user in enabled_receivers
558 test "it does not send notifications to users who are only cc in new messages" do
560 other_user = insert(:user)
563 "@context" => "https://www.w3.org/ns/activitystreams",
565 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
566 "cc" => [other_user.ap_id],
567 "actor" => user.ap_id,
570 "content" => "hi everyone",
571 "attributedTo" => user.ap_id
575 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
577 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
579 assert other_user not in enabled_receivers
582 test "it does not send notification to mentioned users in likes" do
584 other_user = insert(:user)
585 third_user = insert(:user)
587 {:ok, activity_one} =
588 CommonAPI.post(user, %{
589 "status" => "hey @#{other_user.nickname}!"
592 {:ok, activity_two} = CommonAPI.favorite(third_user, activity_one.id)
594 {enabled_receivers, _disabled_receivers} =
595 Notification.get_notified_from_activity(activity_two)
597 assert other_user not in enabled_receivers
600 test "it does not send notification to mentioned users in announces" do
602 other_user = insert(:user)
603 third_user = insert(:user)
605 {:ok, activity_one} =
606 CommonAPI.post(user, %{
607 "status" => "hey @#{other_user.nickname}!"
610 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
612 {enabled_receivers, _disabled_receivers} =
613 Notification.get_notified_from_activity(activity_two)
615 assert other_user not in enabled_receivers
618 test "it returns blocking recipient in disabled recipients list" do
620 other_user = insert(:user)
621 {:ok, _user_relationship} = User.block(other_user, user)
623 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
625 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
627 assert [] == enabled_receivers
628 assert [other_user] == disabled_receivers
631 test "it returns notification-muting recipient in disabled recipients list" do
633 other_user = insert(:user)
634 {:ok, _user_relationships} = User.mute(other_user, user)
636 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
638 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
640 assert [] == enabled_receivers
641 assert [other_user] == disabled_receivers
644 test "it returns thread-muting recipient in disabled recipients list" do
646 other_user = insert(:user)
648 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
650 {:ok, _} = CommonAPI.add_mute(other_user, activity)
652 {:ok, same_context_activity} =
653 CommonAPI.post(user, %{
654 "status" => "hey-hey-hey @#{other_user.nickname}!",
655 "in_reply_to_status_id" => activity.id
658 {enabled_receivers, disabled_receivers} =
659 Notification.get_notified_from_activity(same_context_activity)
661 assert [other_user] == disabled_receivers
662 refute other_user in enabled_receivers
665 test "it returns non-following domain-blocking recipient in disabled recipients list" do
666 blocked_domain = "blocked.domain"
667 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
668 other_user = insert(:user)
670 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
672 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
674 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
676 assert [] == enabled_receivers
677 assert [other_user] == disabled_receivers
680 test "it returns following domain-blocking recipient in enabled recipients list" do
681 blocked_domain = "blocked.domain"
682 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
683 other_user = insert(:user)
685 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
686 {:ok, other_user} = User.follow(other_user, user)
688 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
690 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
692 assert [other_user] == enabled_receivers
693 assert [] == disabled_receivers
697 describe "notification lifecycle" do
698 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
700 other_user = insert(:user)
702 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
704 assert Enum.empty?(Notification.for_user(user))
706 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
708 assert length(Notification.for_user(user)) == 1
710 {:ok, _} = CommonAPI.delete(activity.id, user)
712 assert Enum.empty?(Notification.for_user(user))
715 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
717 other_user = insert(:user)
719 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
721 assert Enum.empty?(Notification.for_user(user))
723 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
725 assert length(Notification.for_user(user)) == 1
727 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
729 assert Enum.empty?(Notification.for_user(user))
732 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
734 other_user = insert(:user)
736 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
738 assert Enum.empty?(Notification.for_user(user))
740 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
742 assert length(Notification.for_user(user)) == 1
744 {:ok, _} = CommonAPI.delete(activity.id, user)
746 assert Enum.empty?(Notification.for_user(user))
749 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
751 other_user = insert(:user)
753 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
755 assert Enum.empty?(Notification.for_user(user))
757 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
759 assert length(Notification.for_user(user)) == 1
761 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
763 assert Enum.empty?(Notification.for_user(user))
766 test "liking an activity which is already deleted does not generate a notification" do
768 other_user = insert(:user)
770 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
772 assert Enum.empty?(Notification.for_user(user))
774 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
776 assert Enum.empty?(Notification.for_user(user))
778 {:error, :not_found} = CommonAPI.favorite(other_user, activity.id)
780 assert Enum.empty?(Notification.for_user(user))
783 test "repeating an activity which is already deleted does not generate a notification" do
785 other_user = insert(:user)
787 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
789 assert Enum.empty?(Notification.for_user(user))
791 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
793 assert Enum.empty?(Notification.for_user(user))
795 {:error, _} = CommonAPI.repeat(activity.id, other_user)
797 assert Enum.empty?(Notification.for_user(user))
800 test "replying to a deleted post without tagging does not generate a notification" do
802 other_user = insert(:user)
804 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
805 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
807 {:ok, _reply_activity} =
808 CommonAPI.post(other_user, %{
809 "status" => "test reply",
810 "in_reply_to_status_id" => activity.id
813 assert Enum.empty?(Notification.for_user(user))
816 test "notifications are deleted if a local user is deleted" do
818 other_user = insert(:user)
821 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
823 refute Enum.empty?(Notification.for_user(other_user))
825 {:ok, job} = User.delete(user)
826 ObanHelpers.perform(job)
828 assert Enum.empty?(Notification.for_user(other_user))
831 test "notifications are deleted if a remote user is deleted" do
832 remote_user = insert(:user)
833 local_user = insert(:user)
836 "@context" => "https://www.w3.org/ns/activitystreams",
838 "actor" => remote_user.ap_id,
839 "id" => remote_user.ap_id <> "/activities/test",
840 "to" => [local_user.ap_id],
844 "content" => "Hello!",
848 "href" => local_user.ap_id,
849 "name" => "@#{local_user.nickname}"
852 "to" => [local_user.ap_id],
854 "attributedTo" => remote_user.ap_id
858 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
860 refute Enum.empty?(Notification.for_user(local_user))
862 delete_user_message = %{
863 "@context" => "https://www.w3.org/ns/activitystreams",
864 "id" => remote_user.ap_id <> "/activities/delete",
865 "actor" => remote_user.ap_id,
867 "object" => remote_user.ap_id
870 remote_user_url = remote_user.ap_id
873 %{method: :get, url: ^remote_user_url} ->
874 %Tesla.Env{status: 404, body: ""}
877 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
878 ObanHelpers.perform_all()
880 assert Enum.empty?(Notification.for_user(local_user))
883 @tag capture_log: true
884 test "move activity generates a notification" do
885 %{ap_id: old_ap_id} = old_user = insert(:user)
886 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
887 follower = insert(:user)
888 other_follower = insert(:user, %{allow_following_move: false})
890 User.follow(follower, old_user)
891 User.follow(other_follower, old_user)
893 old_user_url = old_user.ap_id
896 File.read!("test/fixtures/users_mock/localhost.json")
897 |> String.replace("{{nickname}}", old_user.nickname)
901 %{method: :get, url: ^old_user_url} ->
902 %Tesla.Env{status: 200, body: body}
905 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
906 ObanHelpers.perform_all()
911 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
914 ] = Notification.for_user(follower)
919 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
922 ] = Notification.for_user(other_follower)
926 describe "for_user" do
927 test "it returns notifications for muted user without notifications" do
929 muted = insert(:user)
930 {:ok, _user_relationships} = User.mute(user, muted, false)
932 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
934 assert length(Notification.for_user(user)) == 1
937 test "it doesn't return notifications for muted user with notifications" do
939 muted = insert(:user)
940 {:ok, _user_relationships} = User.mute(user, muted)
942 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
944 assert Notification.for_user(user) == []
947 test "it doesn't return notifications for blocked user" do
949 blocked = insert(:user)
950 {:ok, _user_relationship} = User.block(user, blocked)
952 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
954 assert Notification.for_user(user) == []
957 test "it doesn't return notifications for domain-blocked non-followed user" do
959 blocked = insert(:user, ap_id: "http://some-domain.com")
960 {:ok, user} = User.block_domain(user, "some-domain.com")
962 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
964 assert Notification.for_user(user) == []
967 test "it returns notifications for domain-blocked but followed user" do
969 blocked = insert(:user, ap_id: "http://some-domain.com")
971 {:ok, user} = User.block_domain(user, "some-domain.com")
972 {:ok, _} = User.follow(user, blocked)
974 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
976 assert length(Notification.for_user(user)) == 1
979 test "it doesn't return notifications for muted thread" do
981 another_user = insert(:user)
983 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
985 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
986 assert Notification.for_user(user) == []
989 test "it returns notifications from a muted user when with_muted is set" do
991 muted = insert(:user)
992 {:ok, _user_relationships} = User.mute(user, muted)
994 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
996 assert length(Notification.for_user(user, %{with_muted: true})) == 1
999 test "it doesn't return notifications from a blocked user when with_muted is set" do
1000 user = insert(:user)
1001 blocked = insert(:user)
1002 {:ok, _user_relationship} = User.block(user, blocked)
1004 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1006 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1009 test "when with_muted is set, " <>
1010 "it doesn't return notifications from a domain-blocked non-followed user" do
1011 user = insert(:user)
1012 blocked = insert(:user, ap_id: "http://some-domain.com")
1013 {:ok, user} = User.block_domain(user, "some-domain.com")
1015 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1017 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1020 test "it returns notifications from muted threads when with_muted is set" do
1021 user = insert(:user)
1022 another_user = insert(:user)
1024 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
1026 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
1027 assert length(Notification.for_user(user, %{with_muted: true})) == 1