1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
47 assert [%Pleroma.Marker{unread_count: 2}] =
48 Pleroma.Marker.get_markers(other_user, ["notifications"])
51 test "it creates a notification for subscribed users" do
53 subscriber = insert(:user)
55 User.subscribe(subscriber, user)
57 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
58 {:ok, [notification]} = Notification.create_notifications(status)
60 assert notification.user_id == subscriber.id
63 test "does not create a notification for subscribed users if status is a reply" do
65 other_user = insert(:user)
66 subscriber = insert(:user)
68 User.subscribe(subscriber, other_user)
70 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
72 {:ok, _reply_activity} =
73 CommonAPI.post(other_user, %{
74 "status" => "test reply",
75 "in_reply_to_status_id" => activity.id
78 user_notifications = Notification.for_user(user)
79 assert length(user_notifications) == 1
81 subscriber_notifications = Notification.for_user(subscriber)
82 assert Enum.empty?(subscriber_notifications)
86 describe "create_notification" do
87 @tag needs_streamer: true
88 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
90 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
91 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
92 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
96 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
99 activity = insert(:note_activity)
101 notify = Notification.create_notification(activity, user)
102 assert notify.user_id == user.id
104 Task.await(task_user_notification)
107 test "it creates a notification for user if the user blocks the activity author" do
108 activity = insert(:note_activity)
109 author = User.get_cached_by_ap_id(activity.data["actor"])
111 {:ok, _user_relationship} = User.block(user, author)
113 assert Notification.create_notification(activity, user)
116 test "it creates a notification for the user if the user mutes the activity author" do
117 muter = insert(:user)
118 muted = insert(:user)
119 {:ok, _} = User.mute(muter, muted)
120 muter = Repo.get(User, muter.id)
121 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
123 assert Notification.create_notification(activity, muter)
126 test "notification created if user is muted without notifications" do
127 muter = insert(:user)
128 muted = insert(:user)
130 {:ok, _user_relationships} = User.mute(muter, muted, false)
132 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
134 assert Notification.create_notification(activity, muter)
137 test "it creates a notification for an activity from a muted thread" do
138 muter = insert(:user)
139 other_user = insert(:user)
140 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
141 CommonAPI.add_mute(muter, activity)
144 CommonAPI.post(other_user, %{
145 "status" => "Hi @#{muter.nickname}",
146 "in_reply_to_status_id" => activity.id
149 assert Notification.create_notification(activity, muter)
152 test "it disables notifications from followers" do
153 follower = insert(:user)
156 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
158 User.follow(follower, followed)
159 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
160 refute Notification.create_notification(activity, followed)
163 test "it disables notifications from non-followers" do
164 follower = insert(:user)
168 notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
171 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
172 refute Notification.create_notification(activity, followed)
175 test "it disables notifications from people the user follows" do
177 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
179 followed = insert(:user)
180 User.follow(follower, followed)
181 follower = Repo.get(User, follower.id)
182 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
183 refute Notification.create_notification(activity, follower)
186 test "it disables notifications from people the user does not follow" do
188 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
190 followed = insert(:user)
191 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
192 refute Notification.create_notification(activity, follower)
195 test "it doesn't create a notification for user if he is the activity author" do
196 activity = insert(:note_activity)
197 author = User.get_cached_by_ap_id(activity.data["actor"])
199 refute Notification.create_notification(activity, author)
202 test "it doesn't create a notification for follow-unfollow-follow chains" do
204 followed_user = insert(:user)
205 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
206 Notification.create_notification(activity, followed_user)
207 CommonAPI.unfollow(user, followed_user)
208 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
209 refute Notification.create_notification(activity_dupe, followed_user)
212 test "it doesn't create duplicate notifications for follow+subscribed users" do
214 subscriber = insert(:user)
216 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
217 User.subscribe(subscriber, user)
218 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
219 {:ok, [_notif]} = Notification.create_notifications(status)
222 test "it doesn't create subscription notifications if the recipient cannot see the status" do
224 subscriber = insert(:user)
226 User.subscribe(subscriber, user)
228 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
230 assert {:ok, []} == Notification.create_notifications(status)
234 describe "get notification" do
235 test "it gets a notification that belongs to the user" do
237 other_user = insert(:user)
239 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
241 {:ok, [notification]} = Notification.create_notifications(activity)
242 {:ok, notification} = Notification.get(other_user, notification.id)
244 assert notification.user_id == other_user.id
247 test "it returns error if the notification doesn't belong to the user" do
249 other_user = insert(:user)
251 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
253 {:ok, [notification]} = Notification.create_notifications(activity)
254 {:error, _notification} = Notification.get(user, notification.id)
258 describe "dismiss notification" do
259 test "it dismisses a notification that belongs to the user" do
261 other_user = insert(:user)
263 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
265 {:ok, [notification]} = Notification.create_notifications(activity)
266 {:ok, notification} = Notification.dismiss(other_user, notification.id)
268 assert notification.user_id == other_user.id
271 test "it returns error if the notification doesn't belong to the user" do
273 other_user = insert(:user)
275 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
277 {:ok, [notification]} = Notification.create_notifications(activity)
278 {:error, _notification} = Notification.dismiss(user, notification.id)
282 describe "clear notification" do
283 test "it clears all notifications belonging to the user" do
285 other_user = insert(:user)
286 third_user = insert(:user)
289 CommonAPI.post(user, %{
290 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
293 {:ok, _notifs} = Notification.create_notifications(activity)
296 CommonAPI.post(user, %{
297 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
300 {:ok, _notifs} = Notification.create_notifications(activity)
301 Notification.clear(other_user)
303 assert Notification.for_user(other_user) == []
304 assert Notification.for_user(third_user) != []
308 describe "set_read_up_to()" do
309 test "it sets all notifications as read up to a specified notification ID" do
311 other_user = insert(:user)
314 CommonAPI.post(user, %{
315 "status" => "hey @#{other_user.nickname}!"
319 CommonAPI.post(user, %{
320 "status" => "hey again @#{other_user.nickname}!"
323 [n2, n1] = notifs = Notification.for_user(other_user)
324 assert length(notifs) == 2
329 CommonAPI.post(user, %{
330 "status" => "hey yet again @#{other_user.nickname}!"
333 Notification.set_read_up_to(other_user, n2.id)
335 [n3, n2, n1] = Notification.for_user(other_user)
337 assert n1.seen == true
338 assert n2.seen == true
339 assert n3.seen == false
341 assert %Pleroma.Marker{unread_count: 1} =
344 user_id: other_user.id,
345 timeline: "notifications"
350 describe "for_user_since/2" do
351 defp days_ago(days) do
353 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
354 -days * 60 * 60 * 24,
359 test "Returns recent notifications" do
360 user1 = insert(:user)
361 user2 = insert(:user)
363 Enum.each(0..10, fn i ->
365 CommonAPI.post(user1, %{
366 "status" => "hey ##{i} @#{user2.nickname}!"
370 {old, new} = Enum.split(Notification.for_user(user2), 5)
372 Enum.each(old, fn notification ->
374 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
375 |> Pleroma.Repo.update!()
378 recent_notifications_ids =
380 |> Notification.for_user_since(
381 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
385 Enum.each(old, fn %{id: id} ->
386 refute id in recent_notifications_ids
389 Enum.each(new, fn %{id: id} ->
390 assert id in recent_notifications_ids
395 describe "notification target determination" do
396 test "it sends notifications to addressed users in new messages" do
398 other_user = insert(:user)
401 CommonAPI.post(user, %{
402 "status" => "hey @#{other_user.nickname}!"
405 assert other_user in Notification.get_notified_from_activity(activity)
408 test "it sends notifications to mentioned users in new messages" do
410 other_user = insert(:user)
413 "@context" => "https://www.w3.org/ns/activitystreams",
415 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
416 "actor" => user.ap_id,
419 "content" => "message with a Mention tag, but no explicit tagging",
423 "href" => other_user.ap_id,
424 "name" => other_user.nickname
427 "attributedTo" => user.ap_id
431 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
433 assert other_user in Notification.get_notified_from_activity(activity)
436 test "it does not send notifications to users who are only cc in new messages" do
438 other_user = insert(:user)
441 "@context" => "https://www.w3.org/ns/activitystreams",
443 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
444 "cc" => [other_user.ap_id],
445 "actor" => user.ap_id,
448 "content" => "hi everyone",
449 "attributedTo" => user.ap_id
453 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
455 assert other_user not in Notification.get_notified_from_activity(activity)
458 test "it does not send notification to mentioned users in likes" do
460 other_user = insert(:user)
461 third_user = insert(:user)
463 {:ok, activity_one} =
464 CommonAPI.post(user, %{
465 "status" => "hey @#{other_user.nickname}!"
468 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
470 assert other_user not in Notification.get_notified_from_activity(activity_two)
473 test "it does not send notification to mentioned users in announces" do
475 other_user = insert(:user)
476 third_user = insert(:user)
478 {:ok, activity_one} =
479 CommonAPI.post(user, %{
480 "status" => "hey @#{other_user.nickname}!"
483 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
485 assert other_user not in Notification.get_notified_from_activity(activity_two)
489 describe "notification lifecycle" do
490 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
492 other_user = insert(:user)
494 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
496 assert Enum.empty?(Notification.for_user(user))
498 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
500 assert length(Notification.for_user(user)) == 1
502 {:ok, _} = CommonAPI.delete(activity.id, user)
504 assert Enum.empty?(Notification.for_user(user))
507 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
509 other_user = insert(:user)
511 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
513 assert Enum.empty?(Notification.for_user(user))
515 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
517 assert length(Notification.for_user(user)) == 1
519 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
521 assert Enum.empty?(Notification.for_user(user))
524 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
526 other_user = insert(:user)
528 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
530 assert Enum.empty?(Notification.for_user(user))
532 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
534 assert length(Notification.for_user(user)) == 1
536 {:ok, _} = CommonAPI.delete(activity.id, user)
538 assert Enum.empty?(Notification.for_user(user))
541 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
543 other_user = insert(:user)
545 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
547 assert Enum.empty?(Notification.for_user(user))
549 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
551 assert length(Notification.for_user(user)) == 1
553 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
555 assert Enum.empty?(Notification.for_user(user))
558 test "liking an activity which is already deleted does not generate a notification" do
560 other_user = insert(:user)
562 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
564 assert Enum.empty?(Notification.for_user(user))
566 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
568 assert Enum.empty?(Notification.for_user(user))
570 {:error, _} = CommonAPI.favorite(activity.id, other_user)
572 assert Enum.empty?(Notification.for_user(user))
575 test "repeating an activity which is already deleted does not generate a notification" do
577 other_user = insert(:user)
579 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
581 assert Enum.empty?(Notification.for_user(user))
583 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
585 assert Enum.empty?(Notification.for_user(user))
587 {:error, _} = CommonAPI.repeat(activity.id, other_user)
589 assert Enum.empty?(Notification.for_user(user))
592 test "replying to a deleted post without tagging does not generate a notification" do
594 other_user = insert(:user)
596 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
597 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
599 {:ok, _reply_activity} =
600 CommonAPI.post(other_user, %{
601 "status" => "test reply",
602 "in_reply_to_status_id" => activity.id
605 assert Enum.empty?(Notification.for_user(user))
608 test "notifications are deleted if a local user is deleted" do
610 other_user = insert(:user)
613 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
615 refute Enum.empty?(Notification.for_user(other_user))
617 {:ok, job} = User.delete(user)
618 ObanHelpers.perform(job)
620 assert Enum.empty?(Notification.for_user(other_user))
623 test "notifications are deleted if a remote user is deleted" do
624 remote_user = insert(:user)
625 local_user = insert(:user)
628 "@context" => "https://www.w3.org/ns/activitystreams",
630 "actor" => remote_user.ap_id,
631 "id" => remote_user.ap_id <> "/activities/test",
632 "to" => [local_user.ap_id],
636 "content" => "Hello!",
640 "href" => local_user.ap_id,
641 "name" => "@#{local_user.nickname}"
644 "to" => [local_user.ap_id],
646 "attributedTo" => remote_user.ap_id
650 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
652 refute Enum.empty?(Notification.for_user(local_user))
654 delete_user_message = %{
655 "@context" => "https://www.w3.org/ns/activitystreams",
656 "id" => remote_user.ap_id <> "/activities/delete",
657 "actor" => remote_user.ap_id,
659 "object" => remote_user.ap_id
662 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
663 ObanHelpers.perform_all()
665 assert Enum.empty?(Notification.for_user(local_user))
668 test "move activity generates a notification" do
669 %{ap_id: old_ap_id} = old_user = insert(:user)
670 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
671 follower = insert(:user)
672 other_follower = insert(:user, %{allow_following_move: false})
674 User.follow(follower, old_user)
675 User.follow(other_follower, old_user)
677 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
678 ObanHelpers.perform_all()
680 assert [] = Notification.for_user(follower)
685 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
688 ] = Notification.for_user(follower, %{with_move: true})
690 assert [] = Notification.for_user(other_follower)
695 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
698 ] = Notification.for_user(other_follower, %{with_move: true})
702 describe "for_user" do
703 test "it returns notifications for muted user without notifications" do
705 muted = insert(:user)
706 {:ok, _user_relationships} = User.mute(user, muted, false)
708 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
710 assert length(Notification.for_user(user)) == 1
713 test "it doesn't return notifications for muted user with notifications" do
715 muted = insert(:user)
716 {:ok, _user_relationships} = User.mute(user, muted)
718 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
720 assert Notification.for_user(user) == []
723 test "it doesn't return notifications for blocked user" do
725 blocked = insert(:user)
726 {:ok, _user_relationship} = User.block(user, blocked)
728 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
730 assert Notification.for_user(user) == []
733 test "it doesn't return notificatitons for blocked domain" do
735 blocked = insert(:user, ap_id: "http://some-domain.com")
736 {:ok, user} = User.block_domain(user, "some-domain.com")
738 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
740 assert Notification.for_user(user) == []
743 test "it doesn't return notifications for muted thread" do
745 another_user = insert(:user)
747 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
749 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
750 assert Notification.for_user(user) == []
753 test "it returns notifications from a muted user when with_muted is set" do
755 muted = insert(:user)
756 {:ok, _user_relationships} = User.mute(user, muted)
758 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
760 assert length(Notification.for_user(user, %{with_muted: true})) == 1
763 test "it doesn't return notifications from a blocked user when with_muted is set" do
765 blocked = insert(:user)
766 {:ok, _user_relationship} = User.block(user, blocked)
768 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
770 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
773 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
775 blocked = insert(:user, ap_id: "http://some-domain.com")
776 {:ok, user} = User.block_domain(user, "some-domain.com")
778 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
780 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
783 test "it returns notifications from muted threads when with_muted is set" do
785 another_user = insert(:user)
787 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
789 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
790 assert length(Notification.for_user(user, %{with_muted: true})) == 1