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)
168 Streamer.get_topic_and_add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
170 Streamer.get_topic_and_add_socket("user:notification", %{
171 transport_pid: task_user_notification.pid,
172 assigns: %{user: user}
175 activity = insert(:note_activity)
177 notify = Notification.create_notification(activity, user)
178 assert notify.user_id == user.id
180 Task.await(task_user_notification)
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"])
187 {:ok, _user_relationship} = User.block(user, author)
189 assert Notification.create_notification(activity, user)
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}"})
199 assert Notification.create_notification(activity, muter)
202 test "notification created if user is muted without notifications" do
203 muter = insert(:user)
204 muted = insert(:user)
206 {:ok, _user_relationships} = User.mute(muter, muted, false)
208 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
210 assert Notification.create_notification(activity, muter)
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)
220 CommonAPI.post(other_user, %{
221 "status" => "Hi @#{muter.nickname}",
222 "in_reply_to_status_id" => activity.id
225 assert Notification.create_notification(activity, muter)
228 test "it disables notifications from followers" do
229 follower = insert(:user)
232 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
234 User.follow(follower, followed)
235 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
236 refute Notification.create_notification(activity, followed)
239 test "it disables notifications from non-followers" do
240 follower = insert(:user)
244 notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
247 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
248 refute Notification.create_notification(activity, followed)
251 test "it disables notifications from people the user follows" do
253 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
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)
262 test "it disables notifications from people the user does not follow" do
264 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
266 followed = insert(:user)
267 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
268 refute Notification.create_notification(activity, follower)
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"])
275 refute Notification.create_notification(activity, author)
278 test "it doesn't create duplicate notifications for follow+subscribed users" do
280 subscriber = insert(:user)
282 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
283 User.subscribe(subscriber, user)
284 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
285 {:ok, [_notif]} = Notification.create_notifications(status)
288 test "it doesn't create subscription notifications if the recipient cannot see the status" do
290 subscriber = insert(:user)
292 User.subscribe(subscriber, user)
294 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
296 assert {:ok, []} == Notification.create_notifications(status)
300 describe "follow / follow_request notifications" do
301 test "it creates `follow` notification for approved Follow activity" do
303 followed_user = insert(:user, locked: false)
305 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
306 assert FollowingRelationship.following?(user, followed_user)
307 assert [notification] = Notification.for_user(followed_user)
309 assert %{type: "follow"} =
310 NotificationView.render("show.json", %{
311 notification: notification,
316 test "it creates `follow_request` notification for pending Follow activity" do
318 followed_user = insert(:user, locked: true)
320 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
321 refute FollowingRelationship.following?(user, followed_user)
322 assert [notification] = Notification.for_user(followed_user)
324 render_opts = %{notification: notification, for: followed_user}
325 assert %{type: "follow_request"} = NotificationView.render("show.json", render_opts)
327 # After request is accepted, the same notification is rendered with type "follow":
328 assert {:ok, _} = CommonAPI.accept_follow_request(user, followed_user)
330 notification_id = notification.id
331 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
332 assert %{type: "follow"} = NotificationView.render("show.json", render_opts)
335 test "it doesn't create a notification for follow-unfollow-follow chains" do
337 followed_user = insert(:user, locked: false)
339 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
340 assert FollowingRelationship.following?(user, followed_user)
341 assert [notification] = Notification.for_user(followed_user)
343 CommonAPI.unfollow(user, followed_user)
344 {:ok, _, _, _activity_dupe} = CommonAPI.follow(user, followed_user)
346 notification_id = notification.id
347 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
350 test "dismisses the notification on follow request rejection" do
351 user = insert(:user, locked: true)
352 follower = insert(:user)
353 {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, user)
354 assert [notification] = Notification.for_user(user)
355 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
356 assert [] = Notification.for_user(user)
360 describe "get notification" do
361 test "it gets a notification that belongs to the user" do
363 other_user = insert(:user)
365 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
367 {:ok, [notification]} = Notification.create_notifications(activity)
368 {:ok, notification} = Notification.get(other_user, notification.id)
370 assert notification.user_id == other_user.id
373 test "it returns error if the notification doesn't belong to the user" do
375 other_user = insert(:user)
377 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
379 {:ok, [notification]} = Notification.create_notifications(activity)
380 {:error, _notification} = Notification.get(user, notification.id)
384 describe "dismiss notification" do
385 test "it dismisses a notification that belongs to the user" do
387 other_user = insert(:user)
389 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
391 {:ok, [notification]} = Notification.create_notifications(activity)
392 {:ok, notification} = Notification.dismiss(other_user, notification.id)
394 assert notification.user_id == other_user.id
397 test "it returns error if the notification doesn't belong to the user" do
399 other_user = insert(:user)
401 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
403 {:ok, [notification]} = Notification.create_notifications(activity)
404 {:error, _notification} = Notification.dismiss(user, notification.id)
408 describe "clear notification" do
409 test "it clears all notifications belonging to the user" do
411 other_user = insert(:user)
412 third_user = insert(:user)
415 CommonAPI.post(user, %{
416 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
419 {:ok, _notifs} = Notification.create_notifications(activity)
422 CommonAPI.post(user, %{
423 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
426 {:ok, _notifs} = Notification.create_notifications(activity)
427 Notification.clear(other_user)
429 assert Notification.for_user(other_user) == []
430 assert Notification.for_user(third_user) != []
434 describe "set_read_up_to()" do
435 test "it sets all notifications as read up to a specified notification ID" do
437 other_user = insert(:user)
440 CommonAPI.post(user, %{
441 "status" => "hey @#{other_user.nickname}!"
445 CommonAPI.post(user, %{
446 "status" => "hey again @#{other_user.nickname}!"
449 [n2, n1] = Notification.for_user(other_user)
454 CommonAPI.post(user, %{
455 "status" => "hey yet again @#{other_user.nickname}!"
458 [_, read_notification] = Notification.set_read_up_to(other_user, n2.id)
460 assert read_notification.activity.object
462 [n3, n2, n1] = Notification.for_user(other_user)
464 assert n1.seen == true
465 assert n2.seen == true
466 assert n3.seen == false
470 describe "for_user_since/2" do
471 defp days_ago(days) do
473 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
474 -days * 60 * 60 * 24,
479 test "Returns recent notifications" do
480 user1 = insert(:user)
481 user2 = insert(:user)
483 Enum.each(0..10, fn i ->
485 CommonAPI.post(user1, %{
486 "status" => "hey ##{i} @#{user2.nickname}!"
490 {old, new} = Enum.split(Notification.for_user(user2), 5)
492 Enum.each(old, fn notification ->
494 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
495 |> Pleroma.Repo.update!()
498 recent_notifications_ids =
500 |> Notification.for_user_since(
501 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
505 Enum.each(old, fn %{id: id} ->
506 refute id in recent_notifications_ids
509 Enum.each(new, fn %{id: id} ->
510 assert id in recent_notifications_ids
515 describe "notification target determination / get_notified_from_activity/2" do
516 test "it sends notifications to addressed users in new messages" do
518 other_user = insert(:user)
521 CommonAPI.post(user, %{
522 "status" => "hey @#{other_user.nickname}!"
525 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
527 assert other_user in enabled_receivers
530 test "it sends notifications to mentioned users in new messages" do
532 other_user = insert(:user)
535 "@context" => "https://www.w3.org/ns/activitystreams",
537 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
538 "actor" => user.ap_id,
541 "content" => "message with a Mention tag, but no explicit tagging",
545 "href" => other_user.ap_id,
546 "name" => other_user.nickname
549 "attributedTo" => user.ap_id
553 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
555 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
557 assert other_user in enabled_receivers
560 test "it does not send notifications to users who are only cc in new messages" do
562 other_user = insert(:user)
565 "@context" => "https://www.w3.org/ns/activitystreams",
567 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
568 "cc" => [other_user.ap_id],
569 "actor" => user.ap_id,
572 "content" => "hi everyone",
573 "attributedTo" => user.ap_id
577 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
579 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
581 assert other_user not in enabled_receivers
584 test "it does not send notification to mentioned users in likes" do
586 other_user = insert(:user)
587 third_user = insert(:user)
589 {:ok, activity_one} =
590 CommonAPI.post(user, %{
591 "status" => "hey @#{other_user.nickname}!"
594 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
596 {enabled_receivers, _disabled_receivers} =
597 Notification.get_notified_from_activity(activity_two)
599 assert other_user not in enabled_receivers
602 test "it does not send notification to mentioned users in announces" do
604 other_user = insert(:user)
605 third_user = insert(:user)
607 {:ok, activity_one} =
608 CommonAPI.post(user, %{
609 "status" => "hey @#{other_user.nickname}!"
612 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
614 {enabled_receivers, _disabled_receivers} =
615 Notification.get_notified_from_activity(activity_two)
617 assert other_user not in enabled_receivers
620 test "it returns blocking recipient in disabled recipients list" do
622 other_user = insert(:user)
623 {:ok, _user_relationship} = User.block(other_user, user)
625 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
627 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
629 assert [] == enabled_receivers
630 assert [other_user] == disabled_receivers
633 test "it returns notification-muting recipient in disabled recipients list" do
635 other_user = insert(:user)
636 {:ok, _user_relationships} = User.mute(other_user, user)
638 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
640 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
642 assert [] == enabled_receivers
643 assert [other_user] == disabled_receivers
646 test "it returns thread-muting recipient in disabled recipients list" do
648 other_user = insert(:user)
650 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
652 {:ok, _} = CommonAPI.add_mute(other_user, activity)
654 {:ok, same_context_activity} =
655 CommonAPI.post(user, %{
656 "status" => "hey-hey-hey @#{other_user.nickname}!",
657 "in_reply_to_status_id" => activity.id
660 {enabled_receivers, disabled_receivers} =
661 Notification.get_notified_from_activity(same_context_activity)
663 assert [other_user] == disabled_receivers
664 refute other_user in enabled_receivers
668 describe "notification lifecycle" do
669 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
671 other_user = insert(:user)
673 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
675 assert Enum.empty?(Notification.for_user(user))
677 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
679 assert length(Notification.for_user(user)) == 1
681 {:ok, _} = CommonAPI.delete(activity.id, user)
683 assert Enum.empty?(Notification.for_user(user))
686 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
688 other_user = insert(:user)
690 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
692 assert Enum.empty?(Notification.for_user(user))
694 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
696 assert length(Notification.for_user(user)) == 1
698 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
700 assert Enum.empty?(Notification.for_user(user))
703 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
705 other_user = insert(:user)
707 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
709 assert Enum.empty?(Notification.for_user(user))
711 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
713 assert length(Notification.for_user(user)) == 1
715 {:ok, _} = CommonAPI.delete(activity.id, user)
717 assert Enum.empty?(Notification.for_user(user))
720 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
722 other_user = insert(:user)
724 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
726 assert Enum.empty?(Notification.for_user(user))
728 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
730 assert length(Notification.for_user(user)) == 1
732 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
734 assert Enum.empty?(Notification.for_user(user))
737 test "liking an activity which is already deleted does not generate a notification" do
739 other_user = insert(:user)
741 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
743 assert Enum.empty?(Notification.for_user(user))
745 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
747 assert Enum.empty?(Notification.for_user(user))
749 {:error, _} = CommonAPI.favorite(activity.id, other_user)
751 assert Enum.empty?(Notification.for_user(user))
754 test "repeating an activity which is already deleted does not generate a notification" do
756 other_user = insert(:user)
758 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
760 assert Enum.empty?(Notification.for_user(user))
762 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
764 assert Enum.empty?(Notification.for_user(user))
766 {:error, _} = CommonAPI.repeat(activity.id, other_user)
768 assert Enum.empty?(Notification.for_user(user))
771 test "replying to a deleted post without tagging does not generate a notification" do
773 other_user = insert(:user)
775 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
776 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
778 {:ok, _reply_activity} =
779 CommonAPI.post(other_user, %{
780 "status" => "test reply",
781 "in_reply_to_status_id" => activity.id
784 assert Enum.empty?(Notification.for_user(user))
787 test "notifications are deleted if a local user is deleted" do
789 other_user = insert(:user)
792 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
794 refute Enum.empty?(Notification.for_user(other_user))
796 {:ok, job} = User.delete(user)
797 ObanHelpers.perform(job)
799 assert Enum.empty?(Notification.for_user(other_user))
802 test "notifications are deleted if a remote user is deleted" do
803 remote_user = insert(:user)
804 local_user = insert(:user)
807 "@context" => "https://www.w3.org/ns/activitystreams",
809 "actor" => remote_user.ap_id,
810 "id" => remote_user.ap_id <> "/activities/test",
811 "to" => [local_user.ap_id],
815 "content" => "Hello!",
819 "href" => local_user.ap_id,
820 "name" => "@#{local_user.nickname}"
823 "to" => [local_user.ap_id],
825 "attributedTo" => remote_user.ap_id
829 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
831 refute Enum.empty?(Notification.for_user(local_user))
833 delete_user_message = %{
834 "@context" => "https://www.w3.org/ns/activitystreams",
835 "id" => remote_user.ap_id <> "/activities/delete",
836 "actor" => remote_user.ap_id,
838 "object" => remote_user.ap_id
841 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
842 ObanHelpers.perform_all()
844 assert Enum.empty?(Notification.for_user(local_user))
847 test "move activity generates a notification" do
848 %{ap_id: old_ap_id} = old_user = insert(:user)
849 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
850 follower = insert(:user)
851 other_follower = insert(:user, %{allow_following_move: false})
853 User.follow(follower, old_user)
854 User.follow(other_follower, old_user)
856 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
857 ObanHelpers.perform_all()
859 assert [] = Notification.for_user(follower)
864 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
867 ] = Notification.for_user(follower, %{with_move: true})
869 assert [] = Notification.for_user(other_follower)
874 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
877 ] = Notification.for_user(other_follower, %{with_move: true})
881 describe "for_user" do
882 test "it returns notifications for muted user without notifications" do
884 muted = insert(:user)
885 {:ok, _user_relationships} = User.mute(user, muted, false)
887 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
889 [notification] = Notification.for_user(user)
891 assert notification.activity.object
894 test "it doesn't return notifications for muted user with notifications" do
896 muted = insert(:user)
897 {:ok, _user_relationships} = User.mute(user, muted)
899 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
901 assert Notification.for_user(user) == []
904 test "it doesn't return notifications for blocked user" do
906 blocked = insert(:user)
907 {:ok, _user_relationship} = User.block(user, blocked)
909 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
911 assert Notification.for_user(user) == []
914 test "it doesn't return notifications for blocked domain" do
916 blocked = insert(:user, ap_id: "http://some-domain.com")
917 {:ok, user} = User.block_domain(user, "some-domain.com")
919 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
921 assert Notification.for_user(user) == []
924 test "it doesn't return notifications for muted thread" do
926 another_user = insert(:user)
928 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
930 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
931 assert Notification.for_user(user) == []
934 test "it returns notifications from a muted user when with_muted is set" do
936 muted = insert(:user)
937 {:ok, _user_relationships} = User.mute(user, muted)
939 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
941 assert length(Notification.for_user(user, %{with_muted: true})) == 1
944 test "it doesn't return notifications from a blocked user when with_muted is set" do
946 blocked = insert(:user)
947 {:ok, _user_relationship} = User.block(user, blocked)
949 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
951 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
954 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
956 blocked = insert(:user, ap_id: "http://some-domain.com")
957 {:ok, user} = User.block_domain(user, "some-domain.com")
959 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
961 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
964 test "it returns notifications from muted threads when with_muted is set" do
966 another_user = insert(:user)
968 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
970 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
971 assert length(Notification.for_user(user, %{with_muted: true})) == 1