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] = notifs = Notification.for_user(other_user)
450 assert length(notifs) == 2
455 CommonAPI.post(user, %{
456 "status" => "hey yet again @#{other_user.nickname}!"
459 Notification.set_read_up_to(other_user, n2.id)
461 [n3, n2, n1] = Notification.for_user(other_user)
463 assert n1.seen == true
464 assert n2.seen == true
465 assert n3.seen == false
469 describe "for_user_since/2" do
470 defp days_ago(days) do
472 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
473 -days * 60 * 60 * 24,
478 test "Returns recent notifications" do
479 user1 = insert(:user)
480 user2 = insert(:user)
482 Enum.each(0..10, fn i ->
484 CommonAPI.post(user1, %{
485 "status" => "hey ##{i} @#{user2.nickname}!"
489 {old, new} = Enum.split(Notification.for_user(user2), 5)
491 Enum.each(old, fn notification ->
493 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
494 |> Pleroma.Repo.update!()
497 recent_notifications_ids =
499 |> Notification.for_user_since(
500 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
504 Enum.each(old, fn %{id: id} ->
505 refute id in recent_notifications_ids
508 Enum.each(new, fn %{id: id} ->
509 assert id in recent_notifications_ids
514 describe "notification target determination / get_notified_from_activity/2" do
515 test "it sends notifications to addressed users in new messages" do
517 other_user = insert(:user)
520 CommonAPI.post(user, %{
521 "status" => "hey @#{other_user.nickname}!"
524 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
526 assert other_user in enabled_receivers
529 test "it sends notifications to mentioned users in new messages" do
531 other_user = insert(:user)
534 "@context" => "https://www.w3.org/ns/activitystreams",
536 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
537 "actor" => user.ap_id,
540 "content" => "message with a Mention tag, but no explicit tagging",
544 "href" => other_user.ap_id,
545 "name" => other_user.nickname
548 "attributedTo" => user.ap_id
552 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
554 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
556 assert other_user in enabled_receivers
559 test "it does not send notifications to users who are only cc in new messages" do
561 other_user = insert(:user)
564 "@context" => "https://www.w3.org/ns/activitystreams",
566 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
567 "cc" => [other_user.ap_id],
568 "actor" => user.ap_id,
571 "content" => "hi everyone",
572 "attributedTo" => user.ap_id
576 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
578 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
580 assert other_user not in enabled_receivers
583 test "it does not send notification to mentioned users in likes" do
585 other_user = insert(:user)
586 third_user = insert(:user)
588 {:ok, activity_one} =
589 CommonAPI.post(user, %{
590 "status" => "hey @#{other_user.nickname}!"
593 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
595 {enabled_receivers, _disabled_receivers} =
596 Notification.get_notified_from_activity(activity_two)
598 assert other_user not in enabled_receivers
601 test "it does not send notification to mentioned users in announces" do
603 other_user = insert(:user)
604 third_user = insert(:user)
606 {:ok, activity_one} =
607 CommonAPI.post(user, %{
608 "status" => "hey @#{other_user.nickname}!"
611 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
613 {enabled_receivers, _disabled_receivers} =
614 Notification.get_notified_from_activity(activity_two)
616 assert other_user not in enabled_receivers
619 test "it returns blocking recipient in disabled recipients list" do
621 other_user = insert(:user)
622 {:ok, _user_relationship} = User.block(other_user, user)
624 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
626 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
628 assert [] == enabled_receivers
629 assert [other_user] == disabled_receivers
632 test "it returns notification-muting recipient in disabled recipients list" do
634 other_user = insert(:user)
635 {:ok, _user_relationships} = User.mute(other_user, user)
637 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
639 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
641 assert [] == enabled_receivers
642 assert [other_user] == disabled_receivers
645 test "it returns thread-muting recipient in disabled recipients list" do
647 other_user = insert(:user)
649 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
651 {:ok, _} = CommonAPI.add_mute(other_user, activity)
653 {:ok, same_context_activity} =
654 CommonAPI.post(user, %{
655 "status" => "hey-hey-hey @#{other_user.nickname}!",
656 "in_reply_to_status_id" => activity.id
659 {enabled_receivers, disabled_receivers} =
660 Notification.get_notified_from_activity(same_context_activity)
662 assert [other_user] == disabled_receivers
663 refute other_user in enabled_receivers
667 describe "notification lifecycle" do
668 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
670 other_user = insert(:user)
672 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
674 assert Enum.empty?(Notification.for_user(user))
676 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
678 assert length(Notification.for_user(user)) == 1
680 {:ok, _} = CommonAPI.delete(activity.id, user)
682 assert Enum.empty?(Notification.for_user(user))
685 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
687 other_user = insert(:user)
689 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
691 assert Enum.empty?(Notification.for_user(user))
693 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
695 assert length(Notification.for_user(user)) == 1
697 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
699 assert Enum.empty?(Notification.for_user(user))
702 test "repeating 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.repeat(activity.id, other_user)
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 "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" 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.repeat(activity.id, other_user)
729 assert length(Notification.for_user(user)) == 1
731 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
733 assert Enum.empty?(Notification.for_user(user))
736 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
746 assert Enum.empty?(Notification.for_user(user))
748 {:error, _} = CommonAPI.favorite(activity.id, other_user)
750 assert Enum.empty?(Notification.for_user(user))
753 test "repeating an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
763 assert Enum.empty?(Notification.for_user(user))
765 {:error, _} = CommonAPI.repeat(activity.id, other_user)
767 assert Enum.empty?(Notification.for_user(user))
770 test "replying to a deleted post without tagging does not generate a notification" do
772 other_user = insert(:user)
774 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
775 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
777 {:ok, _reply_activity} =
778 CommonAPI.post(other_user, %{
779 "status" => "test reply",
780 "in_reply_to_status_id" => activity.id
783 assert Enum.empty?(Notification.for_user(user))
786 test "notifications are deleted if a local user is deleted" do
788 other_user = insert(:user)
791 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
793 refute Enum.empty?(Notification.for_user(other_user))
795 {:ok, job} = User.delete(user)
796 ObanHelpers.perform(job)
798 assert Enum.empty?(Notification.for_user(other_user))
801 test "notifications are deleted if a remote user is deleted" do
802 remote_user = insert(:user)
803 local_user = insert(:user)
806 "@context" => "https://www.w3.org/ns/activitystreams",
808 "actor" => remote_user.ap_id,
809 "id" => remote_user.ap_id <> "/activities/test",
810 "to" => [local_user.ap_id],
814 "content" => "Hello!",
818 "href" => local_user.ap_id,
819 "name" => "@#{local_user.nickname}"
822 "to" => [local_user.ap_id],
824 "attributedTo" => remote_user.ap_id
828 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
830 refute Enum.empty?(Notification.for_user(local_user))
832 delete_user_message = %{
833 "@context" => "https://www.w3.org/ns/activitystreams",
834 "id" => remote_user.ap_id <> "/activities/delete",
835 "actor" => remote_user.ap_id,
837 "object" => remote_user.ap_id
840 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
841 ObanHelpers.perform_all()
843 assert Enum.empty?(Notification.for_user(local_user))
846 test "move activity generates a notification" do
847 %{ap_id: old_ap_id} = old_user = insert(:user)
848 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
849 follower = insert(:user)
850 other_follower = insert(:user, %{allow_following_move: false})
852 User.follow(follower, old_user)
853 User.follow(other_follower, old_user)
855 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
856 ObanHelpers.perform_all()
858 assert [] = Notification.for_user(follower)
863 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
866 ] = Notification.for_user(follower, %{with_move: true})
868 assert [] = Notification.for_user(other_follower)
873 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
876 ] = Notification.for_user(other_follower, %{with_move: true})
880 describe "for_user" do
881 test "it returns notifications for muted user without notifications" do
883 muted = insert(:user)
884 {:ok, _user_relationships} = User.mute(user, muted, false)
886 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
888 assert length(Notification.for_user(user)) == 1
891 test "it doesn't return notifications for muted user with notifications" do
893 muted = insert(:user)
894 {:ok, _user_relationships} = User.mute(user, muted)
896 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
898 assert Notification.for_user(user) == []
901 test "it doesn't return notifications for blocked user" do
903 blocked = insert(:user)
904 {:ok, _user_relationship} = User.block(user, blocked)
906 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
908 assert Notification.for_user(user) == []
911 test "it doesn't return notifications for blocked domain" do
913 blocked = insert(:user, ap_id: "http://some-domain.com")
914 {:ok, user} = User.block_domain(user, "some-domain.com")
916 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
918 assert Notification.for_user(user) == []
921 test "it doesn't return notifications for muted thread" do
923 another_user = insert(:user)
925 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
927 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
928 assert Notification.for_user(user) == []
931 test "it returns notifications from a muted user when with_muted is set" do
933 muted = insert(:user)
934 {:ok, _user_relationships} = User.mute(user, muted)
936 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
938 assert length(Notification.for_user(user, %{with_muted: true})) == 1
941 test "it doesn't return notifications from a blocked user when with_muted is set" do
943 blocked = insert(:user)
944 {:ok, _user_relationship} = User.block(user, blocked)
946 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
948 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
951 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
953 blocked = insert(:user, ap_id: "http://some-domain.com")
954 {:ok, user} = User.block_domain(user, "some-domain.com")
956 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
958 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
961 test "it returns notifications from muted threads when with_muted is set" do
963 another_user = insert(:user)
965 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
967 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
968 assert length(Notification.for_user(user, %{with_muted: true})) == 1