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
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Streamer
15 alias Pleroma.Web.TwitterAPI.TwitterAPI
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 TwitterAPI.create_status(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} = TwitterAPI.create_status(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
73 GenServer.start(Streamer, %{}, name: Streamer)
76 if pid = Process.whereis(Streamer) do
77 Process.exit(pid, :kill)
82 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
84 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
85 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
86 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
90 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
93 activity = insert(:note_activity)
95 notify = Notification.create_notification(activity, user)
96 assert notify.user_id == user.id
98 Task.await(task_user_notification)
101 test "it creates a notification for user if the user blocks the activity author" do
102 activity = insert(:note_activity)
103 author = User.get_cached_by_ap_id(activity.data["actor"])
105 {:ok, user} = User.block(user, author)
107 assert Notification.create_notification(activity, user)
110 test "it creates a notificatin for the user if the user mutes the activity author" do
111 muter = insert(:user)
112 muted = insert(:user)
113 {:ok, _} = User.mute(muter, muted)
114 muter = Repo.get(User, muter.id)
115 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
117 assert Notification.create_notification(activity, muter)
120 test "notification created if user is muted without notifications" do
121 muter = insert(:user)
122 muted = insert(:user)
124 {:ok, muter} = User.mute(muter, muted, false)
126 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
128 assert Notification.create_notification(activity, muter)
131 test "it creates a notification for an activity from a muted thread" do
132 muter = insert(:user)
133 other_user = insert(:user)
134 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
135 CommonAPI.add_mute(muter, activity)
138 CommonAPI.post(other_user, %{
139 "status" => "Hi @#{muter.nickname}",
140 "in_reply_to_status_id" => activity.id
143 assert Notification.create_notification(activity, muter)
146 test "it disables notifications from followers" do
147 follower = insert(:user)
148 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
149 User.follow(follower, followed)
150 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
151 refute Notification.create_notification(activity, followed)
154 test "it disables notifications from non-followers" do
155 follower = insert(:user)
156 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
157 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
158 refute Notification.create_notification(activity, followed)
161 test "it disables notifications from people the user follows" do
162 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
163 followed = insert(:user)
164 User.follow(follower, followed)
165 follower = Repo.get(User, follower.id)
166 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
167 refute Notification.create_notification(activity, follower)
170 test "it disables notifications from people the user does not follow" do
171 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
172 followed = insert(:user)
173 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
174 refute Notification.create_notification(activity, follower)
177 test "it doesn't create a notification for user if he is the activity author" do
178 activity = insert(:note_activity)
179 author = User.get_cached_by_ap_id(activity.data["actor"])
181 refute Notification.create_notification(activity, author)
184 test "it doesn't create a notification for follow-unfollow-follow chains" do
186 followed_user = insert(:user)
187 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
188 Notification.create_notification(activity, followed_user)
189 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
190 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
191 refute Notification.create_notification(activity_dupe, followed_user)
194 test "it doesn't create a notification for like-unlike-like chains" do
196 liked_user = insert(:user)
197 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
198 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
199 Notification.create_notification(fav_status, liked_user)
200 TwitterAPI.unfav(user, status.id)
201 {:ok, dupe} = TwitterAPI.fav(user, status.id)
202 refute Notification.create_notification(dupe, liked_user)
205 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
207 retweeted_user = insert(:user)
210 TwitterAPI.create_status(retweeted_user, %{
211 "status" => "Send dupe notifications to the shadow realm"
214 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
215 Notification.create_notification(retweeted_activity, retweeted_user)
216 TwitterAPI.unrepeat(user, status.id)
217 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
218 refute Notification.create_notification(dupe, retweeted_user)
221 test "it doesn't create duplicate notifications for follow+subscribed users" do
223 subscriber = insert(:user)
225 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
226 User.subscribe(subscriber, user)
227 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
228 {:ok, [_notif]} = Notification.create_notifications(status)
231 test "it doesn't create subscription notifications if the recipient cannot see the status" do
233 subscriber = insert(:user)
235 User.subscribe(subscriber, user)
238 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
240 assert {:ok, []} == Notification.create_notifications(status)
244 describe "get notification" do
245 test "it gets a notification that belongs to the user" do
247 other_user = insert(:user)
250 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
252 {:ok, [notification]} = Notification.create_notifications(activity)
253 {:ok, notification} = Notification.get(other_user, notification.id)
255 assert notification.user_id == other_user.id
258 test "it returns error if the notification doesn't belong to the user" do
260 other_user = insert(:user)
263 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
265 {:ok, [notification]} = Notification.create_notifications(activity)
266 {:error, _notification} = Notification.get(user, notification.id)
270 describe "dismiss notification" do
271 test "it dismisses a notification that belongs to the user" do
273 other_user = insert(:user)
276 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
278 {:ok, [notification]} = Notification.create_notifications(activity)
279 {:ok, notification} = Notification.dismiss(other_user, notification.id)
281 assert notification.user_id == other_user.id
284 test "it returns error if the notification doesn't belong to the user" do
286 other_user = insert(:user)
289 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
291 {:ok, [notification]} = Notification.create_notifications(activity)
292 {:error, _notification} = Notification.dismiss(user, notification.id)
296 describe "clear notification" do
297 test "it clears all notifications belonging to the user" do
299 other_user = insert(:user)
300 third_user = insert(:user)
303 TwitterAPI.create_status(user, %{
304 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
307 {:ok, _notifs} = Notification.create_notifications(activity)
310 TwitterAPI.create_status(user, %{
311 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
314 {:ok, _notifs} = Notification.create_notifications(activity)
315 Notification.clear(other_user)
317 assert Notification.for_user(other_user) == []
318 assert Notification.for_user(third_user) != []
322 describe "set_read_up_to()" do
323 test "it sets all notifications as read up to a specified notification ID" do
325 other_user = insert(:user)
328 TwitterAPI.create_status(user, %{
329 "status" => "hey @#{other_user.nickname}!"
333 TwitterAPI.create_status(user, %{
334 "status" => "hey again @#{other_user.nickname}!"
337 [n2, n1] = notifs = Notification.for_user(other_user)
338 assert length(notifs) == 2
343 TwitterAPI.create_status(user, %{
344 "status" => "hey yet again @#{other_user.nickname}!"
347 Notification.set_read_up_to(other_user, n2.id)
349 [n3, n2, n1] = Notification.for_user(other_user)
351 assert n1.seen == true
352 assert n2.seen == true
353 assert n3.seen == false
357 describe "for_user_since/2" do
358 defp days_ago(days) do
360 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
361 -days * 60 * 60 * 24,
366 test "Returns recent notifications" do
367 user1 = insert(:user)
368 user2 = insert(:user)
370 Enum.each(0..10, fn i ->
372 CommonAPI.post(user1, %{
373 "status" => "hey ##{i} @#{user2.nickname}!"
377 {old, new} = Enum.split(Notification.for_user(user2), 5)
379 Enum.each(old, fn notification ->
381 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
382 |> Pleroma.Repo.update!()
385 recent_notifications_ids =
387 |> Notification.for_user_since(
388 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
392 Enum.each(old, fn %{id: id} ->
393 refute id in recent_notifications_ids
396 Enum.each(new, fn %{id: id} ->
397 assert id in recent_notifications_ids
402 describe "notification target determination" do
403 test "it sends notifications to addressed users in new messages" do
405 other_user = insert(:user)
408 CommonAPI.post(user, %{
409 "status" => "hey @#{other_user.nickname}!"
412 assert other_user in Notification.get_notified_from_activity(activity)
415 test "it sends notifications to mentioned users in new messages" do
417 other_user = insert(:user)
420 "@context" => "https://www.w3.org/ns/activitystreams",
422 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
423 "actor" => user.ap_id,
426 "content" => "message with a Mention tag, but no explicit tagging",
430 "href" => other_user.ap_id,
431 "name" => other_user.nickname
434 "attributedTo" => user.ap_id
438 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
440 assert other_user in Notification.get_notified_from_activity(activity)
443 test "it does not send notifications to users who are only cc in new messages" do
445 other_user = insert(:user)
448 "@context" => "https://www.w3.org/ns/activitystreams",
450 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
451 "cc" => [other_user.ap_id],
452 "actor" => user.ap_id,
455 "content" => "hi everyone",
456 "attributedTo" => user.ap_id
460 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
462 assert other_user not in Notification.get_notified_from_activity(activity)
465 test "it does not send notification to mentioned users in likes" do
467 other_user = insert(:user)
468 third_user = insert(:user)
470 {:ok, activity_one} =
471 CommonAPI.post(user, %{
472 "status" => "hey @#{other_user.nickname}!"
475 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
477 assert other_user not in Notification.get_notified_from_activity(activity_two)
480 test "it does not send notification to mentioned users in announces" do
482 other_user = insert(:user)
483 third_user = insert(:user)
485 {:ok, activity_one} =
486 CommonAPI.post(user, %{
487 "status" => "hey @#{other_user.nickname}!"
490 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
492 assert other_user not in Notification.get_notified_from_activity(activity_two)
496 describe "notification lifecycle" do
497 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
499 other_user = insert(:user)
501 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
503 assert Enum.empty?(Notification.for_user(user))
505 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
507 assert length(Notification.for_user(user)) == 1
509 {:ok, _} = CommonAPI.delete(activity.id, user)
511 assert Enum.empty?(Notification.for_user(user))
514 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
516 other_user = insert(:user)
518 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
520 assert Enum.empty?(Notification.for_user(user))
522 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
524 assert length(Notification.for_user(user)) == 1
526 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
528 assert Enum.empty?(Notification.for_user(user))
531 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
533 other_user = insert(:user)
535 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
537 assert Enum.empty?(Notification.for_user(user))
539 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
541 assert length(Notification.for_user(user)) == 1
543 {:ok, _} = CommonAPI.delete(activity.id, user)
545 assert Enum.empty?(Notification.for_user(user))
548 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
550 other_user = insert(:user)
552 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
554 assert Enum.empty?(Notification.for_user(user))
556 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
558 assert length(Notification.for_user(user)) == 1
560 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
562 assert Enum.empty?(Notification.for_user(user))
565 test "liking an activity which is already deleted does not generate a notification" do
567 other_user = insert(:user)
569 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
571 assert Enum.empty?(Notification.for_user(user))
573 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
575 assert Enum.empty?(Notification.for_user(user))
577 {:error, _} = CommonAPI.favorite(activity.id, other_user)
579 assert Enum.empty?(Notification.for_user(user))
582 test "repeating an activity which is already deleted does not generate a notification" do
584 other_user = insert(:user)
586 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
588 assert Enum.empty?(Notification.for_user(user))
590 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
592 assert Enum.empty?(Notification.for_user(user))
594 {:error, _} = CommonAPI.repeat(activity.id, other_user)
596 assert Enum.empty?(Notification.for_user(user))
599 test "replying to a deleted post without tagging does not generate a notification" do
601 other_user = insert(:user)
603 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
604 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
606 {:ok, _reply_activity} =
607 CommonAPI.post(other_user, %{
608 "status" => "test reply",
609 "in_reply_to_status_id" => activity.id
612 assert Enum.empty?(Notification.for_user(user))
615 test "notifications are deleted if a local user is deleted" do
617 other_user = insert(:user)
620 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
622 refute Enum.empty?(Notification.for_user(other_user))
626 assert Enum.empty?(Notification.for_user(other_user))
629 test "notifications are deleted if a remote user is deleted" do
630 remote_user = insert(:user)
631 local_user = insert(:user)
634 "@context" => "https://www.w3.org/ns/activitystreams",
636 "actor" => remote_user.ap_id,
637 "id" => remote_user.ap_id <> "/activities/test",
638 "to" => [local_user.ap_id],
642 "content" => "Hello!",
646 "href" => local_user.ap_id,
647 "name" => "@#{local_user.nickname}"
650 "to" => [local_user.ap_id],
652 "attributedTo" => remote_user.ap_id
656 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
658 refute Enum.empty?(Notification.for_user(local_user))
660 delete_user_message = %{
661 "@context" => "https://www.w3.org/ns/activitystreams",
662 "id" => remote_user.ap_id <> "/activities/delete",
663 "actor" => remote_user.ap_id,
665 "object" => remote_user.ap_id
668 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
670 assert Enum.empty?(Notification.for_user(local_user))
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} = TwitterAPI.create_status(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} = TwitterAPI.create_status(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} = TwitterAPI.create_status(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} = TwitterAPI.create_status(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)
720 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
722 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
723 assert Notification.for_user(user) == []
726 test "it returns notifications for muted user with notifications and with_muted parameter" do
728 muted = insert(:user)
729 {:ok, user} = User.mute(user, muted)
731 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
733 assert length(Notification.for_user(user, %{with_muted: true})) == 1
736 test "it returns notifications for blocked user and with_muted parameter" do
738 blocked = insert(:user)
739 {:ok, user} = User.block(user, blocked)
741 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
743 assert length(Notification.for_user(user, %{with_muted: true})) == 1
746 test "it returns notificatitons for blocked domain and with_muted parameter" do
748 blocked = insert(:user, ap_id: "http://some-domain.com")
749 {:ok, user} = User.block_domain(user, "some-domain.com")
751 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
753 assert length(Notification.for_user(user, %{with_muted: true})) == 1
756 test "it returns notifications for muted thread with_muted parameter" do
758 another_user = insert(:user)
761 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
763 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
764 assert length(Notification.for_user(user, %{with_muted: true})) == 1