1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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
16 alias Pleroma.Web.TwitterAPI.TwitterAPI
18 describe "create_notifications" do
19 test "notifies someone when they are directly addressed" do
21 other_user = insert(:user)
22 third_user = insert(:user)
25 TwitterAPI.create_status(user, %{
26 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
29 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
31 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
32 assert notified_ids == [other_user.id, third_user.id]
33 assert notification.activity_id == activity.id
34 assert other_notification.activity_id == activity.id
37 test "it creates a notification for subscribed users" do
39 subscriber = insert(:user)
41 User.subscribe(subscriber, user)
43 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
44 {:ok, [notification]} = Notification.create_notifications(status)
46 assert notification.user_id == subscriber.id
49 test "does not create a notification for subscribed users if status is a reply" do
51 other_user = insert(:user)
52 subscriber = insert(:user)
54 User.subscribe(subscriber, other_user)
56 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
58 {:ok, _reply_activity} =
59 CommonAPI.post(other_user, %{
60 "status" => "test reply",
61 "in_reply_to_status_id" => activity.id
64 user_notifications = Notification.for_user(user)
65 assert length(user_notifications) == 1
67 subscriber_notifications = Notification.for_user(subscriber)
68 assert Enum.empty?(subscriber_notifications)
72 describe "create_notification" do
74 GenServer.start(Streamer, %{}, name: Streamer)
77 if pid = Process.whereis(Streamer) do
78 Process.exit(pid, :kill)
83 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
85 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
86 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
87 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
91 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
94 activity = insert(:note_activity)
96 notify = Notification.create_notification(activity, user)
97 assert notify.user_id == user.id
99 Task.await(task_user_notification)
102 test "it creates a notification for user if the user blocks the activity author" do
103 activity = insert(:note_activity)
104 author = User.get_cached_by_ap_id(activity.data["actor"])
106 {:ok, user} = User.block(user, author)
108 assert Notification.create_notification(activity, user)
111 test "it creates a notificatin for the user if the user mutes the activity author" do
112 muter = insert(:user)
113 muted = insert(:user)
114 {:ok, _} = User.mute(muter, muted)
115 muter = Repo.get(User, muter.id)
116 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
118 assert Notification.create_notification(activity, muter)
121 test "notification created if user is muted without notifications" do
122 muter = insert(:user)
123 muted = insert(:user)
125 {:ok, muter} = User.mute(muter, muted, false)
127 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
129 assert Notification.create_notification(activity, muter)
132 test "it creates a notification for an activity from a muted thread" do
133 muter = insert(:user)
134 other_user = insert(:user)
135 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
136 CommonAPI.add_mute(muter, activity)
139 CommonAPI.post(other_user, %{
140 "status" => "Hi @#{muter.nickname}",
141 "in_reply_to_status_id" => activity.id
144 assert Notification.create_notification(activity, muter)
147 test "it disables notifications from followers" do
148 follower = insert(:user)
149 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
150 User.follow(follower, followed)
151 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
152 refute Notification.create_notification(activity, followed)
155 test "it disables notifications from non-followers" do
156 follower = insert(:user)
157 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
158 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
159 refute Notification.create_notification(activity, followed)
162 test "it disables notifications from people the user follows" do
163 follower = insert(:user, info: %{notification_settings: %{"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
172 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
173 followed = insert(:user)
174 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
175 refute Notification.create_notification(activity, follower)
178 test "it doesn't create a notification for user if he is the activity author" do
179 activity = insert(:note_activity)
180 author = User.get_cached_by_ap_id(activity.data["actor"])
182 refute Notification.create_notification(activity, author)
185 test "it doesn't create a notification for follow-unfollow-follow chains" do
187 followed_user = insert(:user)
188 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
189 Notification.create_notification(activity, followed_user)
190 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
191 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
192 refute Notification.create_notification(activity_dupe, followed_user)
195 test "it doesn't create a notification for like-unlike-like chains" do
197 liked_user = insert(:user)
198 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
199 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
200 Notification.create_notification(fav_status, liked_user)
201 TwitterAPI.unfav(user, status.id)
202 {:ok, dupe} = TwitterAPI.fav(user, status.id)
203 refute Notification.create_notification(dupe, liked_user)
206 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
208 retweeted_user = insert(:user)
211 TwitterAPI.create_status(retweeted_user, %{
212 "status" => "Send dupe notifications to the shadow realm"
215 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
216 Notification.create_notification(retweeted_activity, retweeted_user)
217 TwitterAPI.unrepeat(user, status.id)
218 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
219 refute Notification.create_notification(dupe, retweeted_user)
222 test "it doesn't create duplicate notifications for follow+subscribed users" do
224 subscriber = insert(:user)
226 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
227 User.subscribe(subscriber, user)
228 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
229 {:ok, [_notif]} = Notification.create_notifications(status)
232 test "it doesn't create subscription notifications if the recipient cannot see the status" do
234 subscriber = insert(:user)
236 User.subscribe(subscriber, user)
239 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
241 assert {:ok, []} == Notification.create_notifications(status)
245 describe "get notification" do
246 test "it gets a notification that belongs to the user" do
248 other_user = insert(:user)
251 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
253 {:ok, [notification]} = Notification.create_notifications(activity)
254 {:ok, notification} = Notification.get(other_user, notification.id)
256 assert notification.user_id == other_user.id
259 test "it returns error if the notification doesn't belong to the user" do
261 other_user = insert(:user)
264 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
266 {:ok, [notification]} = Notification.create_notifications(activity)
267 {:error, _notification} = Notification.get(user, notification.id)
271 describe "dismiss notification" do
272 test "it dismisses a notification that belongs to the user" do
274 other_user = insert(:user)
277 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
279 {:ok, [notification]} = Notification.create_notifications(activity)
280 {:ok, notification} = Notification.dismiss(other_user, notification.id)
282 assert notification.user_id == other_user.id
285 test "it returns error if the notification doesn't belong to the user" do
287 other_user = insert(:user)
290 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
292 {:ok, [notification]} = Notification.create_notifications(activity)
293 {:error, _notification} = Notification.dismiss(user, notification.id)
297 describe "clear notification" do
298 test "it clears all notifications belonging to the user" do
300 other_user = insert(:user)
301 third_user = insert(:user)
304 TwitterAPI.create_status(user, %{
305 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
308 {:ok, _notifs} = Notification.create_notifications(activity)
311 TwitterAPI.create_status(user, %{
312 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
315 {:ok, _notifs} = Notification.create_notifications(activity)
316 Notification.clear(other_user)
318 assert Notification.for_user(other_user) == []
319 assert Notification.for_user(third_user) != []
323 describe "set_read_up_to()" do
324 test "it sets all notifications as read up to a specified notification ID" do
326 other_user = insert(:user)
329 TwitterAPI.create_status(user, %{
330 "status" => "hey @#{other_user.nickname}!"
334 TwitterAPI.create_status(user, %{
335 "status" => "hey again @#{other_user.nickname}!"
338 [n2, n1] = notifs = Notification.for_user(other_user)
339 assert length(notifs) == 2
344 TwitterAPI.create_status(user, %{
345 "status" => "hey yet again @#{other_user.nickname}!"
348 Notification.set_read_up_to(other_user, n2.id)
350 [n3, n2, n1] = Notification.for_user(other_user)
352 assert n1.seen == true
353 assert n2.seen == true
354 assert n3.seen == false
358 describe "for_user_since/2" do
359 defp days_ago(days) do
361 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
362 -days * 60 * 60 * 24,
367 test "Returns recent notifications" do
368 user1 = insert(:user)
369 user2 = insert(:user)
371 Enum.each(0..10, fn i ->
373 CommonAPI.post(user1, %{
374 "status" => "hey ##{i} @#{user2.nickname}!"
378 {old, new} = Enum.split(Notification.for_user(user2), 5)
380 Enum.each(old, fn notification ->
382 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
383 |> Pleroma.Repo.update!()
386 recent_notifications_ids =
388 |> Notification.for_user_since(
389 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
393 Enum.each(old, fn %{id: id} ->
394 refute id in recent_notifications_ids
397 Enum.each(new, fn %{id: id} ->
398 assert id in recent_notifications_ids
403 describe "notification target determination" do
404 test "it sends notifications to addressed users in new messages" do
406 other_user = insert(:user)
409 CommonAPI.post(user, %{
410 "status" => "hey @#{other_user.nickname}!"
413 assert other_user in Notification.get_notified_from_activity(activity)
416 test "it sends notifications to mentioned users in new messages" do
418 other_user = insert(:user)
421 "@context" => "https://www.w3.org/ns/activitystreams",
423 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
424 "actor" => user.ap_id,
427 "content" => "message with a Mention tag, but no explicit tagging",
431 "href" => other_user.ap_id,
432 "name" => other_user.nickname
435 "attributedTo" => user.ap_id
439 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
441 assert other_user in Notification.get_notified_from_activity(activity)
444 test "it does not send notifications to users who are only cc in new messages" do
446 other_user = insert(:user)
449 "@context" => "https://www.w3.org/ns/activitystreams",
451 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
452 "cc" => [other_user.ap_id],
453 "actor" => user.ap_id,
456 "content" => "hi everyone",
457 "attributedTo" => user.ap_id
461 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
463 assert other_user not in Notification.get_notified_from_activity(activity)
466 test "it does not send notification to mentioned users in likes" do
468 other_user = insert(:user)
469 third_user = insert(:user)
471 {:ok, activity_one} =
472 CommonAPI.post(user, %{
473 "status" => "hey @#{other_user.nickname}!"
476 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
478 assert other_user not in Notification.get_notified_from_activity(activity_two)
481 test "it does not send notification to mentioned users in announces" do
483 other_user = insert(:user)
484 third_user = insert(:user)
486 {:ok, activity_one} =
487 CommonAPI.post(user, %{
488 "status" => "hey @#{other_user.nickname}!"
491 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
493 assert other_user not in Notification.get_notified_from_activity(activity_two)
497 describe "notification lifecycle" do
498 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
500 other_user = insert(:user)
502 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
504 assert Enum.empty?(Notification.for_user(user))
506 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
508 assert length(Notification.for_user(user)) == 1
510 {:ok, _} = CommonAPI.delete(activity.id, user)
512 assert Enum.empty?(Notification.for_user(user))
515 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
517 other_user = insert(:user)
519 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
521 assert Enum.empty?(Notification.for_user(user))
523 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
525 assert length(Notification.for_user(user)) == 1
527 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
529 assert Enum.empty?(Notification.for_user(user))
532 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
534 other_user = insert(:user)
536 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
538 assert Enum.empty?(Notification.for_user(user))
540 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
542 assert length(Notification.for_user(user)) == 1
544 {:ok, _} = CommonAPI.delete(activity.id, user)
546 assert Enum.empty?(Notification.for_user(user))
549 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
551 other_user = insert(:user)
553 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
555 assert Enum.empty?(Notification.for_user(user))
557 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
559 assert length(Notification.for_user(user)) == 1
561 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
563 assert Enum.empty?(Notification.for_user(user))
566 test "liking an activity which is already deleted does not generate a notification" do
568 other_user = insert(:user)
570 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
572 assert Enum.empty?(Notification.for_user(user))
574 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
576 assert Enum.empty?(Notification.for_user(user))
578 {:error, _} = CommonAPI.favorite(activity.id, other_user)
580 assert Enum.empty?(Notification.for_user(user))
583 test "repeating an activity which is already deleted does not generate a notification" do
585 other_user = insert(:user)
587 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
589 assert Enum.empty?(Notification.for_user(user))
591 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
593 assert Enum.empty?(Notification.for_user(user))
595 {:error, _} = CommonAPI.repeat(activity.id, other_user)
597 assert Enum.empty?(Notification.for_user(user))
600 test "replying to a deleted post without tagging does not generate a notification" do
602 other_user = insert(:user)
604 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
605 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
607 {:ok, _reply_activity} =
608 CommonAPI.post(other_user, %{
609 "status" => "test reply",
610 "in_reply_to_status_id" => activity.id
613 assert Enum.empty?(Notification.for_user(user))
616 test "notifications are deleted if a local user is deleted" do
618 other_user = insert(:user)
621 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
623 refute Enum.empty?(Notification.for_user(other_user))
625 {:ok, job} = User.delete(user)
626 ObanHelpers.perform(job)
628 assert Enum.empty?(Notification.for_user(other_user))
631 test "notifications are deleted if a remote user is deleted" do
632 remote_user = insert(:user)
633 local_user = insert(:user)
636 "@context" => "https://www.w3.org/ns/activitystreams",
638 "actor" => remote_user.ap_id,
639 "id" => remote_user.ap_id <> "/activities/test",
640 "to" => [local_user.ap_id],
644 "content" => "Hello!",
648 "href" => local_user.ap_id,
649 "name" => "@#{local_user.nickname}"
652 "to" => [local_user.ap_id],
654 "attributedTo" => remote_user.ap_id
658 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
660 refute Enum.empty?(Notification.for_user(local_user))
662 delete_user_message = %{
663 "@context" => "https://www.w3.org/ns/activitystreams",
664 "id" => remote_user.ap_id <> "/activities/delete",
665 "actor" => remote_user.ap_id,
667 "object" => remote_user.ap_id
670 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
671 ObanHelpers.perform_all()
673 assert Enum.empty?(Notification.for_user(local_user))
677 describe "for_user" do
678 test "it returns notifications for muted user without notifications" do
680 muted = insert(:user)
681 {:ok, user} = User.mute(user, muted, false)
683 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
685 assert length(Notification.for_user(user)) == 1
688 test "it doesn't return notifications for muted user with notifications" do
690 muted = insert(:user)
691 {:ok, user} = User.mute(user, muted)
693 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
695 assert Notification.for_user(user) == []
698 test "it doesn't return notifications for blocked user" do
700 blocked = insert(:user)
701 {:ok, user} = User.block(user, blocked)
703 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
705 assert Notification.for_user(user) == []
708 test "it doesn't return notificatitons for blocked domain" do
710 blocked = insert(:user, ap_id: "http://some-domain.com")
711 {:ok, user} = User.block_domain(user, "some-domain.com")
713 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
715 assert Notification.for_user(user) == []
718 test "it doesn't return notifications for muted thread" do
720 another_user = insert(:user)
723 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
725 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
726 assert Notification.for_user(user) == []
729 test "it returns notifications for muted user with notifications and with_muted parameter" do
731 muted = insert(:user)
732 {:ok, user} = User.mute(user, muted)
734 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
736 assert length(Notification.for_user(user, %{with_muted: true})) == 1
739 test "it returns notifications for blocked user and with_muted parameter" do
741 blocked = insert(:user)
742 {:ok, user} = User.block(user, blocked)
744 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
746 assert length(Notification.for_user(user, %{with_muted: true})) == 1
749 test "it returns notificatitons for blocked domain and with_muted parameter" do
751 blocked = insert(:user, ap_id: "http://some-domain.com")
752 {:ok, user} = User.block_domain(user, "some-domain.com")
754 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
756 assert length(Notification.for_user(user, %{with_muted: true})) == 1
759 test "it returns notifications for muted thread with_muted parameter" do
761 another_user = insert(:user)
764 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
766 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
767 assert length(Notification.for_user(user, %{with_muted: true})) == 1