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_relationship} = 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, _user_relationships} = 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)
141 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
143 User.follow(follower, followed)
144 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
145 refute Notification.create_notification(activity, followed)
148 test "it disables notifications from non-followers" do
149 follower = insert(:user)
153 notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
156 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
157 refute Notification.create_notification(activity, followed)
160 test "it disables notifications from people the user follows" do
162 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
164 followed = insert(:user)
165 User.follow(follower, followed)
166 follower = Repo.get(User, follower.id)
167 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
168 refute Notification.create_notification(activity, follower)
171 test "it disables notifications from people the user does not follow" do
173 insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
175 followed = insert(:user)
176 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
177 refute Notification.create_notification(activity, follower)
180 test "it doesn't create a notification for user if he is the activity author" do
181 activity = insert(:note_activity)
182 author = User.get_cached_by_ap_id(activity.data["actor"])
184 refute Notification.create_notification(activity, author)
187 test "it doesn't create a notification for follow-unfollow-follow chains" do
189 followed_user = insert(:user)
190 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
191 Notification.create_notification(activity, followed_user)
192 CommonAPI.unfollow(user, followed_user)
193 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
194 refute Notification.create_notification(activity_dupe, followed_user)
197 test "it doesn't create duplicate notifications for follow+subscribed users" do
199 subscriber = insert(:user)
201 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
202 User.subscribe(subscriber, user)
203 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
204 {:ok, [_notif]} = Notification.create_notifications(status)
207 test "it doesn't create subscription notifications if the recipient cannot see the status" do
209 subscriber = insert(:user)
211 User.subscribe(subscriber, user)
213 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
215 assert {:ok, []} == Notification.create_notifications(status)
219 describe "get notification" do
220 test "it gets a notification that belongs 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 {:ok, notification} = Notification.get(other_user, notification.id)
229 assert notification.user_id == other_user.id
232 test "it returns error if the notification doesn't belong 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 {:error, _notification} = Notification.get(user, notification.id)
243 describe "dismiss notification" do
244 test "it dismisses a notification that belongs 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 {:ok, notification} = Notification.dismiss(other_user, notification.id)
253 assert notification.user_id == other_user.id
256 test "it returns error if the notification doesn't belong 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 {:error, _notification} = Notification.dismiss(user, notification.id)
267 describe "clear notification" do
268 test "it clears all notifications belonging to the user" do
270 other_user = insert(:user)
271 third_user = insert(:user)
274 CommonAPI.post(user, %{
275 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
278 {:ok, _notifs} = Notification.create_notifications(activity)
281 CommonAPI.post(user, %{
282 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
285 {:ok, _notifs} = Notification.create_notifications(activity)
286 Notification.clear(other_user)
288 assert Notification.for_user(other_user) == []
289 assert Notification.for_user(third_user) != []
293 describe "set_read_up_to()" do
294 test "it sets all notifications as read up to a specified notification ID" do
296 other_user = insert(:user)
299 CommonAPI.post(user, %{
300 "status" => "hey @#{other_user.nickname}!"
304 CommonAPI.post(user, %{
305 "status" => "hey again @#{other_user.nickname}!"
308 [n2, n1] = notifs = Notification.for_user(other_user)
309 assert length(notifs) == 2
314 CommonAPI.post(user, %{
315 "status" => "hey yet again @#{other_user.nickname}!"
318 Notification.set_read_up_to(other_user, n2.id)
320 [n3, n2, n1] = Notification.for_user(other_user)
322 assert n1.seen == true
323 assert n2.seen == true
324 assert n3.seen == false
328 describe "for_user_since/2" do
329 defp days_ago(days) do
331 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
332 -days * 60 * 60 * 24,
337 test "Returns recent notifications" do
338 user1 = insert(:user)
339 user2 = insert(:user)
341 Enum.each(0..10, fn i ->
343 CommonAPI.post(user1, %{
344 "status" => "hey ##{i} @#{user2.nickname}!"
348 {old, new} = Enum.split(Notification.for_user(user2), 5)
350 Enum.each(old, fn notification ->
352 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
353 |> Pleroma.Repo.update!()
356 recent_notifications_ids =
358 |> Notification.for_user_since(
359 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
363 Enum.each(old, fn %{id: id} ->
364 refute id in recent_notifications_ids
367 Enum.each(new, fn %{id: id} ->
368 assert id in recent_notifications_ids
373 describe "notification target determination" do
374 test "it sends notifications to addressed users in new messages" do
376 other_user = insert(:user)
379 CommonAPI.post(user, %{
380 "status" => "hey @#{other_user.nickname}!"
383 assert other_user in Notification.get_notified_from_activity(activity)
386 test "it sends notifications to mentioned users in new messages" do
388 other_user = insert(:user)
391 "@context" => "https://www.w3.org/ns/activitystreams",
393 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
394 "actor" => user.ap_id,
397 "content" => "message with a Mention tag, but no explicit tagging",
401 "href" => other_user.ap_id,
402 "name" => other_user.nickname
405 "attributedTo" => user.ap_id
409 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
411 assert other_user in Notification.get_notified_from_activity(activity)
414 test "it does not send notifications to users who are only cc in new messages" do
416 other_user = insert(:user)
419 "@context" => "https://www.w3.org/ns/activitystreams",
421 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
422 "cc" => [other_user.ap_id],
423 "actor" => user.ap_id,
426 "content" => "hi everyone",
427 "attributedTo" => user.ap_id
431 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
433 assert other_user not in Notification.get_notified_from_activity(activity)
436 test "it does not send notification to mentioned users in likes" do
438 other_user = insert(:user)
439 third_user = insert(:user)
441 {:ok, activity_one} =
442 CommonAPI.post(user, %{
443 "status" => "hey @#{other_user.nickname}!"
446 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
448 assert other_user not in Notification.get_notified_from_activity(activity_two)
451 test "it does not send notification to mentioned users in announces" do
453 other_user = insert(:user)
454 third_user = insert(:user)
456 {:ok, activity_one} =
457 CommonAPI.post(user, %{
458 "status" => "hey @#{other_user.nickname}!"
461 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
463 assert other_user not in Notification.get_notified_from_activity(activity_two)
467 describe "notification lifecycle" do
468 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
470 other_user = insert(:user)
472 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
474 assert Enum.empty?(Notification.for_user(user))
476 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
478 assert length(Notification.for_user(user)) == 1
480 {:ok, _} = CommonAPI.delete(activity.id, user)
482 assert Enum.empty?(Notification.for_user(user))
485 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
487 other_user = insert(:user)
489 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
491 assert Enum.empty?(Notification.for_user(user))
493 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
495 assert length(Notification.for_user(user)) == 1
497 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
499 assert Enum.empty?(Notification.for_user(user))
502 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
504 other_user = insert(:user)
506 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
508 assert Enum.empty?(Notification.for_user(user))
510 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
512 assert length(Notification.for_user(user)) == 1
514 {:ok, _} = CommonAPI.delete(activity.id, user)
516 assert Enum.empty?(Notification.for_user(user))
519 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
521 other_user = insert(:user)
523 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
525 assert Enum.empty?(Notification.for_user(user))
527 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
529 assert length(Notification.for_user(user)) == 1
531 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
533 assert Enum.empty?(Notification.for_user(user))
536 test "liking an activity which is already deleted does not generate a notification" do
538 other_user = insert(:user)
540 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
542 assert Enum.empty?(Notification.for_user(user))
544 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
546 assert Enum.empty?(Notification.for_user(user))
548 {:error, _} = CommonAPI.favorite(activity.id, other_user)
550 assert Enum.empty?(Notification.for_user(user))
553 test "repeating an activity which is already deleted does not generate a notification" do
555 other_user = insert(:user)
557 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
559 assert Enum.empty?(Notification.for_user(user))
561 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
563 assert Enum.empty?(Notification.for_user(user))
565 {:error, _} = CommonAPI.repeat(activity.id, other_user)
567 assert Enum.empty?(Notification.for_user(user))
570 test "replying to a deleted post without tagging does not generate a notification" do
572 other_user = insert(:user)
574 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
575 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
577 {:ok, _reply_activity} =
578 CommonAPI.post(other_user, %{
579 "status" => "test reply",
580 "in_reply_to_status_id" => activity.id
583 assert Enum.empty?(Notification.for_user(user))
586 test "notifications are deleted if a local user is deleted" do
588 other_user = insert(:user)
591 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
593 refute Enum.empty?(Notification.for_user(other_user))
595 {:ok, job} = User.delete(user)
596 ObanHelpers.perform(job)
598 assert Enum.empty?(Notification.for_user(other_user))
601 test "notifications are deleted if a remote user is deleted" do
602 remote_user = insert(:user)
603 local_user = insert(:user)
606 "@context" => "https://www.w3.org/ns/activitystreams",
608 "actor" => remote_user.ap_id,
609 "id" => remote_user.ap_id <> "/activities/test",
610 "to" => [local_user.ap_id],
614 "content" => "Hello!",
618 "href" => local_user.ap_id,
619 "name" => "@#{local_user.nickname}"
622 "to" => [local_user.ap_id],
624 "attributedTo" => remote_user.ap_id
628 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
630 refute Enum.empty?(Notification.for_user(local_user))
632 delete_user_message = %{
633 "@context" => "https://www.w3.org/ns/activitystreams",
634 "id" => remote_user.ap_id <> "/activities/delete",
635 "actor" => remote_user.ap_id,
637 "object" => remote_user.ap_id
640 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
641 ObanHelpers.perform_all()
643 assert Enum.empty?(Notification.for_user(local_user))
646 test "move activity generates a notification" do
647 %{ap_id: old_ap_id} = old_user = insert(:user)
648 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
649 follower = insert(:user)
650 other_follower = insert(:user, %{allow_following_move: false})
652 User.follow(follower, old_user)
653 User.follow(other_follower, old_user)
655 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
656 ObanHelpers.perform_all()
658 assert [] = Notification.for_user(follower)
663 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
666 ] = Notification.for_user(follower, %{with_move: true})
668 assert [] = Notification.for_user(other_follower)
673 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
676 ] = Notification.for_user(other_follower, %{with_move: true})
680 describe "for_user" do
681 test "it returns notifications for muted user without notifications" do
683 muted = insert(:user)
684 {:ok, _user_relationships} = User.mute(user, muted, false)
686 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
688 assert length(Notification.for_user(user)) == 1
691 test "it doesn't return notifications for muted user with notifications" do
693 muted = insert(:user)
694 {:ok, _user_relationships} = User.mute(user, muted)
696 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
698 assert Notification.for_user(user) == []
701 test "it doesn't return notifications for blocked user" do
703 blocked = insert(:user)
704 {:ok, _user_relationship} = User.block(user, blocked)
706 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
708 assert Notification.for_user(user) == []
711 test "it doesn't return notificatitons for blocked domain" do
713 blocked = insert(:user, ap_id: "http://some-domain.com")
714 {:ok, user} = User.block_domain(user, "some-domain.com")
716 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
718 assert Notification.for_user(user) == []
721 test "it doesn't return notifications for muted thread" do
723 another_user = insert(:user)
725 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
727 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
728 assert Notification.for_user(user) == []
731 test "it returns notifications from a muted user when with_muted is set" do
733 muted = insert(:user)
734 {:ok, _user_relationships} = User.mute(user, muted)
736 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
738 assert length(Notification.for_user(user, %{with_muted: true})) == 1
741 test "it doesn't return notifications from a blocked user when with_muted is set" do
743 blocked = insert(:user)
744 {:ok, _user_relationship} = User.block(user, blocked)
746 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
748 assert length(Notification.for_user(user, %{with_muted: true})) == 0
751 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
753 blocked = insert(:user, ap_id: "http://some-domain.com")
754 {:ok, user} = User.block_domain(user, "some-domain.com")
756 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
758 assert length(Notification.for_user(user, %{with_muted: true})) == 0
761 test "it returns notifications from muted threads when with_muted is set" do
763 another_user = insert(:user)
765 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
767 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
768 assert length(Notification.for_user(user, %{with_muted: true})) == 1