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)
366 test "dismisses the notification on follow request rejection" do
367 clear_config([:notifications, :enable_follow_request_notifications], true)
368 user = insert(:user, locked: true)
369 follower = insert(:user)
370 {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, user)
371 assert [notification] = Notification.for_user(user)
372 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
373 assert [] = Notification.for_user(user)
377 describe "get notification" do
378 test "it gets a notification that belongs to the user" do
380 other_user = insert(:user)
382 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
384 {:ok, [notification]} = Notification.create_notifications(activity)
385 {:ok, notification} = Notification.get(other_user, notification.id)
387 assert notification.user_id == other_user.id
390 test "it returns error if the notification doesn't belong to the user" do
392 other_user = insert(:user)
394 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
396 {:ok, [notification]} = Notification.create_notifications(activity)
397 {:error, _notification} = Notification.get(user, notification.id)
401 describe "dismiss notification" do
402 test "it dismisses a notification that belongs to the user" do
404 other_user = insert(:user)
406 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
408 {:ok, [notification]} = Notification.create_notifications(activity)
409 {:ok, notification} = Notification.dismiss(other_user, notification.id)
411 assert notification.user_id == other_user.id
414 test "it returns error if the notification doesn't belong to the user" do
416 other_user = insert(:user)
418 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
420 {:ok, [notification]} = Notification.create_notifications(activity)
421 {:error, _notification} = Notification.dismiss(user, notification.id)
425 describe "clear notification" do
426 test "it clears all notifications belonging to the user" do
428 other_user = insert(:user)
429 third_user = insert(:user)
432 CommonAPI.post(user, %{
433 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
436 {:ok, _notifs} = Notification.create_notifications(activity)
439 CommonAPI.post(user, %{
440 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
443 {:ok, _notifs} = Notification.create_notifications(activity)
444 Notification.clear(other_user)
446 assert Notification.for_user(other_user) == []
447 assert Notification.for_user(third_user) != []
451 describe "set_read_up_to()" do
452 test "it sets all notifications as read up to a specified notification ID" do
454 other_user = insert(:user)
457 CommonAPI.post(user, %{
458 "status" => "hey @#{other_user.nickname}!"
462 CommonAPI.post(user, %{
463 "status" => "hey again @#{other_user.nickname}!"
466 [n2, n1] = notifs = Notification.for_user(other_user)
467 assert length(notifs) == 2
472 CommonAPI.post(user, %{
473 "status" => "hey yet again @#{other_user.nickname}!"
476 Notification.set_read_up_to(other_user, n2.id)
478 [n3, n2, n1] = Notification.for_user(other_user)
480 assert n1.seen == true
481 assert n2.seen == true
482 assert n3.seen == false
486 describe "for_user_since/2" do
487 defp days_ago(days) do
489 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
490 -days * 60 * 60 * 24,
495 test "Returns recent notifications" do
496 user1 = insert(:user)
497 user2 = insert(:user)
499 Enum.each(0..10, fn i ->
501 CommonAPI.post(user1, %{
502 "status" => "hey ##{i} @#{user2.nickname}!"
506 {old, new} = Enum.split(Notification.for_user(user2), 5)
508 Enum.each(old, fn notification ->
510 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
511 |> Pleroma.Repo.update!()
514 recent_notifications_ids =
516 |> Notification.for_user_since(
517 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
521 Enum.each(old, fn %{id: id} ->
522 refute id in recent_notifications_ids
525 Enum.each(new, fn %{id: id} ->
526 assert id in recent_notifications_ids
531 describe "notification target determination / get_notified_from_activity/2" do
532 test "it sends notifications to addressed users in new messages" do
534 other_user = insert(:user)
537 CommonAPI.post(user, %{
538 "status" => "hey @#{other_user.nickname}!"
541 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
543 assert other_user in enabled_receivers
546 test "it sends notifications to mentioned users in new messages" do
548 other_user = insert(:user)
551 "@context" => "https://www.w3.org/ns/activitystreams",
553 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
554 "actor" => user.ap_id,
557 "content" => "message with a Mention tag, but no explicit tagging",
561 "href" => other_user.ap_id,
562 "name" => other_user.nickname
565 "attributedTo" => user.ap_id
569 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
571 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
573 assert other_user in enabled_receivers
576 test "it does not send notifications to users who are only cc in new messages" do
578 other_user = insert(:user)
581 "@context" => "https://www.w3.org/ns/activitystreams",
583 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
584 "cc" => [other_user.ap_id],
585 "actor" => user.ap_id,
588 "content" => "hi everyone",
589 "attributedTo" => user.ap_id
593 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
595 {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
597 assert other_user not in enabled_receivers
600 test "it does not send notification to mentioned users in likes" do
602 other_user = insert(:user)
603 third_user = insert(:user)
605 {:ok, activity_one} =
606 CommonAPI.post(user, %{
607 "status" => "hey @#{other_user.nickname}!"
610 {:ok, activity_two} = CommonAPI.favorite(third_user, activity_one.id)
612 {enabled_receivers, _disabled_receivers} =
613 Notification.get_notified_from_activity(activity_two)
615 assert other_user not in enabled_receivers
618 test "it does not send notification to mentioned users in announces" do
620 other_user = insert(:user)
621 third_user = insert(:user)
623 {:ok, activity_one} =
624 CommonAPI.post(user, %{
625 "status" => "hey @#{other_user.nickname}!"
628 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
630 {enabled_receivers, _disabled_receivers} =
631 Notification.get_notified_from_activity(activity_two)
633 assert other_user not in enabled_receivers
636 test "it returns blocking recipient in disabled recipients list" do
638 other_user = insert(:user)
639 {:ok, _user_relationship} = User.block(other_user, user)
641 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
643 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
645 assert [] == enabled_receivers
646 assert [other_user] == disabled_receivers
649 test "it returns notification-muting recipient in disabled recipients list" do
651 other_user = insert(:user)
652 {:ok, _user_relationships} = User.mute(other_user, user)
654 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
656 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
658 assert [] == enabled_receivers
659 assert [other_user] == disabled_receivers
662 test "it returns thread-muting recipient in disabled recipients list" do
664 other_user = insert(:user)
666 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
668 {:ok, _} = CommonAPI.add_mute(other_user, activity)
670 {:ok, same_context_activity} =
671 CommonAPI.post(user, %{
672 "status" => "hey-hey-hey @#{other_user.nickname}!",
673 "in_reply_to_status_id" => activity.id
676 {enabled_receivers, disabled_receivers} =
677 Notification.get_notified_from_activity(same_context_activity)
679 assert [other_user] == disabled_receivers
680 refute other_user in enabled_receivers
683 test "it returns non-following domain-blocking recipient in disabled recipients list" do
684 blocked_domain = "blocked.domain"
685 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
686 other_user = insert(:user)
688 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
690 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
692 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
694 assert [] == enabled_receivers
695 assert [other_user] == disabled_receivers
698 test "it returns following domain-blocking recipient in enabled recipients list" do
699 blocked_domain = "blocked.domain"
700 user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
701 other_user = insert(:user)
703 {:ok, other_user} = User.block_domain(other_user, blocked_domain)
704 {:ok, other_user} = User.follow(other_user, user)
706 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
708 {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
710 assert [other_user] == enabled_receivers
711 assert [] == disabled_receivers
715 describe "notification lifecycle" do
716 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
718 other_user = insert(:user)
720 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
722 assert Enum.empty?(Notification.for_user(user))
724 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
726 assert length(Notification.for_user(user)) == 1
728 {:ok, _} = CommonAPI.delete(activity.id, user)
730 assert Enum.empty?(Notification.for_user(user))
733 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
735 other_user = insert(:user)
737 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
739 assert Enum.empty?(Notification.for_user(user))
741 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
743 assert length(Notification.for_user(user)) == 1
745 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
747 assert Enum.empty?(Notification.for_user(user))
750 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
752 other_user = insert(:user)
754 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
756 assert Enum.empty?(Notification.for_user(user))
758 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
760 assert length(Notification.for_user(user)) == 1
762 {:ok, _} = CommonAPI.delete(activity.id, user)
764 assert Enum.empty?(Notification.for_user(user))
767 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
769 other_user = insert(:user)
771 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
773 assert Enum.empty?(Notification.for_user(user))
775 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
777 assert length(Notification.for_user(user)) == 1
779 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
781 assert Enum.empty?(Notification.for_user(user))
784 test "liking an activity which is already deleted does not generate a notification" do
786 other_user = insert(:user)
788 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
790 assert Enum.empty?(Notification.for_user(user))
792 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
794 assert Enum.empty?(Notification.for_user(user))
796 {:error, :not_found} = CommonAPI.favorite(other_user, activity.id)
798 assert Enum.empty?(Notification.for_user(user))
801 test "repeating an activity which is already deleted does not generate a notification" do
803 other_user = insert(:user)
805 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
807 assert Enum.empty?(Notification.for_user(user))
809 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
811 assert Enum.empty?(Notification.for_user(user))
813 {:error, _} = CommonAPI.repeat(activity.id, other_user)
815 assert Enum.empty?(Notification.for_user(user))
818 test "replying to a deleted post without tagging does not generate a notification" do
820 other_user = insert(:user)
822 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
823 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
825 {:ok, _reply_activity} =
826 CommonAPI.post(other_user, %{
827 "status" => "test reply",
828 "in_reply_to_status_id" => activity.id
831 assert Enum.empty?(Notification.for_user(user))
834 test "notifications are deleted if a local user is deleted" do
836 other_user = insert(:user)
839 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
841 refute Enum.empty?(Notification.for_user(other_user))
843 {:ok, job} = User.delete(user)
844 ObanHelpers.perform(job)
846 assert Enum.empty?(Notification.for_user(other_user))
849 test "notifications are deleted if a remote user is deleted" do
850 remote_user = insert(:user)
851 local_user = insert(:user)
854 "@context" => "https://www.w3.org/ns/activitystreams",
856 "actor" => remote_user.ap_id,
857 "id" => remote_user.ap_id <> "/activities/test",
858 "to" => [local_user.ap_id],
862 "content" => "Hello!",
866 "href" => local_user.ap_id,
867 "name" => "@#{local_user.nickname}"
870 "to" => [local_user.ap_id],
872 "attributedTo" => remote_user.ap_id
876 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
878 refute Enum.empty?(Notification.for_user(local_user))
880 delete_user_message = %{
881 "@context" => "https://www.w3.org/ns/activitystreams",
882 "id" => remote_user.ap_id <> "/activities/delete",
883 "actor" => remote_user.ap_id,
885 "object" => remote_user.ap_id
888 remote_user_url = remote_user.ap_id
891 %{method: :get, url: ^remote_user_url} ->
892 %Tesla.Env{status: 404, body: ""}
895 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
896 ObanHelpers.perform_all()
898 assert Enum.empty?(Notification.for_user(local_user))
901 @tag capture_log: true
902 test "move activity generates a notification" do
903 %{ap_id: old_ap_id} = old_user = insert(:user)
904 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
905 follower = insert(:user)
906 other_follower = insert(:user, %{allow_following_move: false})
908 User.follow(follower, old_user)
909 User.follow(other_follower, old_user)
911 old_user_url = old_user.ap_id
914 File.read!("test/fixtures/users_mock/localhost.json")
915 |> String.replace("{{nickname}}", old_user.nickname)
919 %{method: :get, url: ^old_user_url} ->
920 %Tesla.Env{status: 200, body: body}
923 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
924 ObanHelpers.perform_all()
929 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
932 ] = Notification.for_user(follower)
937 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
940 ] = Notification.for_user(other_follower)
944 describe "for_user" do
945 test "it returns notifications for muted user without notifications" do
947 muted = insert(:user)
948 {:ok, _user_relationships} = User.mute(user, muted, false)
950 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
952 assert length(Notification.for_user(user)) == 1
955 test "it doesn't return notifications for muted user with notifications" do
957 muted = insert(:user)
958 {:ok, _user_relationships} = User.mute(user, muted)
960 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
962 assert Notification.for_user(user) == []
965 test "it doesn't return notifications for blocked user" do
967 blocked = insert(:user)
968 {:ok, _user_relationship} = User.block(user, blocked)
970 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
972 assert Notification.for_user(user) == []
975 test "it doesn't return notifications for domain-blocked non-followed user" do
977 blocked = insert(:user, ap_id: "http://some-domain.com")
978 {:ok, user} = User.block_domain(user, "some-domain.com")
980 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
982 assert Notification.for_user(user) == []
985 test "it returns notifications for domain-blocked but followed user" do
987 blocked = insert(:user, ap_id: "http://some-domain.com")
989 {:ok, user} = User.block_domain(user, "some-domain.com")
990 {:ok, _} = User.follow(user, blocked)
992 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
994 assert length(Notification.for_user(user)) == 1
997 test "it doesn't return notifications for muted thread" do
999 another_user = insert(:user)
1001 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
1003 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
1004 assert Notification.for_user(user) == []
1007 test "it returns notifications from a muted user when with_muted is set" do
1008 user = insert(:user)
1009 muted = insert(:user)
1010 {:ok, _user_relationships} = User.mute(user, muted)
1012 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
1014 assert length(Notification.for_user(user, %{with_muted: true})) == 1
1017 test "it doesn't return notifications from a blocked user when with_muted is set" do
1018 user = insert(:user)
1019 blocked = insert(:user)
1020 {:ok, _user_relationship} = User.block(user, blocked)
1022 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1024 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1027 test "when with_muted is set, " <>
1028 "it doesn't return notifications from a domain-blocked non-followed user" do
1029 user = insert(:user)
1030 blocked = insert(:user, ap_id: "http://some-domain.com")
1031 {:ok, user} = User.block_domain(user, "some-domain.com")
1033 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
1035 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
1038 test "it returns notifications from muted threads when with_muted is set" do
1039 user = insert(:user)
1040 another_user = insert(:user)
1042 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
1044 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
1045 assert length(Notification.for_user(user, %{with_muted: true})) == 1