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
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
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} = CommonAPI.follow(user, followed_user)
188 Notification.create_notification(activity, followed_user)
189 CommonAPI.unfollow(user, followed_user)
190 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
191 refute Notification.create_notification(activity_dupe, followed_user)
194 test "it doesn't create duplicate notifications for follow+subscribed users" do
196 subscriber = insert(:user)
198 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
199 User.subscribe(subscriber, user)
200 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
201 {:ok, [_notif]} = Notification.create_notifications(status)
204 test "it doesn't create subscription notifications if the recipient cannot see the status" do
206 subscriber = insert(:user)
208 User.subscribe(subscriber, user)
210 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
212 assert {:ok, []} == Notification.create_notifications(status)
216 describe "get notification" do
217 test "it gets a notification that belongs to the user" do
219 other_user = insert(:user)
221 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
223 {:ok, [notification]} = Notification.create_notifications(activity)
224 {:ok, notification} = Notification.get(other_user, notification.id)
226 assert notification.user_id == other_user.id
229 test "it returns error if the notification doesn't belong to the user" do
231 other_user = insert(:user)
233 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
235 {:ok, [notification]} = Notification.create_notifications(activity)
236 {:error, _notification} = Notification.get(user, notification.id)
240 describe "dismiss notification" do
241 test "it dismisses a notification that belongs to the user" do
243 other_user = insert(:user)
245 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
247 {:ok, [notification]} = Notification.create_notifications(activity)
248 {:ok, notification} = Notification.dismiss(other_user, notification.id)
250 assert notification.user_id == other_user.id
253 test "it returns error if the notification doesn't belong to the user" do
255 other_user = insert(:user)
257 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
259 {:ok, [notification]} = Notification.create_notifications(activity)
260 {:error, _notification} = Notification.dismiss(user, notification.id)
264 describe "clear notification" do
265 test "it clears all notifications belonging to the user" do
267 other_user = insert(:user)
268 third_user = insert(:user)
271 CommonAPI.post(user, %{
272 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
275 {:ok, _notifs} = Notification.create_notifications(activity)
278 CommonAPI.post(user, %{
279 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
282 {:ok, _notifs} = Notification.create_notifications(activity)
283 Notification.clear(other_user)
285 assert Notification.for_user(other_user) == []
286 assert Notification.for_user(third_user) != []
290 describe "set_read_up_to()" do
291 test "it sets all notifications as read up to a specified notification ID" do
293 other_user = insert(:user)
296 CommonAPI.post(user, %{
297 "status" => "hey @#{other_user.nickname}!"
301 CommonAPI.post(user, %{
302 "status" => "hey again @#{other_user.nickname}!"
305 [n2, n1] = notifs = Notification.for_user(other_user)
306 assert length(notifs) == 2
311 CommonAPI.post(user, %{
312 "status" => "hey yet again @#{other_user.nickname}!"
315 Notification.set_read_up_to(other_user, n2.id)
317 [n3, n2, n1] = Notification.for_user(other_user)
319 assert n1.seen == true
320 assert n2.seen == true
321 assert n3.seen == false
325 describe "for_user_since/2" do
326 defp days_ago(days) do
328 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
329 -days * 60 * 60 * 24,
334 test "Returns recent notifications" do
335 user1 = insert(:user)
336 user2 = insert(:user)
338 Enum.each(0..10, fn i ->
340 CommonAPI.post(user1, %{
341 "status" => "hey ##{i} @#{user2.nickname}!"
345 {old, new} = Enum.split(Notification.for_user(user2), 5)
347 Enum.each(old, fn notification ->
349 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
350 |> Pleroma.Repo.update!()
353 recent_notifications_ids =
355 |> Notification.for_user_since(
356 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
360 Enum.each(old, fn %{id: id} ->
361 refute id in recent_notifications_ids
364 Enum.each(new, fn %{id: id} ->
365 assert id in recent_notifications_ids
370 describe "notification target determination" do
371 test "it sends notifications to addressed users in new messages" do
373 other_user = insert(:user)
376 CommonAPI.post(user, %{
377 "status" => "hey @#{other_user.nickname}!"
380 assert other_user in Notification.get_notified_from_activity(activity)
383 test "it sends notifications to mentioned users in new messages" do
385 other_user = insert(:user)
388 "@context" => "https://www.w3.org/ns/activitystreams",
390 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
391 "actor" => user.ap_id,
394 "content" => "message with a Mention tag, but no explicit tagging",
398 "href" => other_user.ap_id,
399 "name" => other_user.nickname
402 "attributedTo" => user.ap_id
406 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
408 assert other_user in Notification.get_notified_from_activity(activity)
411 test "it does not send notifications to users who are only cc in new messages" do
413 other_user = insert(:user)
416 "@context" => "https://www.w3.org/ns/activitystreams",
418 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
419 "cc" => [other_user.ap_id],
420 "actor" => user.ap_id,
423 "content" => "hi everyone",
424 "attributedTo" => user.ap_id
428 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
430 assert other_user not in Notification.get_notified_from_activity(activity)
433 test "it does not send notification to mentioned users in likes" do
435 other_user = insert(:user)
436 third_user = insert(:user)
438 {:ok, activity_one} =
439 CommonAPI.post(user, %{
440 "status" => "hey @#{other_user.nickname}!"
443 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
445 assert other_user not in Notification.get_notified_from_activity(activity_two)
448 test "it does not send notification to mentioned users in announces" do
450 other_user = insert(:user)
451 third_user = insert(:user)
453 {:ok, activity_one} =
454 CommonAPI.post(user, %{
455 "status" => "hey @#{other_user.nickname}!"
458 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
460 assert other_user not in Notification.get_notified_from_activity(activity_two)
464 describe "notification lifecycle" do
465 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
467 other_user = insert(:user)
469 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
471 assert Enum.empty?(Notification.for_user(user))
473 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
475 assert length(Notification.for_user(user)) == 1
477 {:ok, _} = CommonAPI.delete(activity.id, user)
479 assert Enum.empty?(Notification.for_user(user))
482 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
484 other_user = insert(:user)
486 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
488 assert Enum.empty?(Notification.for_user(user))
490 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
492 assert length(Notification.for_user(user)) == 1
494 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
496 assert Enum.empty?(Notification.for_user(user))
499 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
501 other_user = insert(:user)
503 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
505 assert Enum.empty?(Notification.for_user(user))
507 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
509 assert length(Notification.for_user(user)) == 1
511 {:ok, _} = CommonAPI.delete(activity.id, user)
513 assert Enum.empty?(Notification.for_user(user))
516 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
518 other_user = insert(:user)
520 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
522 assert Enum.empty?(Notification.for_user(user))
524 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
526 assert length(Notification.for_user(user)) == 1
528 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
530 assert Enum.empty?(Notification.for_user(user))
533 test "liking an activity which is already deleted does not generate a notification" do
535 other_user = insert(:user)
537 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
539 assert Enum.empty?(Notification.for_user(user))
541 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
543 assert Enum.empty?(Notification.for_user(user))
545 {:error, _} = CommonAPI.favorite(activity.id, other_user)
547 assert Enum.empty?(Notification.for_user(user))
550 test "repeating an activity which is already deleted does not generate a notification" do
552 other_user = insert(:user)
554 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
556 assert Enum.empty?(Notification.for_user(user))
558 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
560 assert Enum.empty?(Notification.for_user(user))
562 {:error, _} = CommonAPI.repeat(activity.id, other_user)
564 assert Enum.empty?(Notification.for_user(user))
567 test "replying to a deleted post without tagging does not generate a notification" do
569 other_user = insert(:user)
571 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
572 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
574 {:ok, _reply_activity} =
575 CommonAPI.post(other_user, %{
576 "status" => "test reply",
577 "in_reply_to_status_id" => activity.id
580 assert Enum.empty?(Notification.for_user(user))
583 test "notifications are deleted if a local user is deleted" do
585 other_user = insert(:user)
588 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
590 refute Enum.empty?(Notification.for_user(other_user))
592 {:ok, job} = User.delete(user)
593 ObanHelpers.perform(job)
595 assert Enum.empty?(Notification.for_user(other_user))
598 test "notifications are deleted if a remote user is deleted" do
599 remote_user = insert(:user)
600 local_user = insert(:user)
603 "@context" => "https://www.w3.org/ns/activitystreams",
605 "actor" => remote_user.ap_id,
606 "id" => remote_user.ap_id <> "/activities/test",
607 "to" => [local_user.ap_id],
611 "content" => "Hello!",
615 "href" => local_user.ap_id,
616 "name" => "@#{local_user.nickname}"
619 "to" => [local_user.ap_id],
621 "attributedTo" => remote_user.ap_id
625 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
627 refute Enum.empty?(Notification.for_user(local_user))
629 delete_user_message = %{
630 "@context" => "https://www.w3.org/ns/activitystreams",
631 "id" => remote_user.ap_id <> "/activities/delete",
632 "actor" => remote_user.ap_id,
634 "object" => remote_user.ap_id
637 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
638 ObanHelpers.perform_all()
640 assert Enum.empty?(Notification.for_user(local_user))
644 describe "for_user" do
645 test "it returns notifications for muted user without notifications" do
647 muted = insert(:user)
648 {:ok, user} = User.mute(user, muted, false)
650 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
652 assert length(Notification.for_user(user)) == 1
655 test "it doesn't return notifications for muted user with notifications" do
657 muted = insert(:user)
658 {:ok, user} = User.mute(user, muted)
660 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
662 assert Notification.for_user(user) == []
665 test "it doesn't return notifications for blocked user" do
667 blocked = insert(:user)
668 {:ok, user} = User.block(user, blocked)
670 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
672 assert Notification.for_user(user) == []
675 test "it doesn't return notificatitons for blocked domain" do
677 blocked = insert(:user, ap_id: "http://some-domain.com")
678 {:ok, user} = User.block_domain(user, "some-domain.com")
680 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
682 assert Notification.for_user(user) == []
685 test "it doesn't return notifications for muted thread" do
687 another_user = insert(:user)
689 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
691 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
692 assert Notification.for_user(user) == []
695 test "it returns notifications for muted user with notifications and with_muted parameter" do
697 muted = insert(:user)
698 {:ok, user} = User.mute(user, muted)
700 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
702 assert length(Notification.for_user(user, %{with_muted: true})) == 1
705 test "it returns notifications for blocked user and with_muted parameter" do
707 blocked = insert(:user)
708 {:ok, user} = User.block(user, blocked)
710 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
712 assert length(Notification.for_user(user, %{with_muted: true})) == 1
715 test "it returns notificatitons for blocked domain and with_muted parameter" do
717 blocked = insert(:user, ap_id: "http://some-domain.com")
718 {:ok, user} = User.block_domain(user, "some-domain.com")
720 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
722 assert length(Notification.for_user(user, %{with_muted: true})) == 1
725 test "it returns notifications for muted thread with_muted parameter" do
727 another_user = insert(:user)
729 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
731 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
732 assert length(Notification.for_user(user, %{with_muted: true})) == 1