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
51 assert [%Pleroma.Marker{unread_count: 2}] =
52 Pleroma.Marker.get_markers(other_user, ["notifications"])
55 test "it creates a notification for subscribed users" do
57 subscriber = insert(:user)
59 User.subscribe(subscriber, user)
61 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
62 {:ok, [notification]} = Notification.create_notifications(status)
64 assert notification.user_id == subscriber.id
67 test "does not create a notification for subscribed users if status is a reply" do
69 other_user = insert(:user)
70 subscriber = insert(:user)
72 User.subscribe(subscriber, other_user)
74 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
76 {:ok, _reply_activity} =
77 CommonAPI.post(other_user, %{
78 "status" => "test reply",
79 "in_reply_to_status_id" => activity.id
82 user_notifications = Notification.for_user(user)
83 assert length(user_notifications) == 1
85 subscriber_notifications = Notification.for_user(subscriber)
86 assert Enum.empty?(subscriber_notifications)
90 describe "CommonApi.post/2 notification-related functionality" do
91 test_with_mock "creates but does NOT send notification to blocker user",
96 blocker = insert(:user)
97 {:ok, _user_relationship} = User.block(blocker, user)
99 {:ok, _activity} = CommonAPI.post(user, %{"status" => "hey @#{blocker.nickname}!"})
101 blocker_id = blocker.id
102 assert [%Notification{user_id: ^blocker_id}] = Repo.all(Notification)
103 refute called(Push.send(:_))
106 test_with_mock "creates but does NOT send notification to notification-muter user",
111 muter = insert(:user)
112 {:ok, _user_relationships} = User.mute(muter, user)
114 {:ok, _activity} = CommonAPI.post(user, %{"status" => "hey @#{muter.nickname}!"})
117 assert [%Notification{user_id: ^muter_id}] = Repo.all(Notification)
118 refute called(Push.send(:_))
121 test_with_mock "creates but does NOT send notification to thread-muter user",
126 thread_muter = insert(:user)
128 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{thread_muter.nickname}!"})
130 {:ok, _} = CommonAPI.add_mute(thread_muter, activity)
132 {:ok, _same_context_activity} =
133 CommonAPI.post(user, %{
134 "status" => "hey-hey-hey @#{thread_muter.nickname}!",
135 "in_reply_to_status_id" => activity.id
138 [pre_mute_notification, post_mute_notification] =
139 Repo.all(from(n in Notification, where: n.user_id == ^thread_muter.id, order_by: n.id))
141 pre_mute_notification_id = pre_mute_notification.id
142 post_mute_notification_id = post_mute_notification.id
147 %Notification{id: ^pre_mute_notification_id} -> true
156 %Notification{id: ^post_mute_notification_id} -> true
164 describe "create_notification" do
165 @tag needs_streamer: true
166 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
168 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
169 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
170 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
174 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
177 activity = insert(:note_activity)
179 notify = Notification.create_notification(activity, user)
180 assert notify.user_id == user.id
182 Task.await(task_user_notification)
185 test "it creates a notification for user if the user blocks the activity author" do
186 activity = insert(:note_activity)
187 author = User.get_cached_by_ap_id(activity.data["actor"])
189 {:ok, _user_relationship} = User.block(user, author)
191 assert Notification.create_notification(activity, user)
194 test "it creates a notification for the user if the user mutes the activity author" do
195 muter = insert(:user)
196 muted = insert(:user)
197 {:ok, _} = User.mute(muter, muted)
198 muter = Repo.get(User, muter.id)
199 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
201 assert Notification.create_notification(activity, muter)
204 test "notification created if user is muted without notifications" do
205 muter = insert(:user)
206 muted = insert(:user)
208 {:ok, _user_relationships} = User.mute(muter, muted, false)
210 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
212 assert Notification.create_notification(activity, muter)
215 test "it creates a notification for an activity from a muted thread" do
216 muter = insert(:user)
217 other_user = insert(:user)
218 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
219 CommonAPI.add_mute(muter, activity)
222 CommonAPI.post(other_user, %{
223 "status" => "Hi @#{muter.nickname}",
224 "in_reply_to_status_id" => activity.id
227 assert Notification.create_notification(activity, muter)
230 test "it disables notifications from followers" do
231 follower = insert(:user)
234 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
236 User.follow(follower, followed)
237 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
238 refute Notification.create_notification(activity, followed)
241 test "it disables notifications from non-followers" do
242 follower = insert(:user)
246 notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
249 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
250 refute Notification.create_notification(activity, followed)
253 test "it disables notifications from people the user follows" do
255 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
257 followed = insert(:user)
258 User.follow(follower, followed)
259 follower = Repo.get(User, follower.id)
260 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
261 refute Notification.create_notification(activity, follower)
264 test "it disables notifications from people the user does not follow" do
266 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
268 followed = insert(:user)
269 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
270 refute Notification.create_notification(activity, follower)
273 test "it doesn't create a notification for user if he is the activity author" do
274 activity = insert(:note_activity)
275 author = User.get_cached_by_ap_id(activity.data["actor"])
277 refute Notification.create_notification(activity, author)
280 test "it doesn't create duplicate notifications for follow+subscribed users" do
282 subscriber = insert(:user)
284 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
285 User.subscribe(subscriber, user)
286 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
287 {:ok, [_notif]} = Notification.create_notifications(status)
290 test "it doesn't create subscription notifications if the recipient cannot see the status" do
292 subscriber = insert(:user)
294 User.subscribe(subscriber, user)
296 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
298 assert {:ok, []} == Notification.create_notifications(status)
302 describe "follow / follow_request notifications" do
303 test "it creates `follow` notification for approved Follow activity" do
305 followed_user = insert(:user, locked: false)
307 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
308 assert FollowingRelationship.following?(user, followed_user)
309 assert [notification] = Notification.for_user(followed_user)
311 assert %{type: "follow"} =
312 NotificationView.render("show.json", %{
313 notification: notification,
318 test "it creates `follow_request` notification for pending Follow activity" do
320 followed_user = insert(:user, locked: true)
322 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
323 refute FollowingRelationship.following?(user, followed_user)
324 assert [notification] = Notification.for_user(followed_user)
326 render_opts = %{notification: notification, for: followed_user}
327 assert %{type: "follow_request"} = NotificationView.render("show.json", render_opts)
329 # After request is accepted, the same notification is rendered with type "follow":
330 assert {:ok, _} = CommonAPI.accept_follow_request(user, followed_user)
332 notification_id = notification.id
333 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
334 assert %{type: "follow"} = NotificationView.render("show.json", render_opts)
337 test "it doesn't create a notification for follow-unfollow-follow chains" do
339 followed_user = insert(:user, locked: false)
341 {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
342 assert FollowingRelationship.following?(user, followed_user)
343 assert [notification] = Notification.for_user(followed_user)
345 CommonAPI.unfollow(user, followed_user)
346 {:ok, _, _, _activity_dupe} = CommonAPI.follow(user, followed_user)
348 notification_id = notification.id
349 assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
352 test "dismisses the notification on follow request rejection" do
353 user = insert(:user, locked: true)
354 follower = insert(:user)
355 {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, user)
356 assert [notification] = Notification.for_user(user)
357 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
358 assert [] = Notification.for_user(user)
362 describe "get notification" do
363 test "it gets a notification that belongs to the user" do
365 other_user = insert(:user)
367 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
369 {:ok, [notification]} = Notification.create_notifications(activity)
370 {:ok, notification} = Notification.get(other_user, notification.id)
372 assert notification.user_id == other_user.id
375 test "it returns error if the notification doesn't belong to the user" do
377 other_user = insert(:user)
379 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
381 {:ok, [notification]} = Notification.create_notifications(activity)
382 {:error, _notification} = Notification.get(user, notification.id)
386 describe "dismiss notification" do
387 test "it dismisses a notification that belongs to the user" do
389 other_user = insert(:user)
391 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
393 {:ok, [notification]} = Notification.create_notifications(activity)
394 {:ok, notification} = Notification.dismiss(other_user, notification.id)
396 assert notification.user_id == other_user.id
399 test "it returns error if the notification doesn't belong to the user" do
401 other_user = insert(:user)
403 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
405 {:ok, [notification]} = Notification.create_notifications(activity)
406 {:error, _notification} = Notification.dismiss(user, notification.id)
410 describe "clear notification" do
411 test "it clears all notifications belonging to the user" do
413 other_user = insert(:user)
414 third_user = insert(:user)
417 CommonAPI.post(user, %{
418 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
421 {:ok, _notifs} = Notification.create_notifications(activity)
424 CommonAPI.post(user, %{
425 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
428 {:ok, _notifs} = Notification.create_notifications(activity)
429 Notification.clear(other_user)
431 assert Notification.for_user(other_user) == []
432 assert Notification.for_user(third_user) != []
436 describe "set_read_up_to()" do
437 test "it sets all notifications as read up to a specified notification ID" do
439 other_user = insert(:user)
442 CommonAPI.post(user, %{
443 "status" => "hey @#{other_user.nickname}!"
447 CommonAPI.post(user, %{
448 "status" => "hey again @#{other_user.nickname}!"
451 [n2, n1] = notifs = Notification.for_user(other_user)
452 assert length(notifs) == 2
457 CommonAPI.post(user, %{
458 "status" => "hey yet again @#{other_user.nickname}!"
461 Notification.set_read_up_to(other_user, n2.id)
463 [n3, n2, n1] = Notification.for_user(other_user)
465 assert n1.seen == true
466 assert n2.seen == true
467 assert n3.seen == false
469 assert %Pleroma.Marker{} =
473 user_id: other_user.id,
474 timeline: "notifications"
477 assert m.last_read_id == to_string(n2.id)
481 describe "for_user_since/2" do
482 defp days_ago(days) do
484 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
485 -days * 60 * 60 * 24,
490 test "Returns recent notifications" do
491 user1 = insert(:user)
492 user2 = insert(:user)
494 Enum.each(0..10, fn i ->
496 CommonAPI.post(user1, %{
497 "status" => "hey ##{i} @#{user2.nickname}!"
501 {old, new} = Enum.split(Notification.for_user(user2), 5)
503 Enum.each(old, fn notification ->
505 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
506 |> Pleroma.Repo.update!()
509 recent_notifications_ids =
511 |> Notification.for_user_since(
512 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
516 Enum.each(old, fn %{id: id} ->
517 refute id in recent_notifications_ids
520 Enum.each(new, fn %{id: id} ->
521 assert id in recent_notifications_ids
526 describe "notification target determination / get_notified_from_activity/2" do
527 test "it sends notifications to addressed users in new messages" do
529 other_user = insert(:user)
532 CommonAPI.post(user, %{
533 "status" => "hey @#{other_user.nickname}!"
536 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
538 assert other_user in enabled_receivers
541 test "it sends notifications to mentioned users in new messages" do
543 other_user = insert(:user)
546 "@context" => "https://www.w3.org/ns/activitystreams",
548 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
549 "actor" => user.ap_id,
552 "content" => "message with a Mention tag, but no explicit tagging",
556 "href" => other_user.ap_id,
557 "name" => other_user.nickname
560 "attributedTo" => user.ap_id
564 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
566 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
568 assert other_user in enabled_receivers
571 test "it does not send notifications to users who are only cc in new messages" do
573 other_user = insert(:user)
576 "@context" => "https://www.w3.org/ns/activitystreams",
578 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
579 "cc" => [other_user.ap_id],
580 "actor" => user.ap_id,
583 "content" => "hi everyone",
584 "attributedTo" => user.ap_id
588 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
590 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
592 assert other_user not in enabled_receivers
595 test "it does not send notification to mentioned users in likes" do
597 other_user = insert(:user)
598 third_user = insert(:user)
600 {:ok, activity_one} =
601 CommonAPI.post(user, %{
602 "status" => "hey @#{other_user.nickname}!"
605 {:ok, activity_two} = CommonAPI.favorite(third_user, activity_one.id)
607 {enabled_receivers, _disabled_receivers} =
608 Notification.get_notified_from_activity(activity_two)
610 assert other_user not in enabled_receivers
613 test "it does not send notification to mentioned users in announces" do
615 other_user = insert(:user)
616 third_user = insert(:user)
618 {:ok, activity_one} =
619 CommonAPI.post(user, %{
620 "status" => "hey @#{other_user.nickname}!"
623 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
625 {enabled_receivers, _disabled_receivers} =
626 Notification.get_notified_from_activity(activity_two)
628 assert other_user not in enabled_receivers
631 test "it returns blocking recipient in disabled recipients list" do
633 other_user = insert(:user)
634 {:ok, _user_relationship} = User.block(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 notification-muting recipient in disabled recipients list" do
646 other_user = insert(:user)
647 {:ok, _user_relationships} = User.mute(other_user, user)
649 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
651 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
653 assert [] == enabled_receivers
654 assert [other_user] == disabled_receivers
657 test "it returns thread-muting recipient in disabled recipients list" do
659 other_user = insert(:user)
661 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
663 {:ok, _} = CommonAPI.add_mute(other_user, activity)
665 {:ok, same_context_activity} =
666 CommonAPI.post(user, %{
667 "status" => "hey-hey-hey @#{other_user.nickname}!",
668 "in_reply_to_status_id" => activity.id
671 {enabled_receivers, disabled_receivers} =
672 Notification.get_notified_from_activity(same_context_activity)
674 assert [other_user] == disabled_receivers
675 refute other_user in enabled_receivers
678 test "it returns non-following domain-blocking recipient in disabled recipients list" do
679 blocked_domain = "blocked.domain"
680 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
681 other_user = insert(:user)
683 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
685 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
687 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
689 assert [] == enabled_receivers
690 assert [other_user] == disabled_receivers
693 test "it returns following domain-blocking recipient in enabled recipients list" do
694 blocked_domain = "blocked.domain"
695 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
696 other_user = insert(:user)
698 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
699 {:ok, other_user} = User.follow(other_user, user)
701 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
703 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
705 assert [other_user] == enabled_receivers
706 assert [] == disabled_receivers
710 describe "notification lifecycle" do
711 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
713 other_user = insert(:user)
715 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
717 assert Enum.empty?(Notification.for_user(user))
719 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
721 assert length(Notification.for_user(user)) == 1
723 {:ok, _} = CommonAPI.delete(activity.id, user)
725 assert Enum.empty?(Notification.for_user(user))
728 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
730 other_user = insert(:user)
732 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
734 assert Enum.empty?(Notification.for_user(user))
736 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
738 assert length(Notification.for_user(user)) == 1
740 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
742 assert Enum.empty?(Notification.for_user(user))
745 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
747 other_user = insert(:user)
749 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
751 assert Enum.empty?(Notification.for_user(user))
753 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
755 assert length(Notification.for_user(user)) == 1
757 {:ok, _} = CommonAPI.delete(activity.id, user)
759 assert Enum.empty?(Notification.for_user(user))
762 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
764 other_user = insert(:user)
766 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
768 assert Enum.empty?(Notification.for_user(user))
770 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
772 assert length(Notification.for_user(user)) == 1
774 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
776 assert Enum.empty?(Notification.for_user(user))
779 test "liking an activity which is already deleted does not generate a notification" do
781 other_user = insert(:user)
783 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
785 assert Enum.empty?(Notification.for_user(user))
787 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
789 assert Enum.empty?(Notification.for_user(user))
791 {:error, :not_found} = CommonAPI.favorite(other_user, activity.id)
793 assert Enum.empty?(Notification.for_user(user))
796 test "repeating an activity which is already deleted does not generate a notification" do
798 other_user = insert(:user)
800 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
802 assert Enum.empty?(Notification.for_user(user))
804 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
806 assert Enum.empty?(Notification.for_user(user))
808 {:error, _} = CommonAPI.repeat(activity.id, other_user)
810 assert Enum.empty?(Notification.for_user(user))
813 test "replying to a deleted post without tagging does not generate a notification" do
815 other_user = insert(:user)
817 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
818 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
820 {:ok, _reply_activity} =
821 CommonAPI.post(other_user, %{
822 "status" => "test reply",
823 "in_reply_to_status_id" => activity.id
826 assert Enum.empty?(Notification.for_user(user))
829 test "notifications are deleted if a local user is deleted" do
831 other_user = insert(:user)
834 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
836 refute Enum.empty?(Notification.for_user(other_user))
838 {:ok, job} = User.delete(user)
839 ObanHelpers.perform(job)
841 assert Enum.empty?(Notification.for_user(other_user))
844 test "notifications are deleted if a remote user is deleted" do
845 remote_user = insert(:user)
846 local_user = insert(:user)
849 "@context" => "https://www.w3.org/ns/activitystreams",
851 "actor" => remote_user.ap_id,
852 "id" => remote_user.ap_id <> "/activities/test",
853 "to" => [local_user.ap_id],
857 "content" => "Hello!",
861 "href" => local_user.ap_id,
862 "name" => "@#{local_user.nickname}"
865 "to" => [local_user.ap_id],
867 "attributedTo" => remote_user.ap_id
871 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
873 refute Enum.empty?(Notification.for_user(local_user))
875 delete_user_message = %{
876 "@context" => "https://www.w3.org/ns/activitystreams",
877 "id" => remote_user.ap_id <> "/activities/delete",
878 "actor" => remote_user.ap_id,
880 "object" => remote_user.ap_id
883 remote_user_url = remote_user.ap_id
886 %{method: :get, url: ^remote_user_url} ->
887 %Tesla.Env{status: 404, body: ""}
890 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
891 ObanHelpers.perform_all()
893 assert Enum.empty?(Notification.for_user(local_user))
896 @tag capture_log: true
897 test "move activity generates a notification" do
898 %{ap_id: old_ap_id} = old_user = insert(:user)
899 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
900 follower = insert(:user)
901 other_follower = insert(:user, %{allow_following_move: false})
903 User.follow(follower, old_user)
904 User.follow(other_follower, old_user)
906 old_user_url = old_user.ap_id
909 File.read!("test/fixtures/users_mock/localhost.json")
910 |> String.replace("{{nickname}}", old_user.nickname)
914 %{method: :get, url: ^old_user_url} ->
915 %Tesla.Env{status: 200, body: body}
918 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
919 ObanHelpers.perform_all()
924 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
927 ] = Notification.for_user(follower)
932 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
935 ] = Notification.for_user(other_follower)
939 describe "for_user" do
940 test "it returns notifications for muted user without notifications" do
942 muted = insert(:user)
943 {:ok, _user_relationships} = User.mute(user, muted, false)
945 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
947 assert length(Notification.for_user(user)) == 1
950 test "it doesn't return notifications for muted user with notifications" do
952 muted = insert(:user)
953 {:ok, _user_relationships} = User.mute(user, muted)
955 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
957 assert Notification.for_user(user) == []
960 test "it doesn't return notifications for blocked user" do
962 blocked = insert(:user)
963 {:ok, _user_relationship} = User.block(user, blocked)
965 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
967 assert Notification.for_user(user) == []
970 test "it doesn't return notifications for domain-blocked non-followed user" do
972 blocked = insert(:user, ap_id: "http://some-domain.com")
973 {:ok, user} = User.block_domain(user, "some-domain.com")
975 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
977 assert Notification.for_user(user) == []
980 test "it returns notifications for domain-blocked but followed user" do
982 blocked = insert(:user, ap_id: "http://some-domain.com")
984 {:ok, user} = User.block_domain(user, "some-domain.com")
985 {:ok, _} = User.follow(user, blocked)
987 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
989 assert length(Notification.for_user(user)) == 1
992 test "it doesn't return notifications for muted thread" do
994 another_user = insert(:user)
996 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
998 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
999 assert Notification.for_user(user) == []
1002 test "it returns notifications from a muted user when with_muted is set" do
1003 user = insert(:user)
1004 muted = insert(:user)
1005 {:ok, _user_relationships} = User.mute(user, muted)
1007 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
1009 assert length(Notification.for_user(user, %{with_muted: true})) == 1
1012 test "it doesn't return notifications from a blocked user when with_muted is set" do
1013 user = insert(:user)
1014 blocked = insert(:user)
1015 {:ok, _user_relationship} = User.block(user, blocked)
1017 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1019 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1022 test "when with_muted is set, " <>
1023 "it doesn't return notifications from a domain-blocked non-followed user" do
1024 user = insert(:user)
1025 blocked = insert(:user, ap_id: "http://some-domain.com")
1026 {:ok, user} = User.block_domain(user, "some-domain.com")
1028 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1030 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1033 test "it returns notifications from muted threads when with_muted is set" do
1034 user = insert(:user)
1035 another_user = insert(:user)
1037 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
1039 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
1040 assert length(Notification.for_user(user, %{with_muted: true})) == 1