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 import ExUnit.CaptureLog
19 describe "create_notifications" do
20 test "notifies someone when they are directly addressed" do
22 other_user = insert(:user)
23 third_user = insert(:user)
26 CommonAPI.post(user, %{
27 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
30 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
32 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
33 assert notified_ids == [other_user.id, third_user.id]
34 assert notification.activity_id == activity.id
35 assert other_notification.activity_id == activity.id
38 test "it creates a notification for subscribed users" do
40 subscriber = insert(:user)
42 User.subscribe(subscriber, user)
44 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
45 {:ok, [notification]} = Notification.create_notifications(status)
47 assert notification.user_id == subscriber.id
50 test "does not create a notification for subscribed users if status is a reply" do
52 other_user = insert(:user)
53 subscriber = insert(:user)
55 User.subscribe(subscriber, other_user)
57 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
59 {:ok, _reply_activity} =
60 CommonAPI.post(other_user, %{
61 "status" => "test reply",
62 "in_reply_to_status_id" => activity.id
65 user_notifications = Notification.for_user(user)
66 assert length(user_notifications) == 1
68 subscriber_notifications = Notification.for_user(subscriber)
69 assert Enum.empty?(subscriber_notifications)
73 describe "create_notification" do
74 @tag needs_streamer: true
75 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
77 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
78 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
79 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
83 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
86 activity = insert(:note_activity)
88 notify = Notification.create_notification(activity, user)
89 assert notify.user_id == user.id
91 Task.await(task_user_notification)
94 test "it creates a notification for user if the user blocks the activity author" do
95 activity = insert(:note_activity)
96 author = User.get_cached_by_ap_id(activity.data["actor"])
98 {:ok, user} = User.block(user, author)
100 assert Notification.create_notification(activity, user)
103 test "it creates a notificatin for the user if the user mutes the activity author" do
104 muter = insert(:user)
105 muted = insert(:user)
106 {:ok, _} = User.mute(muter, muted)
107 muter = Repo.get(User, muter.id)
108 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
110 assert Notification.create_notification(activity, muter)
113 test "notification created if user is muted without notifications" do
114 muter = insert(:user)
115 muted = insert(:user)
117 {:ok, muter} = User.mute(muter, muted, false)
119 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
121 assert Notification.create_notification(activity, muter)
124 test "it creates a notification for an activity from a muted thread" do
125 muter = insert(:user)
126 other_user = insert(:user)
127 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
128 CommonAPI.add_mute(muter, activity)
131 CommonAPI.post(other_user, %{
132 "status" => "Hi @#{muter.nickname}",
133 "in_reply_to_status_id" => activity.id
136 assert Notification.create_notification(activity, muter)
139 test "it disables notifications from followers" do
140 follower = insert(:user)
141 followed = insert(:user, notification_settings: %{"followers" => false})
142 User.follow(follower, followed)
143 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
144 refute Notification.create_notification(activity, followed)
147 test "it disables notifications from non-followers" do
148 follower = insert(:user)
149 followed = insert(:user, notification_settings: %{"non_followers" => false})
150 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
151 refute Notification.create_notification(activity, followed)
154 test "it disables notifications from people the user follows" do
155 follower = insert(:user, notification_settings: %{"follows" => false})
156 followed = insert(:user)
157 User.follow(follower, followed)
158 follower = Repo.get(User, follower.id)
159 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
160 refute Notification.create_notification(activity, follower)
163 test "it disables notifications from people the user does not follow" do
164 follower = insert(:user, notification_settings: %{"non_follows" => false})
165 followed = insert(:user)
166 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
167 refute Notification.create_notification(activity, follower)
170 test "it doesn't create a notification for user if he is the activity author" do
171 activity = insert(:note_activity)
172 author = User.get_cached_by_ap_id(activity.data["actor"])
174 refute Notification.create_notification(activity, author)
177 test "it doesn't create a notification for follow-unfollow-follow chains" do
179 followed_user = insert(:user)
180 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
181 Notification.create_notification(activity, followed_user)
182 CommonAPI.unfollow(user, followed_user)
183 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
184 refute Notification.create_notification(activity_dupe, followed_user)
187 test "it doesn't create duplicate notifications for follow+subscribed users" do
189 subscriber = insert(:user)
191 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
192 User.subscribe(subscriber, user)
193 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
194 {:ok, [_notif]} = Notification.create_notifications(status)
197 test "it doesn't create subscription notifications if the recipient cannot see the status" do
199 subscriber = insert(:user)
201 User.subscribe(subscriber, user)
203 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
205 assert {:ok, []} == Notification.create_notifications(status)
209 describe "get notification" do
210 test "it gets a notification that belongs to the user" do
212 other_user = insert(:user)
214 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
216 {:ok, [notification]} = Notification.create_notifications(activity)
217 {:ok, notification} = Notification.get(other_user, notification.id)
219 assert notification.user_id == other_user.id
222 test "it returns error if the notification doesn't belong to the user" do
224 other_user = insert(:user)
226 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
228 {:ok, [notification]} = Notification.create_notifications(activity)
229 {:error, _notification} = Notification.get(user, notification.id)
233 describe "dismiss notification" do
234 test "it dismisses a notification that belongs to the user" do
236 other_user = insert(:user)
238 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
240 {:ok, [notification]} = Notification.create_notifications(activity)
241 {:ok, notification} = Notification.dismiss(other_user, notification.id)
243 assert notification.user_id == other_user.id
246 test "it returns error if the notification doesn't belong to the user" do
248 other_user = insert(:user)
250 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
252 {:ok, [notification]} = Notification.create_notifications(activity)
253 {:error, _notification} = Notification.dismiss(user, notification.id)
257 describe "clear notification" do
258 test "it clears all notifications belonging to the user" do
260 other_user = insert(:user)
261 third_user = insert(:user)
264 CommonAPI.post(user, %{
265 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
268 {:ok, _notifs} = Notification.create_notifications(activity)
271 CommonAPI.post(user, %{
272 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
275 {:ok, _notifs} = Notification.create_notifications(activity)
276 Notification.clear(other_user)
278 assert Notification.for_user(other_user) == []
279 assert Notification.for_user(third_user) != []
283 describe "set_read_up_to()" do
284 test "it sets all notifications as read up to a specified notification ID" do
286 other_user = insert(:user)
289 CommonAPI.post(user, %{
290 "status" => "hey @#{other_user.nickname}!"
294 CommonAPI.post(user, %{
295 "status" => "hey again @#{other_user.nickname}!"
298 [n2, n1] = notifs = Notification.for_user(other_user)
299 assert length(notifs) == 2
304 CommonAPI.post(user, %{
305 "status" => "hey yet again @#{other_user.nickname}!"
308 Notification.set_read_up_to(other_user, n2.id)
310 [n3, n2, n1] = Notification.for_user(other_user)
312 assert n1.seen == true
313 assert n2.seen == true
314 assert n3.seen == false
318 describe "for_user_since/2" do
319 defp days_ago(days) do
321 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
322 -days * 60 * 60 * 24,
327 test "Returns recent notifications" do
328 user1 = insert(:user)
329 user2 = insert(:user)
331 Enum.each(0..10, fn i ->
333 CommonAPI.post(user1, %{
334 "status" => "hey ##{i} @#{user2.nickname}!"
338 {old, new} = Enum.split(Notification.for_user(user2), 5)
340 Enum.each(old, fn notification ->
342 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
343 |> Pleroma.Repo.update!()
346 recent_notifications_ids =
348 |> Notification.for_user_since(
349 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
353 Enum.each(old, fn %{id: id} ->
354 refute id in recent_notifications_ids
357 Enum.each(new, fn %{id: id} ->
358 assert id in recent_notifications_ids
363 describe "notification target determination" do
364 test "it sends notifications to addressed users in new messages" do
366 other_user = insert(:user)
369 CommonAPI.post(user, %{
370 "status" => "hey @#{other_user.nickname}!"
373 assert other_user in Notification.get_notified_from_activity(activity)
376 test "it sends notifications to mentioned users in new messages" do
378 other_user = insert(:user)
381 "@context" => "https://www.w3.org/ns/activitystreams",
383 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
384 "actor" => user.ap_id,
387 "content" => "message with a Mention tag, but no explicit tagging",
391 "href" => other_user.ap_id,
392 "name" => other_user.nickname
395 "attributedTo" => user.ap_id
399 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
401 assert other_user in Notification.get_notified_from_activity(activity)
404 test "it does not send notifications to users who are only cc in new messages" do
406 other_user = insert(:user)
409 "@context" => "https://www.w3.org/ns/activitystreams",
411 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
412 "cc" => [other_user.ap_id],
413 "actor" => user.ap_id,
416 "content" => "hi everyone",
417 "attributedTo" => user.ap_id
421 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
423 assert other_user not in Notification.get_notified_from_activity(activity)
426 test "it does not send notification to mentioned users in likes" do
428 other_user = insert(:user)
429 third_user = insert(:user)
431 {:ok, activity_one} =
432 CommonAPI.post(user, %{
433 "status" => "hey @#{other_user.nickname}!"
436 {:ok, activity_two} = CommonAPI.favorite(third_user, activity_one.id)
438 assert other_user not in Notification.get_notified_from_activity(activity_two)
441 test "it does not send notification to mentioned users in announces" do
443 other_user = insert(:user)
444 third_user = insert(:user)
446 {:ok, activity_one} =
447 CommonAPI.post(user, %{
448 "status" => "hey @#{other_user.nickname}!"
451 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
453 assert other_user not in Notification.get_notified_from_activity(activity_two)
457 describe "notification lifecycle" do
458 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
460 other_user = insert(:user)
462 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
464 assert Enum.empty?(Notification.for_user(user))
466 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
468 assert length(Notification.for_user(user)) == 1
470 {:ok, _} = CommonAPI.delete(activity.id, user)
472 assert Enum.empty?(Notification.for_user(user))
475 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
477 other_user = insert(:user)
479 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
481 assert Enum.empty?(Notification.for_user(user))
483 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
485 assert length(Notification.for_user(user)) == 1
487 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
489 assert Enum.empty?(Notification.for_user(user))
492 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
494 other_user = insert(:user)
496 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
498 assert Enum.empty?(Notification.for_user(user))
500 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
502 assert length(Notification.for_user(user)) == 1
504 {:ok, _} = CommonAPI.delete(activity.id, user)
506 assert Enum.empty?(Notification.for_user(user))
509 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
511 other_user = insert(:user)
513 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
515 assert Enum.empty?(Notification.for_user(user))
517 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
519 assert length(Notification.for_user(user)) == 1
521 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
523 assert Enum.empty?(Notification.for_user(user))
526 test "liking an activity which is already deleted does not generate a notification" do
528 other_user = insert(:user)
530 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
532 assert Enum.empty?(Notification.for_user(user))
534 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
536 assert Enum.empty?(Notification.for_user(user))
538 assert capture_log(fn ->
539 {:error, _} = CommonAPI.favorite(other_user, activity.id)
542 assert Enum.empty?(Notification.for_user(user))
545 test "repeating an activity which is already deleted does not generate a notification" do
547 other_user = insert(:user)
549 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
551 assert Enum.empty?(Notification.for_user(user))
553 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
555 assert Enum.empty?(Notification.for_user(user))
557 {:error, _} = CommonAPI.repeat(activity.id, other_user)
559 assert Enum.empty?(Notification.for_user(user))
562 test "replying to a deleted post without tagging does not generate a notification" do
564 other_user = insert(:user)
566 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
567 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
569 {:ok, _reply_activity} =
570 CommonAPI.post(other_user, %{
571 "status" => "test reply",
572 "in_reply_to_status_id" => activity.id
575 assert Enum.empty?(Notification.for_user(user))
578 test "notifications are deleted if a local user is deleted" do
580 other_user = insert(:user)
583 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
585 refute Enum.empty?(Notification.for_user(other_user))
587 {:ok, job} = User.delete(user)
588 ObanHelpers.perform(job)
590 assert Enum.empty?(Notification.for_user(other_user))
593 test "notifications are deleted if a remote user is deleted" do
594 remote_user = insert(:user)
595 local_user = insert(:user)
598 "@context" => "https://www.w3.org/ns/activitystreams",
600 "actor" => remote_user.ap_id,
601 "id" => remote_user.ap_id <> "/activities/test",
602 "to" => [local_user.ap_id],
606 "content" => "Hello!",
610 "href" => local_user.ap_id,
611 "name" => "@#{local_user.nickname}"
614 "to" => [local_user.ap_id],
616 "attributedTo" => remote_user.ap_id
620 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
622 refute Enum.empty?(Notification.for_user(local_user))
624 delete_user_message = %{
625 "@context" => "https://www.w3.org/ns/activitystreams",
626 "id" => remote_user.ap_id <> "/activities/delete",
627 "actor" => remote_user.ap_id,
629 "object" => remote_user.ap_id
632 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
633 ObanHelpers.perform_all()
635 assert Enum.empty?(Notification.for_user(local_user))
638 test "move activity generates a notification" do
639 %{ap_id: old_ap_id} = old_user = insert(:user)
640 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
641 follower = insert(:user)
642 other_follower = insert(:user, %{allow_following_move: false})
644 User.follow(follower, old_user)
645 User.follow(other_follower, old_user)
647 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
648 ObanHelpers.perform_all()
653 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
656 ] = Notification.for_user(follower)
661 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
664 ] = Notification.for_user(other_follower)
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