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 "if `follow_request` notifications are enabled, " <>
316 "it creates `follow_request` notification for pending Follow activity" do
317 clear_config([:notifications, :enable_follow_request_notifications], true)
319 followed_user = insert(:user, locked: true)
321 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
322 refute FollowingRelationship.following?(user, followed_user)
323 assert [notification] = Notification.for_user(followed_user)
325 render_opts = %{notification: notification, for: followed_user}
326 assert %{type: "follow_request"} = NotificationView.render("show.json", render_opts)
328 # After request is accepted, the same notification is rendered with type "follow":
329 assert {:ok, _} = CommonAPI.accept_follow_request(user, followed_user)
331 notification_id = notification.id
332 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
333 assert %{type: "follow"} = NotificationView.render("show.json", render_opts)
336 test "if `follow_request` notifications are disabled, " <>
337 "it does NOT create `follow*` notification for pending Follow activity" do
338 clear_config([:notifications, :enable_follow_request_notifications], false)
340 followed_user = insert(:user, locked: true)
342 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
343 refute FollowingRelationship.following?(user, followed_user)
344 assert [] = Notification.for_user(followed_user)
346 # After request is accepted, no new notifications are generated:
347 assert {:ok, _} = CommonAPI.accept_follow_request(user, followed_user)
348 assert [] = Notification.for_user(followed_user)
351 test "it doesn't create a notification for follow-unfollow-follow chains" do
353 followed_user = insert(:user, locked: false)
355 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
356 assert FollowingRelationship.following?(user, followed_user)
357 assert [notification] = Notification.for_user(followed_user)
359 CommonAPI.unfollow(user, followed_user)
360 {:ok, _, _, _activity_dupe} = CommonAPI.follow(user, followed_user)
362 notification_id = notification.id
363 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
367 describe "get notification" do
368 test "it gets a notification that belongs to the user" do
370 other_user = insert(:user)
372 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
374 {:ok, [notification]} = Notification.create_notifications(activity)
375 {:ok, notification} = Notification.get(other_user, notification.id)
377 assert notification.user_id == other_user.id
380 test "it returns error if the notification doesn't belong to the user" do
382 other_user = insert(:user)
384 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
386 {:ok, [notification]} = Notification.create_notifications(activity)
387 {:error, _notification} = Notification.get(user, notification.id)
391 describe "dismiss notification" do
392 test "it dismisses a notification that belongs to the user" do
394 other_user = insert(:user)
396 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
398 {:ok, [notification]} = Notification.create_notifications(activity)
399 {:ok, notification} = Notification.dismiss(other_user, notification.id)
401 assert notification.user_id == other_user.id
404 test "it returns error if the notification doesn't belong to the user" do
406 other_user = insert(:user)
408 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
410 {:ok, [notification]} = Notification.create_notifications(activity)
411 {:error, _notification} = Notification.dismiss(user, notification.id)
415 describe "clear notification" do
416 test "it clears all notifications belonging to the user" do
418 other_user = insert(:user)
419 third_user = insert(:user)
422 CommonAPI.post(user, %{
423 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
426 {:ok, _notifs} = Notification.create_notifications(activity)
429 CommonAPI.post(user, %{
430 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
433 {:ok, _notifs} = Notification.create_notifications(activity)
434 Notification.clear(other_user)
436 assert Notification.for_user(other_user) == []
437 assert Notification.for_user(third_user) != []
441 describe "set_read_up_to()" do
442 test "it sets all notifications as read up to a specified notification ID" do
444 other_user = insert(:user)
447 CommonAPI.post(user, %{
448 "status" => "hey @#{other_user.nickname}!"
452 CommonAPI.post(user, %{
453 "status" => "hey again @#{other_user.nickname}!"
456 [n2, n1] = notifs = Notification.for_user(other_user)
457 assert length(notifs) == 2
462 CommonAPI.post(user, %{
463 "status" => "hey yet again @#{other_user.nickname}!"
466 Notification.set_read_up_to(other_user, n2.id)
468 [n3, n2, n1] = Notification.for_user(other_user)
470 assert n1.seen == true
471 assert n2.seen == true
472 assert n3.seen == false
476 describe "for_user_since/2" do
477 defp days_ago(days) do
479 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
480 -days * 60 * 60 * 24,
485 test "Returns recent notifications" do
486 user1 = insert(:user)
487 user2 = insert(:user)
489 Enum.each(0..10, fn i ->
491 CommonAPI.post(user1, %{
492 "status" => "hey ##{i} @#{user2.nickname}!"
496 {old, new} = Enum.split(Notification.for_user(user2), 5)
498 Enum.each(old, fn notification ->
500 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
501 |> Pleroma.Repo.update!()
504 recent_notifications_ids =
506 |> Notification.for_user_since(
507 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
511 Enum.each(old, fn %{id: id} ->
512 refute id in recent_notifications_ids
515 Enum.each(new, fn %{id: id} ->
516 assert id in recent_notifications_ids
521 describe "notification target determination / get_notified_from_activity/2" do
522 test "it sends notifications to addressed users in new messages" do
524 other_user = insert(:user)
527 CommonAPI.post(user, %{
528 "status" => "hey @#{other_user.nickname}!"
531 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
533 assert other_user in enabled_receivers
536 test "it sends notifications to mentioned users in new messages" do
538 other_user = insert(:user)
541 "@context" => "https://www.w3.org/ns/activitystreams",
543 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
544 "actor" => user.ap_id,
547 "content" => "message with a Mention tag, but no explicit tagging",
551 "href" => other_user.ap_id,
552 "name" => other_user.nickname
555 "attributedTo" => user.ap_id
559 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
561 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
563 assert other_user in enabled_receivers
566 test "it does not send notifications to users who are only cc in new messages" do
568 other_user = insert(:user)
571 "@context" => "https://www.w3.org/ns/activitystreams",
573 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
574 "cc" => [other_user.ap_id],
575 "actor" => user.ap_id,
578 "content" => "hi everyone",
579 "attributedTo" => user.ap_id
583 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
585 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
587 assert other_user not in enabled_receivers
590 test "it does not send notification to mentioned users in likes" do
592 other_user = insert(:user)
593 third_user = insert(:user)
595 {:ok, activity_one} =
596 CommonAPI.post(user, %{
597 "status" => "hey @#{other_user.nickname}!"
600 {:ok, activity_two} = CommonAPI.favorite(third_user, activity_one.id)
602 {enabled_receivers, _disabled_receivers} =
603 Notification.get_notified_from_activity(activity_two)
605 assert other_user not in enabled_receivers
608 test "it does not send notification to mentioned users in announces" do
610 other_user = insert(:user)
611 third_user = insert(:user)
613 {:ok, activity_one} =
614 CommonAPI.post(user, %{
615 "status" => "hey @#{other_user.nickname}!"
618 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
620 {enabled_receivers, _disabled_receivers} =
621 Notification.get_notified_from_activity(activity_two)
623 assert other_user not in enabled_receivers
626 test "it returns blocking recipient in disabled recipients list" do
628 other_user = insert(:user)
629 {:ok, _user_relationship} = User.block(other_user, user)
631 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
633 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
635 assert [] == enabled_receivers
636 assert [other_user] == disabled_receivers
639 test "it returns notification-muting recipient in disabled recipients list" do
641 other_user = insert(:user)
642 {:ok, _user_relationships} = User.mute(other_user, user)
644 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
646 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
648 assert [] == enabled_receivers
649 assert [other_user] == disabled_receivers
652 test "it returns thread-muting recipient in disabled recipients list" do
654 other_user = insert(:user)
656 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
658 {:ok, _} = CommonAPI.add_mute(other_user, activity)
660 {:ok, same_context_activity} =
661 CommonAPI.post(user, %{
662 "status" => "hey-hey-hey @#{other_user.nickname}!",
663 "in_reply_to_status_id" => activity.id
666 {enabled_receivers, disabled_receivers} =
667 Notification.get_notified_from_activity(same_context_activity)
669 assert [other_user] == disabled_receivers
670 refute other_user in enabled_receivers
674 describe "notification lifecycle" do
675 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
677 other_user = insert(:user)
679 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
681 assert Enum.empty?(Notification.for_user(user))
683 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
685 assert length(Notification.for_user(user)) == 1
687 {:ok, _} = CommonAPI.delete(activity.id, user)
689 assert Enum.empty?(Notification.for_user(user))
692 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
694 other_user = insert(:user)
696 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
698 assert Enum.empty?(Notification.for_user(user))
700 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
702 assert length(Notification.for_user(user)) == 1
704 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
706 assert Enum.empty?(Notification.for_user(user))
709 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
711 other_user = insert(:user)
713 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
715 assert Enum.empty?(Notification.for_user(user))
717 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
719 assert length(Notification.for_user(user)) == 1
721 {:ok, _} = CommonAPI.delete(activity.id, user)
723 assert Enum.empty?(Notification.for_user(user))
726 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
728 other_user = insert(:user)
730 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
732 assert Enum.empty?(Notification.for_user(user))
734 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
736 assert length(Notification.for_user(user)) == 1
738 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
740 assert Enum.empty?(Notification.for_user(user))
743 test "liking an activity which is already deleted does not generate a notification" do
745 other_user = insert(:user)
747 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
749 assert Enum.empty?(Notification.for_user(user))
751 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
753 assert Enum.empty?(Notification.for_user(user))
755 {:error, :not_found} = CommonAPI.favorite(other_user, activity.id)
757 assert Enum.empty?(Notification.for_user(user))
760 test "repeating an activity which is already deleted does not generate a notification" do
762 other_user = insert(:user)
764 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
766 assert Enum.empty?(Notification.for_user(user))
768 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
770 assert Enum.empty?(Notification.for_user(user))
772 {:error, _} = CommonAPI.repeat(activity.id, other_user)
774 assert Enum.empty?(Notification.for_user(user))
777 test "replying to a deleted post without tagging does not generate a notification" do
779 other_user = insert(:user)
781 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
782 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
784 {:ok, _reply_activity} =
785 CommonAPI.post(other_user, %{
786 "status" => "test reply",
787 "in_reply_to_status_id" => activity.id
790 assert Enum.empty?(Notification.for_user(user))
793 test "notifications are deleted if a local user is deleted" do
795 other_user = insert(:user)
798 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
800 refute Enum.empty?(Notification.for_user(other_user))
802 {:ok, job} = User.delete(user)
803 ObanHelpers.perform(job)
805 assert Enum.empty?(Notification.for_user(other_user))
808 test "notifications are deleted if a remote user is deleted" do
809 remote_user = insert(:user)
810 local_user = insert(:user)
813 "@context" => "https://www.w3.org/ns/activitystreams",
815 "actor" => remote_user.ap_id,
816 "id" => remote_user.ap_id <> "/activities/test",
817 "to" => [local_user.ap_id],
821 "content" => "Hello!",
825 "href" => local_user.ap_id,
826 "name" => "@#{local_user.nickname}"
829 "to" => [local_user.ap_id],
831 "attributedTo" => remote_user.ap_id
835 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
837 refute Enum.empty?(Notification.for_user(local_user))
839 delete_user_message = %{
840 "@context" => "https://www.w3.org/ns/activitystreams",
841 "id" => remote_user.ap_id <> "/activities/delete",
842 "actor" => remote_user.ap_id,
844 "object" => remote_user.ap_id
847 remote_user_url = remote_user.ap_id
850 %{method: :get, url: ^remote_user_url} ->
851 %Tesla.Env{status: 404, body: ""}
854 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
855 ObanHelpers.perform_all()
857 assert Enum.empty?(Notification.for_user(local_user))
860 @tag capture_log: true
861 test "move activity generates a notification" do
862 %{ap_id: old_ap_id} = old_user = insert(:user)
863 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
864 follower = insert(:user)
865 other_follower = insert(:user, %{allow_following_move: false})
867 User.follow(follower, old_user)
868 User.follow(other_follower, old_user)
870 old_user_url = old_user.ap_id
873 File.read!("test/fixtures/users_mock/localhost.json")
874 |> String.replace("{{nickname}}", old_user.nickname)
878 %{method: :get, url: ^old_user_url} ->
879 %Tesla.Env{status: 200, body: body}
882 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
883 ObanHelpers.perform_all()
888 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
891 ] = Notification.for_user(follower)
896 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
899 ] = Notification.for_user(other_follower)
903 describe "for_user" do
904 test "it returns notifications for muted user without notifications" do
906 muted = insert(:user)
907 {:ok, _user_relationships} = User.mute(user, muted, false)
909 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
911 assert length(Notification.for_user(user)) == 1
914 test "it doesn't return notifications for muted user with notifications" do
916 muted = insert(:user)
917 {:ok, _user_relationships} = User.mute(user, muted)
919 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
921 assert Notification.for_user(user) == []
924 test "it doesn't return notifications for blocked user" do
926 blocked = insert(:user)
927 {:ok, _user_relationship} = User.block(user, blocked)
929 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
931 assert Notification.for_user(user) == []
934 test "it doesn't return notifications for blocked domain" do
936 blocked = insert(:user, ap_id: "http://some-domain.com")
937 {:ok, user} = User.block_domain(user, "some-domain.com")
939 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
941 assert Notification.for_user(user) == []
944 test "it doesn't return notifications for muted thread" do
946 another_user = insert(:user)
948 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
950 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
951 assert Notification.for_user(user) == []
954 test "it returns notifications from a muted user when with_muted is set" do
956 muted = insert(:user)
957 {:ok, _user_relationships} = User.mute(user, muted)
959 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
961 assert length(Notification.for_user(user, %{with_muted: true})) == 1
964 test "it doesn't return notifications from a blocked user when with_muted is set" do
966 blocked = insert(:user)
967 {:ok, _user_relationship} = User.block(user, blocked)
969 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
971 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
974 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
976 blocked = insert(:user, ap_id: "http://some-domain.com")
977 {:ok, user} = User.block_domain(user, "some-domain.com")
979 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
981 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
984 test "it returns notifications from muted threads when with_muted is set" do
986 another_user = insert(:user)
988 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
990 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
991 assert length(Notification.for_user(user, %{with_muted: true})) == 1