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
35 assert [%Pleroma.Marker{unread_count: 2}] =
36 Pleroma.Marker.get_markers(other_user, ["notifications"])
39 test "it creates a notification for subscribed users" do
41 subscriber = insert(:user)
43 User.subscribe(subscriber, user)
45 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
46 {:ok, [notification]} = Notification.create_notifications(status)
48 assert notification.user_id == subscriber.id
51 test "does not create a notification for subscribed users if status is a reply" do
53 other_user = insert(:user)
54 subscriber = insert(:user)
56 User.subscribe(subscriber, other_user)
58 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
60 {:ok, _reply_activity} =
61 CommonAPI.post(other_user, %{
62 "status" => "test reply",
63 "in_reply_to_status_id" => activity.id
66 user_notifications = Notification.for_user(user)
67 assert length(user_notifications) == 1
69 subscriber_notifications = Notification.for_user(subscriber)
70 assert Enum.empty?(subscriber_notifications)
74 describe "create_notification" do
75 @tag needs_streamer: true
76 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
78 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
79 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
80 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
84 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
87 activity = insert(:note_activity)
89 notify = Notification.create_notification(activity, user)
90 assert notify.user_id == user.id
92 Task.await(task_user_notification)
95 test "it creates a notification for user if the user blocks the activity author" do
96 activity = insert(:note_activity)
97 author = User.get_cached_by_ap_id(activity.data["actor"])
99 {:ok, user} = User.block(user, author)
101 assert Notification.create_notification(activity, user)
104 test "it creates a notificatin for the user if the user mutes the activity author" do
105 muter = insert(:user)
106 muted = insert(:user)
107 {:ok, _} = User.mute(muter, muted)
108 muter = Repo.get(User, muter.id)
109 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
111 assert Notification.create_notification(activity, muter)
114 test "notification created if user is muted without notifications" do
115 muter = insert(:user)
116 muted = insert(:user)
118 {:ok, muter} = User.mute(muter, muted, false)
120 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
122 assert Notification.create_notification(activity, muter)
125 test "it creates a notification for an activity from a muted thread" do
126 muter = insert(:user)
127 other_user = insert(:user)
128 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
129 CommonAPI.add_mute(muter, activity)
132 CommonAPI.post(other_user, %{
133 "status" => "Hi @#{muter.nickname}",
134 "in_reply_to_status_id" => activity.id
137 assert Notification.create_notification(activity, muter)
140 test "it disables notifications from followers" do
141 follower = insert(:user)
142 followed = insert(:user, notification_settings: %{"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)
150 followed = insert(:user, notification_settings: %{"non_followers" => false})
151 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
152 refute Notification.create_notification(activity, followed)
155 test "it disables notifications from people the user follows" do
156 follower = insert(:user, notification_settings: %{"follows" => false})
157 followed = insert(:user)
158 User.follow(follower, followed)
159 follower = Repo.get(User, follower.id)
160 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
161 refute Notification.create_notification(activity, follower)
164 test "it disables notifications from people the user does not follow" do
165 follower = insert(:user, notification_settings: %{"non_follows" => false})
166 followed = insert(:user)
167 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
168 refute Notification.create_notification(activity, follower)
171 test "it doesn't create a notification for user if he is the activity author" do
172 activity = insert(:note_activity)
173 author = User.get_cached_by_ap_id(activity.data["actor"])
175 refute Notification.create_notification(activity, author)
178 test "it doesn't create a notification for follow-unfollow-follow chains" do
180 followed_user = insert(:user)
181 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
182 Notification.create_notification(activity, followed_user)
183 CommonAPI.unfollow(user, followed_user)
184 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
185 refute Notification.create_notification(activity_dupe, followed_user)
188 test "it doesn't create duplicate notifications for follow+subscribed users" do
190 subscriber = insert(:user)
192 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
193 User.subscribe(subscriber, user)
194 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
195 {:ok, [_notif]} = Notification.create_notifications(status)
198 test "it doesn't create subscription notifications if the recipient cannot see the status" do
200 subscriber = insert(:user)
202 User.subscribe(subscriber, user)
204 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
206 assert {:ok, []} == Notification.create_notifications(status)
210 describe "get notification" do
211 test "it gets a notification that belongs to the user" do
213 other_user = insert(:user)
215 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
217 {:ok, [notification]} = Notification.create_notifications(activity)
218 {:ok, notification} = Notification.get(other_user, notification.id)
220 assert notification.user_id == other_user.id
223 test "it returns error if the notification doesn't belong to the user" do
225 other_user = insert(:user)
227 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
229 {:ok, [notification]} = Notification.create_notifications(activity)
230 {:error, _notification} = Notification.get(user, notification.id)
234 describe "dismiss notification" do
235 test "it dismisses 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.dismiss(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.dismiss(user, notification.id)
258 describe "clear notification" do
259 test "it clears all notifications belonging to the user" do
261 other_user = insert(:user)
262 third_user = insert(:user)
265 CommonAPI.post(user, %{
266 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
269 {:ok, _notifs} = Notification.create_notifications(activity)
272 CommonAPI.post(user, %{
273 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
276 {:ok, _notifs} = Notification.create_notifications(activity)
277 Notification.clear(other_user)
279 assert Notification.for_user(other_user) == []
280 assert Notification.for_user(third_user) != []
284 describe "set_read_up_to()" do
285 test "it sets all notifications as read up to a specified notification ID" do
287 other_user = insert(:user)
290 CommonAPI.post(user, %{
291 "status" => "hey @#{other_user.nickname}!"
295 CommonAPI.post(user, %{
296 "status" => "hey again @#{other_user.nickname}!"
299 [n2, n1] = notifs = Notification.for_user(other_user)
300 assert length(notifs) == 2
305 CommonAPI.post(user, %{
306 "status" => "hey yet again @#{other_user.nickname}!"
309 Notification.set_read_up_to(other_user, n2.id)
311 [n3, n2, n1] = Notification.for_user(other_user)
313 assert n1.seen == true
314 assert n2.seen == true
315 assert n3.seen == false
317 assert %Pleroma.Marker{unread_count: 1} =
320 user_id: other_user.id,
321 timeline: "notifications"
326 describe "for_user_since/2" do
327 defp days_ago(days) do
329 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
330 -days * 60 * 60 * 24,
335 test "Returns recent notifications" do
336 user1 = insert(:user)
337 user2 = insert(:user)
339 Enum.each(0..10, fn i ->
341 CommonAPI.post(user1, %{
342 "status" => "hey ##{i} @#{user2.nickname}!"
346 {old, new} = Enum.split(Notification.for_user(user2), 5)
348 Enum.each(old, fn notification ->
350 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
351 |> Pleroma.Repo.update!()
354 recent_notifications_ids =
356 |> Notification.for_user_since(
357 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
361 Enum.each(old, fn %{id: id} ->
362 refute id in recent_notifications_ids
365 Enum.each(new, fn %{id: id} ->
366 assert id in recent_notifications_ids
371 describe "notification target determination" do
372 test "it sends notifications to addressed users in new messages" do
374 other_user = insert(:user)
377 CommonAPI.post(user, %{
378 "status" => "hey @#{other_user.nickname}!"
381 assert other_user in Notification.get_notified_from_activity(activity)
384 test "it sends notifications to mentioned users in new messages" do
386 other_user = insert(:user)
389 "@context" => "https://www.w3.org/ns/activitystreams",
391 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
392 "actor" => user.ap_id,
395 "content" => "message with a Mention tag, but no explicit tagging",
399 "href" => other_user.ap_id,
400 "name" => other_user.nickname
403 "attributedTo" => user.ap_id
407 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
409 assert other_user in Notification.get_notified_from_activity(activity)
412 test "it does not send notifications to users who are only cc in new messages" do
414 other_user = insert(:user)
417 "@context" => "https://www.w3.org/ns/activitystreams",
419 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
420 "cc" => [other_user.ap_id],
421 "actor" => user.ap_id,
424 "content" => "hi everyone",
425 "attributedTo" => user.ap_id
429 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
431 assert other_user not in Notification.get_notified_from_activity(activity)
434 test "it does not send notification to mentioned users in likes" do
436 other_user = insert(:user)
437 third_user = insert(:user)
439 {:ok, activity_one} =
440 CommonAPI.post(user, %{
441 "status" => "hey @#{other_user.nickname}!"
444 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
446 assert other_user not in Notification.get_notified_from_activity(activity_two)
449 test "it does not send notification to mentioned users in announces" do
451 other_user = insert(:user)
452 third_user = insert(:user)
454 {:ok, activity_one} =
455 CommonAPI.post(user, %{
456 "status" => "hey @#{other_user.nickname}!"
459 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
461 assert other_user not in Notification.get_notified_from_activity(activity_two)
465 describe "notification lifecycle" do
466 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
468 other_user = insert(:user)
470 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
472 assert Enum.empty?(Notification.for_user(user))
474 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
476 assert length(Notification.for_user(user)) == 1
478 {:ok, _} = CommonAPI.delete(activity.id, user)
480 assert Enum.empty?(Notification.for_user(user))
483 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
485 other_user = insert(:user)
487 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
489 assert Enum.empty?(Notification.for_user(user))
491 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
493 assert length(Notification.for_user(user)) == 1
495 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
497 assert Enum.empty?(Notification.for_user(user))
500 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
502 other_user = insert(:user)
504 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
506 assert Enum.empty?(Notification.for_user(user))
508 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
510 assert length(Notification.for_user(user)) == 1
512 {:ok, _} = CommonAPI.delete(activity.id, user)
514 assert Enum.empty?(Notification.for_user(user))
517 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
519 other_user = insert(:user)
521 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
523 assert Enum.empty?(Notification.for_user(user))
525 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
527 assert length(Notification.for_user(user)) == 1
529 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
531 assert Enum.empty?(Notification.for_user(user))
534 test "liking an activity which is already deleted does not generate a notification" do
536 other_user = insert(:user)
538 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
540 assert Enum.empty?(Notification.for_user(user))
542 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
544 assert Enum.empty?(Notification.for_user(user))
546 {:error, _} = CommonAPI.favorite(activity.id, other_user)
548 assert Enum.empty?(Notification.for_user(user))
551 test "repeating an activity which is already deleted does not generate a notification" do
553 other_user = insert(:user)
555 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
557 assert Enum.empty?(Notification.for_user(user))
559 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
561 assert Enum.empty?(Notification.for_user(user))
563 {:error, _} = CommonAPI.repeat(activity.id, other_user)
565 assert Enum.empty?(Notification.for_user(user))
568 test "replying to a deleted post without tagging does not generate a notification" do
570 other_user = insert(:user)
572 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
573 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
575 {:ok, _reply_activity} =
576 CommonAPI.post(other_user, %{
577 "status" => "test reply",
578 "in_reply_to_status_id" => activity.id
581 assert Enum.empty?(Notification.for_user(user))
584 test "notifications are deleted if a local user is deleted" do
586 other_user = insert(:user)
589 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
591 refute Enum.empty?(Notification.for_user(other_user))
593 {:ok, job} = User.delete(user)
594 ObanHelpers.perform(job)
596 assert Enum.empty?(Notification.for_user(other_user))
599 test "notifications are deleted if a remote user is deleted" do
600 remote_user = insert(:user)
601 local_user = insert(:user)
604 "@context" => "https://www.w3.org/ns/activitystreams",
606 "actor" => remote_user.ap_id,
607 "id" => remote_user.ap_id <> "/activities/test",
608 "to" => [local_user.ap_id],
612 "content" => "Hello!",
616 "href" => local_user.ap_id,
617 "name" => "@#{local_user.nickname}"
620 "to" => [local_user.ap_id],
622 "attributedTo" => remote_user.ap_id
626 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
628 refute Enum.empty?(Notification.for_user(local_user))
630 delete_user_message = %{
631 "@context" => "https://www.w3.org/ns/activitystreams",
632 "id" => remote_user.ap_id <> "/activities/delete",
633 "actor" => remote_user.ap_id,
635 "object" => remote_user.ap_id
638 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
639 ObanHelpers.perform_all()
641 assert Enum.empty?(Notification.for_user(local_user))
644 test "move activity generates a notification" do
645 %{ap_id: old_ap_id} = old_user = insert(:user)
646 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
647 follower = insert(:user)
648 other_follower = insert(:user, %{allow_following_move: false})
650 User.follow(follower, old_user)
651 User.follow(other_follower, old_user)
653 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
654 ObanHelpers.perform_all()
659 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
662 ] = Notification.for_user(follower)
667 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
670 ] = Notification.for_user(other_follower)
674 describe "for_user" do
675 test "it returns notifications for muted user without notifications" do
677 muted = insert(:user)
678 {:ok, user} = User.mute(user, muted, false)
680 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
682 assert length(Notification.for_user(user)) == 1
685 test "it doesn't return notifications for muted user with notifications" do
687 muted = insert(:user)
688 {:ok, user} = User.mute(user, muted)
690 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
692 assert Notification.for_user(user) == []
695 test "it doesn't return notifications for blocked user" do
697 blocked = insert(:user)
698 {:ok, user} = User.block(user, blocked)
700 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
702 assert Notification.for_user(user) == []
705 test "it doesn't return notificatitons for blocked domain" do
707 blocked = insert(:user, ap_id: "http://some-domain.com")
708 {:ok, user} = User.block_domain(user, "some-domain.com")
710 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
712 assert Notification.for_user(user) == []
715 test "it doesn't return notifications for muted thread" do
717 another_user = insert(:user)
719 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
721 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
722 assert Notification.for_user(user) == []
725 test "it returns notifications from a muted user when with_muted is set" do
727 muted = insert(:user)
728 {:ok, user} = User.mute(user, muted)
730 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
732 assert length(Notification.for_user(user, %{with_muted: true})) == 1
735 test "it doesn't return notifications from a blocked user when with_muted is set" do
737 blocked = insert(:user)
738 {:ok, user} = User.block(user, blocked)
740 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
742 assert length(Notification.for_user(user, %{with_muted: true})) == 0
745 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
747 blocked = insert(:user, ap_id: "http://some-domain.com")
748 {:ok, user} = User.block_domain(user, "some-domain.com")
750 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
752 assert length(Notification.for_user(user, %{with_muted: true})) == 0
755 test "it returns notifications from muted threads when with_muted is set" do
757 another_user = insert(:user)
759 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
761 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
762 assert length(Notification.for_user(user, %{with_muted: true})) == 1