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 "notifies someone when they are directly addressed" do
20 other_user = insert(:user)
21 third_user = insert(:user)
24 CommonAPI.post(user, %{
25 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
28 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
30 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
31 assert notified_ids == [other_user.id, third_user.id]
32 assert notification.activity_id == activity.id
33 assert other_notification.activity_id == activity.id
36 test "it creates a notification for subscribed users" do
38 subscriber = insert(:user)
40 User.subscribe(subscriber, user)
42 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
43 {:ok, [notification]} = Notification.create_notifications(status)
45 assert notification.user_id == subscriber.id
48 test "does not create a notification for subscribed users if status is a reply" do
50 other_user = insert(:user)
51 subscriber = insert(:user)
53 User.subscribe(subscriber, other_user)
55 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
57 {:ok, _reply_activity} =
58 CommonAPI.post(other_user, %{
59 "status" => "test reply",
60 "in_reply_to_status_id" => activity.id
63 user_notifications = Notification.for_user(user)
64 assert length(user_notifications) == 1
66 subscriber_notifications = Notification.for_user(subscriber)
67 assert Enum.empty?(subscriber_notifications)
71 describe "create_notification" do
72 @tag needs_streamer: true
73 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
75 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
76 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
77 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
81 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
84 activity = insert(:note_activity)
86 notify = Notification.create_notification(activity, user)
87 assert notify.user_id == user.id
89 Task.await(task_user_notification)
92 test "it creates a notification for user if the user blocks the activity author" do
93 activity = insert(:note_activity)
94 author = User.get_cached_by_ap_id(activity.data["actor"])
96 {:ok, user} = User.block(user, author)
98 assert Notification.create_notification(activity, user)
101 test "it creates a notificatin for the user if the user mutes the activity author" do
102 muter = insert(:user)
103 muted = insert(:user)
104 {:ok, _} = User.mute(muter, muted)
105 muter = Repo.get(User, muter.id)
106 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
108 assert Notification.create_notification(activity, muter)
111 test "notification created if user is muted without notifications" do
112 muter = insert(:user)
113 muted = insert(:user)
115 {:ok, muter} = User.mute(muter, muted, false)
117 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
119 assert Notification.create_notification(activity, muter)
122 test "it creates a notification for an activity from a muted thread" do
123 muter = insert(:user)
124 other_user = insert(:user)
125 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
126 CommonAPI.add_mute(muter, activity)
129 CommonAPI.post(other_user, %{
130 "status" => "Hi @#{muter.nickname}",
131 "in_reply_to_status_id" => activity.id
134 assert Notification.create_notification(activity, muter)
137 test "it disables notifications from followers" do
138 follower = insert(:user)
139 followed = insert(:user, notification_settings: %{"followers" => false})
140 User.follow(follower, followed)
141 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
142 refute Notification.create_notification(activity, followed)
145 test "it disables notifications from non-followers" do
146 follower = insert(:user)
147 followed = insert(:user, notification_settings: %{"non_followers" => false})
148 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
149 refute Notification.create_notification(activity, followed)
152 test "it disables notifications from people the user follows" do
153 follower = insert(:user, notification_settings: %{"follows" => false})
154 followed = insert(:user)
155 User.follow(follower, followed)
156 follower = Repo.get(User, follower.id)
157 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
158 refute Notification.create_notification(activity, follower)
161 test "it disables notifications from people the user does not follow" do
162 follower = insert(:user, notification_settings: %{"non_follows" => false})
163 followed = insert(:user)
164 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
165 refute Notification.create_notification(activity, follower)
168 test "it doesn't create a notification for user if he is the activity author" do
169 activity = insert(:note_activity)
170 author = User.get_cached_by_ap_id(activity.data["actor"])
172 refute Notification.create_notification(activity, author)
175 test "it doesn't create a notification for follow-unfollow-follow chains" do
177 followed_user = insert(:user)
178 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
179 Notification.create_notification(activity, followed_user)
180 CommonAPI.unfollow(user, followed_user)
181 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
182 refute Notification.create_notification(activity_dupe, followed_user)
185 test "it doesn't create duplicate notifications for follow+subscribed users" do
187 subscriber = insert(:user)
189 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
190 User.subscribe(subscriber, user)
191 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
192 {:ok, [_notif]} = Notification.create_notifications(status)
195 test "it doesn't create subscription notifications if the recipient cannot see the status" do
197 subscriber = insert(:user)
199 User.subscribe(subscriber, user)
201 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
203 assert {:ok, []} == Notification.create_notifications(status)
207 describe "get notification" do
208 test "it gets a notification that belongs to the user" do
210 other_user = insert(:user)
212 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
214 {:ok, [notification]} = Notification.create_notifications(activity)
215 {:ok, notification} = Notification.get(other_user, notification.id)
217 assert notification.user_id == other_user.id
220 test "it returns error if the notification doesn't belong to the user" do
222 other_user = insert(:user)
224 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
226 {:ok, [notification]} = Notification.create_notifications(activity)
227 {:error, _notification} = Notification.get(user, notification.id)
231 describe "dismiss notification" do
232 test "it dismisses 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.dismiss(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.dismiss(user, notification.id)
255 describe "clear notification" do
256 test "it clears all notifications belonging to the user" do
258 other_user = insert(:user)
259 third_user = insert(:user)
262 CommonAPI.post(user, %{
263 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
266 {:ok, _notifs} = Notification.create_notifications(activity)
269 CommonAPI.post(user, %{
270 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
273 {:ok, _notifs} = Notification.create_notifications(activity)
274 Notification.clear(other_user)
276 assert Notification.for_user(other_user) == []
277 assert Notification.for_user(third_user) != []
281 describe "set_read_up_to()" do
282 test "it sets all notifications as read up to a specified notification ID" do
284 other_user = insert(:user)
287 CommonAPI.post(user, %{
288 "status" => "hey @#{other_user.nickname}!"
292 CommonAPI.post(user, %{
293 "status" => "hey again @#{other_user.nickname}!"
296 [n2, n1] = notifs = Notification.for_user(other_user)
297 assert length(notifs) == 2
302 CommonAPI.post(user, %{
303 "status" => "hey yet again @#{other_user.nickname}!"
306 Notification.set_read_up_to(other_user, n2.id)
308 [n3, n2, n1] = Notification.for_user(other_user)
310 assert n1.seen == true
311 assert n2.seen == true
312 assert n3.seen == false
316 describe "for_user_since/2" do
317 defp days_ago(days) do
319 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
320 -days * 60 * 60 * 24,
325 test "Returns recent notifications" do
326 user1 = insert(:user)
327 user2 = insert(:user)
329 Enum.each(0..10, fn i ->
331 CommonAPI.post(user1, %{
332 "status" => "hey ##{i} @#{user2.nickname}!"
336 {old, new} = Enum.split(Notification.for_user(user2), 5)
338 Enum.each(old, fn notification ->
340 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
341 |> Pleroma.Repo.update!()
344 recent_notifications_ids =
346 |> Notification.for_user_since(
347 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
351 Enum.each(old, fn %{id: id} ->
352 refute id in recent_notifications_ids
355 Enum.each(new, fn %{id: id} ->
356 assert id in recent_notifications_ids
361 describe "notification target determination" do
362 test "it sends notifications to addressed users in new messages" do
364 other_user = insert(:user)
367 CommonAPI.post(user, %{
368 "status" => "hey @#{other_user.nickname}!"
371 assert other_user in Notification.get_notified_from_activity(activity)
374 test "it sends notifications to mentioned users in new messages" do
376 other_user = insert(:user)
379 "@context" => "https://www.w3.org/ns/activitystreams",
381 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
382 "actor" => user.ap_id,
385 "content" => "message with a Mention tag, but no explicit tagging",
389 "href" => other_user.ap_id,
390 "name" => other_user.nickname
393 "attributedTo" => user.ap_id
397 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
399 assert other_user in Notification.get_notified_from_activity(activity)
402 test "it does not send notifications to users who are only cc in new messages" do
404 other_user = insert(:user)
407 "@context" => "https://www.w3.org/ns/activitystreams",
409 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
410 "cc" => [other_user.ap_id],
411 "actor" => user.ap_id,
414 "content" => "hi everyone",
415 "attributedTo" => user.ap_id
419 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
421 assert other_user not in Notification.get_notified_from_activity(activity)
424 test "it does not send notification to mentioned users in likes" do
426 other_user = insert(:user)
427 third_user = insert(:user)
429 {:ok, activity_one} =
430 CommonAPI.post(user, %{
431 "status" => "hey @#{other_user.nickname}!"
434 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
436 assert other_user not in Notification.get_notified_from_activity(activity_two)
439 test "it does not send notification to mentioned users in announces" do
441 other_user = insert(:user)
442 third_user = insert(:user)
444 {:ok, activity_one} =
445 CommonAPI.post(user, %{
446 "status" => "hey @#{other_user.nickname}!"
449 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
451 assert other_user not in Notification.get_notified_from_activity(activity_two)
455 describe "notification lifecycle" do
456 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
458 other_user = insert(:user)
460 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
462 assert Enum.empty?(Notification.for_user(user))
464 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
466 assert length(Notification.for_user(user)) == 1
468 {:ok, _} = CommonAPI.delete(activity.id, user)
470 assert Enum.empty?(Notification.for_user(user))
473 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
475 other_user = insert(:user)
477 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
479 assert Enum.empty?(Notification.for_user(user))
481 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
483 assert length(Notification.for_user(user)) == 1
485 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
487 assert Enum.empty?(Notification.for_user(user))
490 test "repeating 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.repeat(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 "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" 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.repeat(activity.id, other_user)
517 assert length(Notification.for_user(user)) == 1
519 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
521 assert Enum.empty?(Notification.for_user(user))
524 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
534 assert Enum.empty?(Notification.for_user(user))
536 {:error, _} = CommonAPI.favorite(activity.id, other_user)
538 assert Enum.empty?(Notification.for_user(user))
541 test "repeating an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
551 assert Enum.empty?(Notification.for_user(user))
553 {:error, _} = CommonAPI.repeat(activity.id, other_user)
555 assert Enum.empty?(Notification.for_user(user))
558 test "replying to a deleted post without tagging does not generate a notification" do
560 other_user = insert(:user)
562 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
563 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
565 {:ok, _reply_activity} =
566 CommonAPI.post(other_user, %{
567 "status" => "test reply",
568 "in_reply_to_status_id" => activity.id
571 assert Enum.empty?(Notification.for_user(user))
574 test "notifications are deleted if a local user is deleted" do
576 other_user = insert(:user)
579 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
581 refute Enum.empty?(Notification.for_user(other_user))
583 {:ok, job} = User.delete(user)
584 ObanHelpers.perform(job)
586 assert Enum.empty?(Notification.for_user(other_user))
589 test "notifications are deleted if a remote user is deleted" do
590 remote_user = insert(:user)
591 local_user = insert(:user)
594 "@context" => "https://www.w3.org/ns/activitystreams",
596 "actor" => remote_user.ap_id,
597 "id" => remote_user.ap_id <> "/activities/test",
598 "to" => [local_user.ap_id],
602 "content" => "Hello!",
606 "href" => local_user.ap_id,
607 "name" => "@#{local_user.nickname}"
610 "to" => [local_user.ap_id],
612 "attributedTo" => remote_user.ap_id
616 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
618 refute Enum.empty?(Notification.for_user(local_user))
620 delete_user_message = %{
621 "@context" => "https://www.w3.org/ns/activitystreams",
622 "id" => remote_user.ap_id <> "/activities/delete",
623 "actor" => remote_user.ap_id,
625 "object" => remote_user.ap_id
628 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
629 ObanHelpers.perform_all()
631 assert Enum.empty?(Notification.for_user(local_user))
634 test "move activity generates a notification" do
635 %{ap_id: old_ap_id} = old_user = insert(:user)
636 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
637 follower = insert(:user)
638 other_follower = insert(:user, %{allow_following_move: false})
640 User.follow(follower, old_user)
641 User.follow(other_follower, old_user)
643 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
644 ObanHelpers.perform_all()
646 assert [] = Notification.for_user(follower)
651 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
654 ] = Notification.for_user(follower, %{with_move: true})
656 assert [] = Notification.for_user(other_follower)
661 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
664 ] = Notification.for_user(other_follower, %{with_move: true})
668 describe "for_user" do
669 test "it returns notifications for muted user without notifications" do
671 muted = insert(:user)
672 {:ok, user} = User.mute(user, muted, false)
674 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
676 assert length(Notification.for_user(user)) == 1
679 test "it doesn't return notifications for muted user with notifications" do
681 muted = insert(:user)
682 {:ok, user} = User.mute(user, muted)
684 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
686 assert Notification.for_user(user) == []
689 test "it doesn't return notifications for blocked user" do
691 blocked = insert(:user)
692 {:ok, user} = User.block(user, blocked)
694 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
696 assert Notification.for_user(user) == []
699 test "it doesn't return notificatitons for blocked domain" do
701 blocked = insert(:user, ap_id: "http://some-domain.com")
702 {:ok, user} = User.block_domain(user, "some-domain.com")
704 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
706 assert Notification.for_user(user) == []
709 test "it doesn't return notifications for muted thread" do
711 another_user = insert(:user)
713 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
715 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
716 assert Notification.for_user(user) == []
719 test "it returns notifications from a muted user when with_muted is set" do
721 muted = insert(:user)
722 {:ok, user} = User.mute(user, muted)
724 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
726 assert length(Notification.for_user(user, %{with_muted: true})) == 1
729 test "it doesn't return notifications from a blocked user when with_muted is set" do
731 blocked = insert(:user)
732 {:ok, user} = User.block(user, blocked)
734 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
736 assert length(Notification.for_user(user, %{with_muted: true})) == 0
739 test "it doesn't return notifications from a domain-blocked user when with_muted is set" 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 length(Notification.for_user(user, %{with_muted: true})) == 0
749 test "it returns notifications from muted threads when with_muted is set" 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 length(Notification.for_user(user, %{with_muted: true})) == 1