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
10 alias Pleroma.Notification
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.Streamer
17 describe "create_notifications" do
18 test "creates a notification for an emoji reaction" do
20 other_user = insert(:user)
22 {:ok, activity} = CommonAPI.post(user, %{"status" => "yeah"})
23 {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
25 {:ok, [notification]} = Notification.create_notifications(activity)
27 assert notification.user_id == user.id
30 test "notifies someone when they are directly addressed" do
32 other_user = insert(:user)
33 third_user = insert(:user)
36 CommonAPI.post(user, %{
37 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
40 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
42 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
43 assert notified_ids == [other_user.id, third_user.id]
44 assert notification.activity_id == activity.id
45 assert other_notification.activity_id == activity.id
48 test "it creates a notification for subscribed users" do
50 subscriber = insert(:user)
52 User.subscribe(subscriber, user)
54 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
55 {:ok, [notification]} = Notification.create_notifications(status)
57 assert notification.user_id == subscriber.id
60 test "does not create a notification for subscribed users if status is a reply" do
62 other_user = insert(:user)
63 subscriber = insert(:user)
65 User.subscribe(subscriber, other_user)
67 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
69 {:ok, _reply_activity} =
70 CommonAPI.post(other_user, %{
71 "status" => "test reply",
72 "in_reply_to_status_id" => activity.id
75 user_notifications = Notification.for_user(user)
76 assert length(user_notifications) == 1
78 subscriber_notifications = Notification.for_user(subscriber)
79 assert Enum.empty?(subscriber_notifications)
83 describe "create_notification" do
84 @tag needs_streamer: true
85 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
87 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
88 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
89 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
93 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
96 activity = insert(:note_activity)
98 notify = Notification.create_notification(activity, user)
99 assert notify.user_id == user.id
101 Task.await(task_user_notification)
104 test "it creates a notification for user if the user blocks the activity author" do
105 activity = insert(:note_activity)
106 author = User.get_cached_by_ap_id(activity.data["actor"])
108 {:ok, _user_relationship} = User.block(user, author)
110 assert Notification.create_notification(activity, user)
113 test "it creates a notification for the user if the user mutes the activity author" do
114 muter = insert(:user)
115 muted = insert(:user)
116 {:ok, _} = User.mute(muter, muted)
117 muter = Repo.get(User, muter.id)
118 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
120 assert Notification.create_notification(activity, muter)
123 test "notification created if user is muted without notifications" do
124 muter = insert(:user)
125 muted = insert(:user)
127 {:ok, _user_relationships} = User.mute(muter, muted, false)
129 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
131 assert Notification.create_notification(activity, muter)
134 test "it creates a notification for an activity from a muted thread" do
135 muter = insert(:user)
136 other_user = insert(:user)
137 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
138 CommonAPI.add_mute(muter, activity)
141 CommonAPI.post(other_user, %{
142 "status" => "Hi @#{muter.nickname}",
143 "in_reply_to_status_id" => activity.id
146 assert Notification.create_notification(activity, muter)
149 test "it disables notifications from followers" do
150 follower = insert(:user)
153 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
155 User.follow(follower, followed)
156 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
157 refute Notification.create_notification(activity, followed)
160 test "it disables notifications from non-followers" do
161 follower = insert(:user)
165 notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
168 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
169 refute Notification.create_notification(activity, followed)
172 test "it disables notifications from people the user follows" do
174 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
176 followed = insert(:user)
177 User.follow(follower, followed)
178 follower = Repo.get(User, follower.id)
179 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
180 refute Notification.create_notification(activity, follower)
183 test "it disables notifications from people the user does not follow" do
185 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
187 followed = insert(:user)
188 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
189 refute Notification.create_notification(activity, follower)
192 test "it doesn't create a notification for user if he is the activity author" do
193 activity = insert(:note_activity)
194 author = User.get_cached_by_ap_id(activity.data["actor"])
196 refute Notification.create_notification(activity, author)
199 test "it doesn't create a notification for follow-unfollow-follow chains" do
201 followed_user = insert(:user)
202 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
203 Notification.create_notification(activity, followed_user)
204 CommonAPI.unfollow(user, followed_user)
205 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
206 refute Notification.create_notification(activity_dupe, followed_user)
209 test "it doesn't create duplicate notifications for follow+subscribed users" do
211 subscriber = insert(:user)
213 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
214 User.subscribe(subscriber, user)
215 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
216 {:ok, [_notif]} = Notification.create_notifications(status)
219 test "it doesn't create subscription notifications if the recipient cannot see the status" do
221 subscriber = insert(:user)
223 User.subscribe(subscriber, user)
225 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
227 assert {:ok, []} == Notification.create_notifications(status)
231 describe "get notification" do
232 test "it gets a notification that belongs to the user" do
234 other_user = insert(:user)
236 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
238 {:ok, [notification]} = Notification.create_notifications(activity)
239 {:ok, notification} = Notification.get(other_user, notification.id)
241 assert notification.user_id == other_user.id
244 test "it returns error if the notification doesn't belong to the user" do
246 other_user = insert(:user)
248 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
250 {:ok, [notification]} = Notification.create_notifications(activity)
251 {:error, _notification} = Notification.get(user, notification.id)
255 describe "dismiss notification" do
256 test "it dismisses a notification that belongs to the user" do
258 other_user = insert(:user)
260 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
262 {:ok, [notification]} = Notification.create_notifications(activity)
263 {:ok, notification} = Notification.dismiss(other_user, notification.id)
265 assert notification.user_id == other_user.id
268 test "it returns error if the notification doesn't belong to the user" do
270 other_user = insert(:user)
272 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
274 {:ok, [notification]} = Notification.create_notifications(activity)
275 {:error, _notification} = Notification.dismiss(user, notification.id)
279 describe "clear notification" do
280 test "it clears all notifications belonging to the user" do
282 other_user = insert(:user)
283 third_user = insert(:user)
286 CommonAPI.post(user, %{
287 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
290 {:ok, _notifs} = Notification.create_notifications(activity)
293 CommonAPI.post(user, %{
294 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
297 {:ok, _notifs} = Notification.create_notifications(activity)
298 Notification.clear(other_user)
300 assert Notification.for_user(other_user) == []
301 assert Notification.for_user(third_user) != []
305 describe "set_read_up_to()" do
306 test "it sets all notifications as read up to a specified notification ID" do
308 other_user = insert(:user)
311 CommonAPI.post(user, %{
312 "status" => "hey @#{other_user.nickname}!"
316 CommonAPI.post(user, %{
317 "status" => "hey again @#{other_user.nickname}!"
320 [n2, n1] = notifs = Notification.for_user(other_user)
321 assert length(notifs) == 2
326 CommonAPI.post(user, %{
327 "status" => "hey yet again @#{other_user.nickname}!"
330 Notification.set_read_up_to(other_user, n2.id)
332 [n3, n2, n1] = Notification.for_user(other_user)
334 assert n1.seen == true
335 assert n2.seen == true
336 assert n3.seen == false
340 describe "for_user_since/2" do
341 defp days_ago(days) do
343 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
344 -days * 60 * 60 * 24,
349 test "Returns recent notifications" do
350 user1 = insert(:user)
351 user2 = insert(:user)
353 Enum.each(0..10, fn i ->
355 CommonAPI.post(user1, %{
356 "status" => "hey ##{i} @#{user2.nickname}!"
360 {old, new} = Enum.split(Notification.for_user(user2), 5)
362 Enum.each(old, fn notification ->
364 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
365 |> Pleroma.Repo.update!()
368 recent_notifications_ids =
370 |> Notification.for_user_since(
371 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
375 Enum.each(old, fn %{id: id} ->
376 refute id in recent_notifications_ids
379 Enum.each(new, fn %{id: id} ->
380 assert id in recent_notifications_ids
385 describe "notification target determination" do
386 test "it sends notifications to addressed users in new messages" do
388 other_user = insert(:user)
391 CommonAPI.post(user, %{
392 "status" => "hey @#{other_user.nickname}!"
395 assert other_user in Notification.get_notified_from_activity(activity)
398 test "it sends notifications to mentioned users in new messages" do
400 other_user = insert(:user)
403 "@context" => "https://www.w3.org/ns/activitystreams",
405 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
406 "actor" => user.ap_id,
409 "content" => "message with a Mention tag, but no explicit tagging",
413 "href" => other_user.ap_id,
414 "name" => other_user.nickname
417 "attributedTo" => user.ap_id
421 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
423 assert other_user in Notification.get_notified_from_activity(activity)
426 test "it does not send notifications to users who are only cc in new messages" do
428 other_user = insert(:user)
431 "@context" => "https://www.w3.org/ns/activitystreams",
433 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
434 "cc" => [other_user.ap_id],
435 "actor" => user.ap_id,
438 "content" => "hi everyone",
439 "attributedTo" => user.ap_id
443 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
445 assert other_user not in Notification.get_notified_from_activity(activity)
448 test "it does not send notification to mentioned users in likes" do
450 other_user = insert(:user)
451 third_user = insert(:user)
453 {:ok, activity_one} =
454 CommonAPI.post(user, %{
455 "status" => "hey @#{other_user.nickname}!"
458 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
460 assert other_user not in Notification.get_notified_from_activity(activity_two)
463 test "it does not send notification to mentioned users in announces" do
465 other_user = insert(:user)
466 third_user = insert(:user)
468 {:ok, activity_one} =
469 CommonAPI.post(user, %{
470 "status" => "hey @#{other_user.nickname}!"
473 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
475 assert other_user not in Notification.get_notified_from_activity(activity_two)
479 describe "notification lifecycle" do
480 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
482 other_user = insert(:user)
484 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
486 assert Enum.empty?(Notification.for_user(user))
488 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
490 assert length(Notification.for_user(user)) == 1
492 {:ok, _} = CommonAPI.delete(activity.id, user)
494 assert Enum.empty?(Notification.for_user(user))
497 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
499 other_user = insert(:user)
501 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
503 assert Enum.empty?(Notification.for_user(user))
505 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
507 assert length(Notification.for_user(user)) == 1
509 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
511 assert Enum.empty?(Notification.for_user(user))
514 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
516 other_user = insert(:user)
518 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
520 assert Enum.empty?(Notification.for_user(user))
522 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
524 assert length(Notification.for_user(user)) == 1
526 {:ok, _} = CommonAPI.delete(activity.id, user)
528 assert Enum.empty?(Notification.for_user(user))
531 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
533 other_user = insert(:user)
535 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
537 assert Enum.empty?(Notification.for_user(user))
539 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
541 assert length(Notification.for_user(user)) == 1
543 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
545 assert Enum.empty?(Notification.for_user(user))
548 test "liking an activity which is already deleted does not generate a notification" do
550 other_user = insert(:user)
552 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
554 assert Enum.empty?(Notification.for_user(user))
556 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
558 assert Enum.empty?(Notification.for_user(user))
560 {:error, _} = CommonAPI.favorite(activity.id, other_user)
562 assert Enum.empty?(Notification.for_user(user))
565 test "repeating an activity which is already deleted does not generate a notification" do
567 other_user = insert(:user)
569 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
571 assert Enum.empty?(Notification.for_user(user))
573 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
575 assert Enum.empty?(Notification.for_user(user))
577 {:error, _} = CommonAPI.repeat(activity.id, other_user)
579 assert Enum.empty?(Notification.for_user(user))
582 test "replying to a deleted post without tagging does not generate a notification" do
584 other_user = insert(:user)
586 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
587 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
589 {:ok, _reply_activity} =
590 CommonAPI.post(other_user, %{
591 "status" => "test reply",
592 "in_reply_to_status_id" => activity.id
595 assert Enum.empty?(Notification.for_user(user))
598 test "notifications are deleted if a local user is deleted" do
600 other_user = insert(:user)
603 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
605 refute Enum.empty?(Notification.for_user(other_user))
607 {:ok, job} = User.delete(user)
608 ObanHelpers.perform(job)
610 assert Enum.empty?(Notification.for_user(other_user))
613 test "notifications are deleted if a remote user is deleted" do
614 remote_user = insert(:user)
615 local_user = insert(:user)
618 "@context" => "https://www.w3.org/ns/activitystreams",
620 "actor" => remote_user.ap_id,
621 "id" => remote_user.ap_id <> "/activities/test",
622 "to" => [local_user.ap_id],
626 "content" => "Hello!",
630 "href" => local_user.ap_id,
631 "name" => "@#{local_user.nickname}"
634 "to" => [local_user.ap_id],
636 "attributedTo" => remote_user.ap_id
640 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
642 refute Enum.empty?(Notification.for_user(local_user))
644 delete_user_message = %{
645 "@context" => "https://www.w3.org/ns/activitystreams",
646 "id" => remote_user.ap_id <> "/activities/delete",
647 "actor" => remote_user.ap_id,
649 "object" => remote_user.ap_id
652 remote_user_url = remote_user.ap_id
655 %{method: :get, url: ^remote_user_url} ->
656 %Tesla.Env{status: 404, body: ""}
659 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
660 ObanHelpers.perform_all()
662 assert Enum.empty?(Notification.for_user(local_user))
665 @tag capture_log: true
666 test "move activity generates a notification" do
667 %{ap_id: old_ap_id} = old_user = insert(:user)
668 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
669 follower = insert(:user)
670 other_follower = insert(:user, %{allow_following_move: false})
672 User.follow(follower, old_user)
673 User.follow(other_follower, old_user)
675 old_user_url = old_user.ap_id
678 File.read!("test/fixtures/users_mock/localhost.json")
679 |> String.replace("{{nickname}}", old_user.nickname)
683 %{method: :get, url: ^old_user_url} ->
684 %Tesla.Env{status: 200, body: body}
687 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
688 ObanHelpers.perform_all()
693 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
696 ] = Notification.for_user(follower)
701 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
704 ] = Notification.for_user(other_follower)
708 describe "for_user" do
709 test "it returns notifications for muted user without notifications" do
711 muted = insert(:user)
712 {:ok, _user_relationships} = User.mute(user, muted, false)
714 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
716 assert length(Notification.for_user(user)) == 1
719 test "it doesn't return notifications for muted user with notifications" do
721 muted = insert(:user)
722 {:ok, _user_relationships} = User.mute(user, muted)
724 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
726 assert Notification.for_user(user) == []
729 test "it doesn't return notifications for blocked user" do
731 blocked = insert(:user)
732 {:ok, _user_relationship} = User.block(user, blocked)
734 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
736 assert Notification.for_user(user) == []
739 test "it doesn't return notificatitons for blocked domain" do
741 blocked = insert(:user, ap_id: "http://some-domain.com")
742 {:ok, user} = User.block_domain(user, "some-domain.com")
744 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
746 assert Notification.for_user(user) == []
749 test "it doesn't return notifications for muted thread" do
751 another_user = insert(:user)
753 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
755 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
756 assert Notification.for_user(user) == []
759 test "it returns notifications from a muted user when with_muted is set" do
761 muted = insert(:user)
762 {:ok, _user_relationships} = User.mute(user, muted)
764 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
766 assert length(Notification.for_user(user, %{with_muted: true})) == 1
769 test "it doesn't return notifications from a blocked user when with_muted is set" do
771 blocked = insert(:user)
772 {:ok, _user_relationship} = User.block(user, blocked)
774 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
776 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
779 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
781 blocked = insert(:user, ap_id: "http://some-domain.com")
782 {:ok, user} = User.block_domain(user, "some-domain.com")
784 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
786 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
789 test "it returns notifications from muted threads when with_muted is set" do
791 another_user = insert(:user)
793 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
795 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
796 assert length(Notification.for_user(user, %{with_muted: true})) == 1