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 does not create 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, notifications} = Notification.create_notifications(status)
45 assert notifications == []
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} = 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, muter} = 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)
139 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
140 User.follow(follower, followed)
141 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
142 refute Notification.create_notification(activity, followed)
145 test "it disables notifications from non-followers" do
146 follower = insert(:user)
147 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
148 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
149 refute Notification.create_notification(activity, followed)
152 test "it disables notifications from people the user follows" do
153 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
154 followed = insert(:user)
155 User.follow(follower, followed)
156 follower = Repo.get(User, follower.id)
157 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
158 refute Notification.create_notification(activity, follower)
161 test "it disables notifications from people the user does not follow" do
162 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
163 followed = insert(:user)
164 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
165 refute Notification.create_notification(activity, follower)
168 test "it doesn't create a notification for user if he is the activity author" do
169 activity = insert(:note_activity)
170 author = User.get_cached_by_ap_id(activity.data["actor"])
172 refute Notification.create_notification(activity, author)
175 test "it doesn't create a notification for follow-unfollow-follow chains" do
177 followed_user = insert(:user)
178 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
179 Notification.create_notification(activity, followed_user)
180 CommonAPI.unfollow(user, followed_user)
181 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
182 refute Notification.create_notification(activity_dupe, followed_user)
185 test "it doesn't create notifications for follow+subscribed users" do
187 subscriber = insert(:user)
189 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
190 User.subscribe(subscriber, user)
191 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
192 {:ok, notifications} = Notification.create_notifications(status)
194 assert notifications == []
197 test "it doesn't create subscription notifications if the recipient cannot see the status" do
199 subscriber = insert(:user)
201 User.subscribe(subscriber, user)
203 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
205 assert {:ok, []} == Notification.create_notifications(status)
209 describe "get notification" do
210 test "it gets a notification that belongs to the user" do
212 other_user = insert(:user)
214 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
216 {:ok, [notification]} = Notification.create_notifications(activity)
217 {:ok, notification} = Notification.get(other_user, notification.id)
219 assert notification.user_id == other_user.id
222 test "it returns error if the notification doesn't belong to the user" do
224 other_user = insert(:user)
226 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
228 {:ok, [notification]} = Notification.create_notifications(activity)
229 {:error, _notification} = Notification.get(user, notification.id)
233 describe "dismiss notification" do
234 test "it dismisses a notification that belongs to the user" do
236 other_user = insert(:user)
238 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
240 {:ok, [notification]} = Notification.create_notifications(activity)
241 {:ok, notification} = Notification.dismiss(other_user, notification.id)
243 assert notification.user_id == other_user.id
246 test "it returns error if the notification doesn't belong to the user" do
248 other_user = insert(:user)
250 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
252 {:ok, [notification]} = Notification.create_notifications(activity)
253 {:error, _notification} = Notification.dismiss(user, notification.id)
257 describe "clear notification" do
258 test "it clears all notifications belonging to the user" do
260 other_user = insert(:user)
261 third_user = insert(:user)
264 CommonAPI.post(user, %{
265 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
268 {:ok, _notifs} = Notification.create_notifications(activity)
271 CommonAPI.post(user, %{
272 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
275 {:ok, _notifs} = Notification.create_notifications(activity)
276 Notification.clear(other_user)
278 assert Notification.for_user(other_user) == []
279 assert Notification.for_user(third_user) != []
283 describe "set_read_up_to()" do
284 test "it sets all notifications as read up to a specified notification ID" do
286 other_user = insert(:user)
289 CommonAPI.post(user, %{
290 "status" => "hey @#{other_user.nickname}!"
294 CommonAPI.post(user, %{
295 "status" => "hey again @#{other_user.nickname}!"
298 [n2, n1] = notifs = Notification.for_user(other_user)
299 assert length(notifs) == 2
304 CommonAPI.post(user, %{
305 "status" => "hey yet again @#{other_user.nickname}!"
308 Notification.set_read_up_to(other_user, n2.id)
310 [n3, n2, n1] = Notification.for_user(other_user)
312 assert n1.seen == true
313 assert n2.seen == true
314 assert n3.seen == false
318 describe "for_user_since/2" do
319 defp days_ago(days) do
321 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
322 -days * 60 * 60 * 24,
327 test "Returns recent notifications" do
328 user1 = insert(:user)
329 user2 = insert(:user)
331 Enum.each(0..10, fn i ->
333 CommonAPI.post(user1, %{
334 "status" => "hey ##{i} @#{user2.nickname}!"
338 {old, new} = Enum.split(Notification.for_user(user2), 5)
340 Enum.each(old, fn notification ->
342 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
343 |> Pleroma.Repo.update!()
346 recent_notifications_ids =
348 |> Notification.for_user_since(
349 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
353 Enum.each(old, fn %{id: id} ->
354 refute id in recent_notifications_ids
357 Enum.each(new, fn %{id: id} ->
358 assert id in recent_notifications_ids
363 describe "notification target determination" do
364 test "it sends notifications to addressed users in new messages" do
366 other_user = insert(:user)
369 CommonAPI.post(user, %{
370 "status" => "hey @#{other_user.nickname}!"
373 assert other_user in Notification.get_notified_from_activity(activity)
376 test "it sends notifications to mentioned users in new messages" do
378 other_user = insert(:user)
381 "@context" => "https://www.w3.org/ns/activitystreams",
383 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
384 "actor" => user.ap_id,
387 "content" => "message with a Mention tag, but no explicit tagging",
391 "href" => other_user.ap_id,
392 "name" => other_user.nickname
395 "attributedTo" => user.ap_id
399 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
401 assert other_user in Notification.get_notified_from_activity(activity)
404 test "it does not send notifications to users who are only cc in new messages" do
406 other_user = insert(:user)
409 "@context" => "https://www.w3.org/ns/activitystreams",
411 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
412 "cc" => [other_user.ap_id],
413 "actor" => user.ap_id,
416 "content" => "hi everyone",
417 "attributedTo" => user.ap_id
421 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
423 assert other_user not in Notification.get_notified_from_activity(activity)
426 test "it does not send notification to mentioned users in likes" do
428 other_user = insert(:user)
429 third_user = insert(:user)
431 {:ok, activity_one} =
432 CommonAPI.post(user, %{
433 "status" => "hey @#{other_user.nickname}!"
436 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
438 assert other_user not in Notification.get_notified_from_activity(activity_two)
441 test "it does not send notification to mentioned users in announces" do
443 other_user = insert(:user)
444 third_user = insert(:user)
446 {:ok, activity_one} =
447 CommonAPI.post(user, %{
448 "status" => "hey @#{other_user.nickname}!"
451 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
453 assert other_user not in Notification.get_notified_from_activity(activity_two)
457 describe "notification lifecycle" do
458 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
460 other_user = insert(:user)
462 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
464 assert Enum.empty?(Notification.for_user(user))
466 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
468 assert length(Notification.for_user(user)) == 1
470 {:ok, _} = CommonAPI.delete(activity.id, user)
472 assert Enum.empty?(Notification.for_user(user))
475 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
477 other_user = insert(:user)
479 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
481 assert Enum.empty?(Notification.for_user(user))
483 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
485 assert length(Notification.for_user(user)) == 1
487 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
489 assert Enum.empty?(Notification.for_user(user))
492 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
494 other_user = insert(:user)
496 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
498 assert Enum.empty?(Notification.for_user(user))
500 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
502 assert length(Notification.for_user(user)) == 1
504 {:ok, _} = CommonAPI.delete(activity.id, user)
506 assert Enum.empty?(Notification.for_user(user))
509 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
511 other_user = insert(:user)
513 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
515 assert Enum.empty?(Notification.for_user(user))
517 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
519 assert length(Notification.for_user(user)) == 1
521 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
523 assert Enum.empty?(Notification.for_user(user))
526 test "liking an activity which is already deleted does not generate a notification" do
528 other_user = insert(:user)
530 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
532 assert Enum.empty?(Notification.for_user(user))
534 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
536 assert Enum.empty?(Notification.for_user(user))
538 {:error, _} = CommonAPI.favorite(activity.id, other_user)
540 assert Enum.empty?(Notification.for_user(user))
543 test "repeating an activity which is already deleted does not generate a notification" do
545 other_user = insert(:user)
547 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
549 assert Enum.empty?(Notification.for_user(user))
551 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
553 assert Enum.empty?(Notification.for_user(user))
555 {:error, _} = CommonAPI.repeat(activity.id, other_user)
557 assert Enum.empty?(Notification.for_user(user))
560 test "replying to a deleted post without tagging does not generate a notification" do
562 other_user = insert(:user)
564 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
565 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
567 {:ok, _reply_activity} =
568 CommonAPI.post(other_user, %{
569 "status" => "test reply",
570 "in_reply_to_status_id" => activity.id
573 assert Enum.empty?(Notification.for_user(user))
576 test "notifications are deleted if a local user is deleted" do
578 other_user = insert(:user)
581 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
583 refute Enum.empty?(Notification.for_user(other_user))
585 {:ok, job} = User.delete(user)
586 ObanHelpers.perform(job)
588 assert Enum.empty?(Notification.for_user(other_user))
591 test "notifications are deleted if a remote user is deleted" do
592 remote_user = insert(:user)
593 local_user = insert(:user)
596 "@context" => "https://www.w3.org/ns/activitystreams",
598 "actor" => remote_user.ap_id,
599 "id" => remote_user.ap_id <> "/activities/test",
600 "to" => [local_user.ap_id],
604 "content" => "Hello!",
608 "href" => local_user.ap_id,
609 "name" => "@#{local_user.nickname}"
612 "to" => [local_user.ap_id],
614 "attributedTo" => remote_user.ap_id
618 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
620 refute Enum.empty?(Notification.for_user(local_user))
622 delete_user_message = %{
623 "@context" => "https://www.w3.org/ns/activitystreams",
624 "id" => remote_user.ap_id <> "/activities/delete",
625 "actor" => remote_user.ap_id,
627 "object" => remote_user.ap_id
630 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
631 ObanHelpers.perform_all()
633 assert Enum.empty?(Notification.for_user(local_user))
637 describe "for_user" do
638 test "it returns notifications for muted user without notifications" do
640 muted = insert(:user)
641 {:ok, user} = User.mute(user, muted, false)
643 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
645 assert length(Notification.for_user(user)) == 1
648 test "it doesn't return notifications for muted user with notifications" do
650 muted = insert(:user)
651 {:ok, user} = User.mute(user, muted)
653 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
655 assert Notification.for_user(user) == []
658 test "it doesn't return notifications for blocked user" do
660 blocked = insert(:user)
661 {:ok, user} = User.block(user, blocked)
663 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
665 assert Notification.for_user(user) == []
668 test "it doesn't return notificatitons for blocked domain" do
670 blocked = insert(:user, ap_id: "http://some-domain.com")
671 {:ok, user} = User.block_domain(user, "some-domain.com")
673 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
675 assert Notification.for_user(user) == []
678 test "it doesn't return notifications for muted thread" do
680 another_user = insert(:user)
682 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
684 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
685 assert Notification.for_user(user) == []
688 test "it returns notifications for muted user with notifications and with_muted parameter" do
690 muted = insert(:user)
691 {:ok, user} = User.mute(user, muted)
693 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
695 assert length(Notification.for_user(user, %{with_muted: true})) == 1
698 test "it returns notifications for blocked user and with_muted parameter" do
700 blocked = insert(:user)
701 {:ok, user} = User.block(user, blocked)
703 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
705 assert length(Notification.for_user(user, %{with_muted: true})) == 1
708 test "it returns notificatitons for blocked domain and with_muted parameter" do
710 blocked = insert(:user, ap_id: "http://some-domain.com")
711 {:ok, user} = User.block_domain(user, "some-domain.com")
713 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
715 assert length(Notification.for_user(user, %{with_muted: true})) == 1
718 test "it returns notifications for muted thread with_muted parameter" do
720 another_user = insert(:user)
722 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
724 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
725 assert length(Notification.for_user(user, %{with_muted: true})) == 1