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{} =
345 user_id: other_user.id,
346 timeline: "notifications"
349 assert m.last_read_id == to_string(n2.id)
353 describe "for_user_since/2" do
354 defp days_ago(days) do
356 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
357 -days * 60 * 60 * 24,
362 test "Returns recent notifications" do
363 user1 = insert(:user)
364 user2 = insert(:user)
366 Enum.each(0..10, fn i ->
368 CommonAPI.post(user1, %{
369 "status" => "hey ##{i} @#{user2.nickname}!"
373 {old, new} = Enum.split(Notification.for_user(user2), 5)
375 Enum.each(old, fn notification ->
377 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
378 |> Pleroma.Repo.update!()
381 recent_notifications_ids =
383 |> Notification.for_user_since(
384 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
388 Enum.each(old, fn %{id: id} ->
389 refute id in recent_notifications_ids
392 Enum.each(new, fn %{id: id} ->
393 assert id in recent_notifications_ids
398 describe "notification target determination" do
399 test "it sends notifications to addressed users in new messages" do
401 other_user = insert(:user)
404 CommonAPI.post(user, %{
405 "status" => "hey @#{other_user.nickname}!"
408 assert other_user in Notification.get_notified_from_activity(activity)
411 test "it sends notifications to mentioned users in new messages" do
413 other_user = insert(:user)
416 "@context" => "https://www.w3.org/ns/activitystreams",
418 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
419 "actor" => user.ap_id,
422 "content" => "message with a Mention tag, but no explicit tagging",
426 "href" => other_user.ap_id,
427 "name" => other_user.nickname
430 "attributedTo" => user.ap_id
434 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
436 assert other_user in Notification.get_notified_from_activity(activity)
439 test "it does not send notifications to users who are only cc in new messages" do
441 other_user = insert(:user)
444 "@context" => "https://www.w3.org/ns/activitystreams",
446 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
447 "cc" => [other_user.ap_id],
448 "actor" => user.ap_id,
451 "content" => "hi everyone",
452 "attributedTo" => user.ap_id
456 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
458 assert other_user not in Notification.get_notified_from_activity(activity)
461 test "it does not send notification to mentioned users in likes" do
463 other_user = insert(:user)
464 third_user = insert(:user)
466 {:ok, activity_one} =
467 CommonAPI.post(user, %{
468 "status" => "hey @#{other_user.nickname}!"
471 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
473 assert other_user not in Notification.get_notified_from_activity(activity_two)
476 test "it does not send notification to mentioned users in announces" do
478 other_user = insert(:user)
479 third_user = insert(:user)
481 {:ok, activity_one} =
482 CommonAPI.post(user, %{
483 "status" => "hey @#{other_user.nickname}!"
486 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
488 assert other_user not in Notification.get_notified_from_activity(activity_two)
492 describe "notification lifecycle" do
493 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
495 other_user = insert(:user)
497 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
499 assert Enum.empty?(Notification.for_user(user))
501 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
503 assert length(Notification.for_user(user)) == 1
505 {:ok, _} = CommonAPI.delete(activity.id, user)
507 assert Enum.empty?(Notification.for_user(user))
510 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
512 other_user = insert(:user)
514 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
516 assert Enum.empty?(Notification.for_user(user))
518 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
520 assert length(Notification.for_user(user)) == 1
522 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
524 assert Enum.empty?(Notification.for_user(user))
527 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
529 other_user = insert(:user)
531 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
533 assert Enum.empty?(Notification.for_user(user))
535 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
537 assert length(Notification.for_user(user)) == 1
539 {:ok, _} = CommonAPI.delete(activity.id, user)
541 assert Enum.empty?(Notification.for_user(user))
544 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
546 other_user = insert(:user)
548 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
550 assert Enum.empty?(Notification.for_user(user))
552 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
554 assert length(Notification.for_user(user)) == 1
556 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
558 assert Enum.empty?(Notification.for_user(user))
561 test "liking an activity which is already deleted does not generate a notification" do
563 other_user = insert(:user)
565 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
567 assert Enum.empty?(Notification.for_user(user))
569 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
571 assert Enum.empty?(Notification.for_user(user))
573 {:error, _} = CommonAPI.favorite(activity.id, other_user)
575 assert Enum.empty?(Notification.for_user(user))
578 test "repeating an activity which is already deleted does not generate a notification" do
580 other_user = insert(:user)
582 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
584 assert Enum.empty?(Notification.for_user(user))
586 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
588 assert Enum.empty?(Notification.for_user(user))
590 {:error, _} = CommonAPI.repeat(activity.id, other_user)
592 assert Enum.empty?(Notification.for_user(user))
595 test "replying to a deleted post without tagging does not generate a notification" do
597 other_user = insert(:user)
599 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
600 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
602 {:ok, _reply_activity} =
603 CommonAPI.post(other_user, %{
604 "status" => "test reply",
605 "in_reply_to_status_id" => activity.id
608 assert Enum.empty?(Notification.for_user(user))
611 test "notifications are deleted if a local user is deleted" do
613 other_user = insert(:user)
616 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
618 refute Enum.empty?(Notification.for_user(other_user))
620 {:ok, job} = User.delete(user)
621 ObanHelpers.perform(job)
623 assert Enum.empty?(Notification.for_user(other_user))
626 test "notifications are deleted if a remote user is deleted" do
627 remote_user = insert(:user)
628 local_user = insert(:user)
631 "@context" => "https://www.w3.org/ns/activitystreams",
633 "actor" => remote_user.ap_id,
634 "id" => remote_user.ap_id <> "/activities/test",
635 "to" => [local_user.ap_id],
639 "content" => "Hello!",
643 "href" => local_user.ap_id,
644 "name" => "@#{local_user.nickname}"
647 "to" => [local_user.ap_id],
649 "attributedTo" => remote_user.ap_id
653 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
655 refute Enum.empty?(Notification.for_user(local_user))
657 delete_user_message = %{
658 "@context" => "https://www.w3.org/ns/activitystreams",
659 "id" => remote_user.ap_id <> "/activities/delete",
660 "actor" => remote_user.ap_id,
662 "object" => remote_user.ap_id
665 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
666 ObanHelpers.perform_all()
668 assert Enum.empty?(Notification.for_user(local_user))
671 test "move activity generates a notification" do
672 %{ap_id: old_ap_id} = old_user = insert(:user)
673 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
674 follower = insert(:user)
675 other_follower = insert(:user, %{allow_following_move: false})
677 User.follow(follower, old_user)
678 User.follow(other_follower, old_user)
680 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
681 ObanHelpers.perform_all()
683 assert [] = Notification.for_user(follower)
688 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
691 ] = Notification.for_user(follower, %{with_move: true})
693 assert [] = Notification.for_user(other_follower)
698 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
701 ] = Notification.for_user(other_follower, %{with_move: true})
705 describe "for_user" do
706 test "it returns notifications for muted user without notifications" do
708 muted = insert(:user)
709 {:ok, _user_relationships} = User.mute(user, muted, false)
711 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
713 assert length(Notification.for_user(user)) == 1
716 test "it doesn't return notifications for muted user with notifications" do
718 muted = insert(:user)
719 {:ok, _user_relationships} = User.mute(user, muted)
721 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
723 assert Notification.for_user(user) == []
726 test "it doesn't return notifications for blocked user" do
728 blocked = insert(:user)
729 {:ok, _user_relationship} = User.block(user, blocked)
731 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
733 assert Notification.for_user(user) == []
736 test "it doesn't return notificatitons for blocked domain" do
738 blocked = insert(:user, ap_id: "http://some-domain.com")
739 {:ok, user} = User.block_domain(user, "some-domain.com")
741 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
743 assert Notification.for_user(user) == []
746 test "it doesn't return notifications for muted thread" do
748 another_user = insert(:user)
750 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
752 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
753 assert Notification.for_user(user) == []
756 test "it returns notifications from a muted user when with_muted is set" do
758 muted = insert(:user)
759 {:ok, _user_relationships} = User.mute(user, muted)
761 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
763 assert length(Notification.for_user(user, %{with_muted: true})) == 1
766 test "it doesn't return notifications from a blocked user when with_muted is set" do
768 blocked = insert(:user)
769 {:ok, _user_relationship} = User.block(user, blocked)
771 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
773 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
776 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
778 blocked = insert(:user, ap_id: "http://some-domain.com")
779 {:ok, user} = User.block_domain(user, "some-domain.com")
781 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
783 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
786 test "it returns notifications from muted threads when with_muted is set" do
788 another_user = insert(:user)
790 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
792 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
793 assert length(Notification.for_user(user, %{with_muted: true})) == 1