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
16 describe "create_notifications" do
17 test "notifies someone when they are directly addressed" do
19 other_user = insert(:user)
20 third_user = insert(:user)
23 CommonAPI.post(user, %{
24 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
27 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
29 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
30 assert notified_ids == [other_user.id, third_user.id]
31 assert notification.activity_id == activity.id
32 assert other_notification.activity_id == activity.id
35 test "it creates a notification for subscribed users" do
37 subscriber = insert(:user)
39 User.subscribe(subscriber, user)
41 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
42 {:ok, [notification]} = Notification.create_notifications(status)
44 assert notification.user_id == subscriber.id
47 test "does not create a notification for subscribed users if status is a reply" do
49 other_user = insert(:user)
50 subscriber = insert(:user)
52 User.subscribe(subscriber, other_user)
54 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
56 {:ok, _reply_activity} =
57 CommonAPI.post(other_user, %{
58 "status" => "test reply",
59 "in_reply_to_status_id" => activity.id
62 user_notifications = Notification.for_user(user)
63 assert length(user_notifications) == 1
65 subscriber_notifications = Notification.for_user(subscriber)
66 assert Enum.empty?(subscriber_notifications)
70 describe "create_notification" do
71 @tag needs_streamer: true
72 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
74 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
75 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
76 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
80 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
83 activity = insert(:note_activity)
85 notify = Notification.create_notification(activity, user)
86 assert notify.user_id == user.id
88 Task.await(task_user_notification)
91 test "it creates a notification for user if the user blocks the activity author" do
92 activity = insert(:note_activity)
93 author = User.get_cached_by_ap_id(activity.data["actor"])
95 {:ok, user} = User.block(user, author)
97 assert Notification.create_notification(activity, user)
100 test "it creates a notificatin for the user if the user mutes the activity author" do
101 muter = insert(:user)
102 muted = insert(:user)
103 {:ok, _} = User.mute(muter, muted)
104 muter = Repo.get(User, muter.id)
105 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
107 assert Notification.create_notification(activity, muter)
110 test "notification created if user is muted without notifications" do
111 muter = insert(:user)
112 muted = insert(:user)
114 {:ok, muter} = User.mute(muter, muted, false)
116 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
118 assert Notification.create_notification(activity, muter)
121 test "it creates a notification for an activity from a muted thread" do
122 muter = insert(:user)
123 other_user = insert(:user)
124 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
125 CommonAPI.add_mute(muter, activity)
128 CommonAPI.post(other_user, %{
129 "status" => "Hi @#{muter.nickname}",
130 "in_reply_to_status_id" => activity.id
133 assert Notification.create_notification(activity, muter)
136 test "it disables notifications from followers" do
137 follower = insert(:user)
138 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
139 User.follow(follower, followed)
140 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
141 refute Notification.create_notification(activity, followed)
144 test "it disables notifications from non-followers" do
145 follower = insert(:user)
146 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
147 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
148 refute Notification.create_notification(activity, followed)
151 test "it disables notifications from people the user follows" do
152 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
153 followed = insert(:user)
154 User.follow(follower, followed)
155 follower = Repo.get(User, follower.id)
156 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
157 refute Notification.create_notification(activity, follower)
160 test "it disables notifications from people the user does not follow" do
161 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
162 followed = insert(:user)
163 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
164 refute Notification.create_notification(activity, follower)
167 test "it doesn't create a notification for user if he is the activity author" do
168 activity = insert(:note_activity)
169 author = User.get_cached_by_ap_id(activity.data["actor"])
171 refute Notification.create_notification(activity, author)
174 test "it doesn't create a notification for follow-unfollow-follow chains" do
176 followed_user = insert(:user)
177 {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
178 Notification.create_notification(activity, followed_user)
179 CommonAPI.unfollow(user, followed_user)
180 {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
181 refute Notification.create_notification(activity_dupe, followed_user)
184 test "it doesn't create duplicate notifications for follow+subscribed users" do
186 subscriber = insert(:user)
188 {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
189 User.subscribe(subscriber, user)
190 {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
191 {:ok, [_notif]} = Notification.create_notifications(status)
194 test "it doesn't create subscription notifications if the recipient cannot see the status" do
196 subscriber = insert(:user)
198 User.subscribe(subscriber, user)
200 {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
202 assert {:ok, []} == Notification.create_notifications(status)
206 describe "get notification" do
207 test "it gets a notification that belongs to the user" do
209 other_user = insert(:user)
211 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
213 {:ok, [notification]} = Notification.create_notifications(activity)
214 {:ok, notification} = Notification.get(other_user, notification.id)
216 assert notification.user_id == other_user.id
219 test "it returns error if the notification doesn't belong to the user" do
221 other_user = insert(:user)
223 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
225 {:ok, [notification]} = Notification.create_notifications(activity)
226 {:error, _notification} = Notification.get(user, notification.id)
230 describe "dismiss notification" do
231 test "it dismisses a notification that belongs to the user" do
233 other_user = insert(:user)
235 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
237 {:ok, [notification]} = Notification.create_notifications(activity)
238 {:ok, notification} = Notification.dismiss(other_user, notification.id)
240 assert notification.user_id == other_user.id
243 test "it returns error if the notification doesn't belong to the user" do
245 other_user = insert(:user)
247 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
249 {:ok, [notification]} = Notification.create_notifications(activity)
250 {:error, _notification} = Notification.dismiss(user, notification.id)
254 describe "clear notification" do
255 test "it clears all notifications belonging to the user" do
257 other_user = insert(:user)
258 third_user = insert(:user)
261 CommonAPI.post(user, %{
262 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
265 {:ok, _notifs} = Notification.create_notifications(activity)
268 CommonAPI.post(user, %{
269 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
272 {:ok, _notifs} = Notification.create_notifications(activity)
273 Notification.clear(other_user)
275 assert Notification.for_user(other_user) == []
276 assert Notification.for_user(third_user) != []
280 describe "set_read_up_to()" do
281 test "it sets all notifications as read up to a specified notification ID" do
283 other_user = insert(:user)
286 CommonAPI.post(user, %{
287 "status" => "hey @#{other_user.nickname}!"
291 CommonAPI.post(user, %{
292 "status" => "hey again @#{other_user.nickname}!"
295 [n2, n1] = notifs = Notification.for_user(other_user)
296 assert length(notifs) == 2
301 CommonAPI.post(user, %{
302 "status" => "hey yet again @#{other_user.nickname}!"
305 Notification.set_read_up_to(other_user, n2.id)
307 [n3, n2, n1] = Notification.for_user(other_user)
309 assert n1.seen == true
310 assert n2.seen == true
311 assert n3.seen == false
315 describe "for_user_since/2" do
316 defp days_ago(days) do
318 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
319 -days * 60 * 60 * 24,
324 test "Returns recent notifications" do
325 user1 = insert(:user)
326 user2 = insert(:user)
328 Enum.each(0..10, fn i ->
330 CommonAPI.post(user1, %{
331 "status" => "hey ##{i} @#{user2.nickname}!"
335 {old, new} = Enum.split(Notification.for_user(user2), 5)
337 Enum.each(old, fn notification ->
339 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
340 |> Pleroma.Repo.update!()
343 recent_notifications_ids =
345 |> Notification.for_user_since(
346 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
350 Enum.each(old, fn %{id: id} ->
351 refute id in recent_notifications_ids
354 Enum.each(new, fn %{id: id} ->
355 assert id in recent_notifications_ids
360 describe "notification target determination" do
361 test "it sends notifications to addressed users in new messages" do
363 other_user = insert(:user)
366 CommonAPI.post(user, %{
367 "status" => "hey @#{other_user.nickname}!"
370 assert other_user in Notification.get_notified_from_activity(activity)
373 test "it sends notifications to mentioned users in new messages" do
375 other_user = insert(:user)
378 "@context" => "https://www.w3.org/ns/activitystreams",
380 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
381 "actor" => user.ap_id,
384 "content" => "message with a Mention tag, but no explicit tagging",
388 "href" => other_user.ap_id,
389 "name" => other_user.nickname
392 "attributedTo" => user.ap_id
396 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
398 assert other_user in Notification.get_notified_from_activity(activity)
401 test "it does not send notifications to users who are only cc in new messages" do
403 other_user = insert(:user)
406 "@context" => "https://www.w3.org/ns/activitystreams",
408 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
409 "cc" => [other_user.ap_id],
410 "actor" => user.ap_id,
413 "content" => "hi everyone",
414 "attributedTo" => user.ap_id
418 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
420 assert other_user not in Notification.get_notified_from_activity(activity)
423 test "it does not send notification to mentioned users in likes" do
425 other_user = insert(:user)
426 third_user = insert(:user)
428 {:ok, activity_one} =
429 CommonAPI.post(user, %{
430 "status" => "hey @#{other_user.nickname}!"
433 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
435 assert other_user not in Notification.get_notified_from_activity(activity_two)
438 test "it does not send notification to mentioned users in announces" do
440 other_user = insert(:user)
441 third_user = insert(:user)
443 {:ok, activity_one} =
444 CommonAPI.post(user, %{
445 "status" => "hey @#{other_user.nickname}!"
448 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
450 assert other_user not in Notification.get_notified_from_activity(activity_two)
454 describe "notification lifecycle" do
455 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
457 other_user = insert(:user)
459 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
461 assert Enum.empty?(Notification.for_user(user))
463 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
465 assert length(Notification.for_user(user)) == 1
467 {:ok, _} = CommonAPI.delete(activity.id, user)
469 assert Enum.empty?(Notification.for_user(user))
472 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
474 other_user = insert(:user)
476 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
478 assert Enum.empty?(Notification.for_user(user))
480 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
482 assert length(Notification.for_user(user)) == 1
484 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
486 assert Enum.empty?(Notification.for_user(user))
489 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
491 other_user = insert(:user)
493 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
495 assert Enum.empty?(Notification.for_user(user))
497 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
499 assert length(Notification.for_user(user)) == 1
501 {:ok, _} = CommonAPI.delete(activity.id, user)
503 assert Enum.empty?(Notification.for_user(user))
506 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
508 other_user = insert(:user)
510 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
512 assert Enum.empty?(Notification.for_user(user))
514 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
516 assert length(Notification.for_user(user)) == 1
518 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
520 assert Enum.empty?(Notification.for_user(user))
523 test "liking an activity which is already deleted does not generate a notification" do
525 other_user = insert(:user)
527 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
529 assert Enum.empty?(Notification.for_user(user))
531 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
533 assert Enum.empty?(Notification.for_user(user))
535 {:error, _} = CommonAPI.favorite(activity.id, other_user)
537 assert Enum.empty?(Notification.for_user(user))
540 test "repeating an activity which is already deleted does not generate a notification" do
542 other_user = insert(:user)
544 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
546 assert Enum.empty?(Notification.for_user(user))
548 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
550 assert Enum.empty?(Notification.for_user(user))
552 {:error, _} = CommonAPI.repeat(activity.id, other_user)
554 assert Enum.empty?(Notification.for_user(user))
557 test "replying to a deleted post without tagging does not generate a notification" do
559 other_user = insert(:user)
561 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
562 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
564 {:ok, _reply_activity} =
565 CommonAPI.post(other_user, %{
566 "status" => "test reply",
567 "in_reply_to_status_id" => activity.id
570 assert Enum.empty?(Notification.for_user(user))
573 test "notifications are deleted if a local user is deleted" do
575 other_user = insert(:user)
578 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
580 refute Enum.empty?(Notification.for_user(other_user))
584 assert Enum.empty?(Notification.for_user(other_user))
587 test "notifications are deleted if a remote user is deleted" do
588 remote_user = insert(:user)
589 local_user = insert(:user)
592 "@context" => "https://www.w3.org/ns/activitystreams",
594 "actor" => remote_user.ap_id,
595 "id" => remote_user.ap_id <> "/activities/test",
596 "to" => [local_user.ap_id],
600 "content" => "Hello!",
604 "href" => local_user.ap_id,
605 "name" => "@#{local_user.nickname}"
608 "to" => [local_user.ap_id],
610 "attributedTo" => remote_user.ap_id
614 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
616 refute Enum.empty?(Notification.for_user(local_user))
618 delete_user_message = %{
619 "@context" => "https://www.w3.org/ns/activitystreams",
620 "id" => remote_user.ap_id <> "/activities/delete",
621 "actor" => remote_user.ap_id,
623 "object" => remote_user.ap_id
626 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
628 assert Enum.empty?(Notification.for_user(local_user))
632 describe "for_user" do
633 test "it returns notifications for muted user without notifications" do
635 muted = insert(:user)
636 {:ok, user} = User.mute(user, muted, false)
638 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
640 assert length(Notification.for_user(user)) == 1
643 test "it doesn't return notifications for muted user with notifications" do
645 muted = insert(:user)
646 {:ok, user} = User.mute(user, muted)
648 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
650 assert Notification.for_user(user) == []
653 test "it doesn't return notifications for blocked user" do
655 blocked = insert(:user)
656 {:ok, user} = User.block(user, blocked)
658 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
660 assert Notification.for_user(user) == []
663 test "it doesn't return notificatitons for blocked domain" do
665 blocked = insert(:user, ap_id: "http://some-domain.com")
666 {:ok, user} = User.block_domain(user, "some-domain.com")
668 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
670 assert Notification.for_user(user) == []
673 test "it doesn't return notifications for muted thread" do
675 another_user = insert(:user)
677 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
679 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
680 assert Notification.for_user(user) == []
683 test "it returns notifications from a muted user when with_muted is set" do
685 muted = insert(:user)
686 {:ok, user} = User.mute(user, muted)
688 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
690 assert length(Notification.for_user(user, %{with_muted: true})) == 1
693 test "it doesn't return notifications from a blocked user when with_muted is set" do
695 blocked = insert(:user)
696 {:ok, user} = User.block(user, blocked)
698 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
700 assert length(Notification.for_user(user, %{with_muted: true})) == 0
703 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
705 blocked = insert(:user, ap_id: "http://some-domain.com")
706 {:ok, user} = User.block_domain(user, "some-domain.com")
708 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
710 assert length(Notification.for_user(user, %{with_muted: true})) == 0
713 test "it returns notifications from muted threads when with_muted is set" do
715 another_user = insert(:user)
717 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
719 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
720 assert length(Notification.for_user(user, %{with_muted: true})) == 1