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
49 describe "create_notification" do
51 GenServer.start(Streamer, %{}, name: Streamer)
54 if pid = Process.whereis(Streamer) do
55 Process.exit(pid, :kill)
60 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
62 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
63 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
64 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
68 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
71 activity = insert(:note_activity)
73 notify = Notification.create_notification(activity, user)
74 assert notify.user_id == user.id
76 Task.await(task_user_notification)
79 test "it creates a notification for user if the user blocks the activity author" do
80 activity = insert(:note_activity)
81 author = User.get_cached_by_ap_id(activity.data["actor"])
83 {:ok, user} = User.block(user, author)
85 assert Notification.create_notification(activity, user)
88 test "it creates a notificatin for the user if the user mutes the activity author" do
91 {:ok, _} = User.mute(muter, muted)
92 muter = Repo.get(User, muter.id)
93 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
95 assert Notification.create_notification(activity, muter)
98 test "notification created if user is muted without notifications" do
100 muted = insert(:user)
102 {:ok, muter} = User.mute(muter, muted, false)
104 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
106 assert Notification.create_notification(activity, muter)
109 test "it creates a notification for an activity from a muted thread" do
110 muter = insert(:user)
111 other_user = insert(:user)
112 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
113 CommonAPI.add_mute(muter, activity)
116 CommonAPI.post(other_user, %{
117 "status" => "Hi @#{muter.nickname}",
118 "in_reply_to_status_id" => activity.id
121 assert Notification.create_notification(activity, muter)
124 test "it disables notifications from followers" do
125 follower = insert(:user)
126 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
127 User.follow(follower, followed)
128 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
129 refute Notification.create_notification(activity, followed)
132 test "it disables notifications from non-followers" do
133 follower = insert(:user)
134 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
135 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
136 refute Notification.create_notification(activity, followed)
139 test "it disables notifications from people the user follows" do
140 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
141 followed = insert(:user)
142 User.follow(follower, followed)
143 follower = Repo.get(User, follower.id)
144 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
145 refute Notification.create_notification(activity, follower)
148 test "it disables notifications from people the user does not follow" do
149 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
150 followed = insert(:user)
151 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
152 refute Notification.create_notification(activity, follower)
155 test "it doesn't create a notification for user if he is the activity author" do
156 activity = insert(:note_activity)
157 author = User.get_cached_by_ap_id(activity.data["actor"])
159 refute Notification.create_notification(activity, author)
162 test "it doesn't create a notification for follow-unfollow-follow chains" do
164 followed_user = insert(:user)
165 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
166 Notification.create_notification(activity, followed_user)
167 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
168 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
169 refute Notification.create_notification(activity_dupe, followed_user)
172 test "it doesn't create a notification for like-unlike-like chains" do
174 liked_user = insert(:user)
175 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
176 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
177 Notification.create_notification(fav_status, liked_user)
178 TwitterAPI.unfav(user, status.id)
179 {:ok, dupe} = TwitterAPI.fav(user, status.id)
180 refute Notification.create_notification(dupe, liked_user)
183 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
185 retweeted_user = insert(:user)
188 TwitterAPI.create_status(retweeted_user, %{
189 "status" => "Send dupe notifications to the shadow realm"
192 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
193 Notification.create_notification(retweeted_activity, retweeted_user)
194 TwitterAPI.unrepeat(user, status.id)
195 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
196 refute Notification.create_notification(dupe, retweeted_user)
199 test "it doesn't create duplicate notifications for follow+subscribed users" do
201 subscriber = insert(:user)
203 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
204 User.subscribe(subscriber, user)
205 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
206 {:ok, [_notif]} = Notification.create_notifications(status)
209 test "it doesn't create subscription notifications if the recipient cannot see the status" do
211 subscriber = insert(:user)
213 User.subscribe(subscriber, user)
216 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
218 assert {:ok, []} == Notification.create_notifications(status)
222 describe "get notification" do
223 test "it gets a notification that belongs to the user" do
225 other_user = insert(:user)
228 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
230 {:ok, [notification]} = Notification.create_notifications(activity)
231 {:ok, notification} = Notification.get(other_user, notification.id)
233 assert notification.user_id == other_user.id
236 test "it returns error if the notification doesn't belong to the user" do
238 other_user = insert(:user)
241 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
243 {:ok, [notification]} = Notification.create_notifications(activity)
244 {:error, _notification} = Notification.get(user, notification.id)
248 describe "dismiss notification" do
249 test "it dismisses a notification that belongs to the user" do
251 other_user = insert(:user)
254 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
256 {:ok, [notification]} = Notification.create_notifications(activity)
257 {:ok, notification} = Notification.dismiss(other_user, notification.id)
259 assert notification.user_id == other_user.id
262 test "it returns error if the notification doesn't belong to the user" do
264 other_user = insert(:user)
267 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
269 {:ok, [notification]} = Notification.create_notifications(activity)
270 {:error, _notification} = Notification.dismiss(user, notification.id)
274 describe "clear notification" do
275 test "it clears all notifications belonging to the user" do
277 other_user = insert(:user)
278 third_user = insert(:user)
281 TwitterAPI.create_status(user, %{
282 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
285 {:ok, _notifs} = Notification.create_notifications(activity)
288 TwitterAPI.create_status(user, %{
289 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
292 {:ok, _notifs} = Notification.create_notifications(activity)
293 Notification.clear(other_user)
295 assert Notification.for_user(other_user) == []
296 assert Notification.for_user(third_user) != []
300 describe "set_read_up_to()" do
301 test "it sets all notifications as read up to a specified notification ID" do
303 other_user = insert(:user)
306 TwitterAPI.create_status(user, %{
307 "status" => "hey @#{other_user.nickname}!"
311 TwitterAPI.create_status(user, %{
312 "status" => "hey again @#{other_user.nickname}!"
315 [n2, n1] = notifs = Notification.for_user(other_user)
316 assert length(notifs) == 2
321 TwitterAPI.create_status(user, %{
322 "status" => "hey yet again @#{other_user.nickname}!"
325 Notification.set_read_up_to(other_user, n2.id)
327 [n3, n2, n1] = Notification.for_user(other_user)
329 assert n1.seen == true
330 assert n2.seen == true
331 assert n3.seen == false
335 describe "for_user_since/2" do
336 defp days_ago(days) do
338 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
339 -days * 60 * 60 * 24,
344 test "Returns recent notifications" do
345 user1 = insert(:user)
346 user2 = insert(:user)
348 Enum.each(0..10, fn i ->
350 CommonAPI.post(user1, %{
351 "status" => "hey ##{i} @#{user2.nickname}!"
355 {old, new} = Enum.split(Notification.for_user(user2), 5)
357 Enum.each(old, fn notification ->
359 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
360 |> Pleroma.Repo.update!()
363 recent_notifications_ids =
365 |> Notification.for_user_since(
366 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
370 Enum.each(old, fn %{id: id} ->
371 refute id in recent_notifications_ids
374 Enum.each(new, fn %{id: id} ->
375 assert id in recent_notifications_ids
380 describe "notification target determination" do
381 test "it sends notifications to addressed users in new messages" do
383 other_user = insert(:user)
386 CommonAPI.post(user, %{
387 "status" => "hey @#{other_user.nickname}!"
390 assert other_user in Notification.get_notified_from_activity(activity)
393 test "it sends notifications to mentioned users in new messages" do
395 other_user = insert(:user)
398 "@context" => "https://www.w3.org/ns/activitystreams",
400 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
401 "actor" => user.ap_id,
404 "content" => "message with a Mention tag, but no explicit tagging",
408 "href" => other_user.ap_id,
409 "name" => other_user.nickname
412 "attributedTo" => user.ap_id
416 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
418 assert other_user in Notification.get_notified_from_activity(activity)
421 test "it does not send notifications to users who are only cc in new messages" do
423 other_user = insert(:user)
426 "@context" => "https://www.w3.org/ns/activitystreams",
428 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
429 "cc" => [other_user.ap_id],
430 "actor" => user.ap_id,
433 "content" => "hi everyone",
434 "attributedTo" => user.ap_id
438 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
440 assert other_user not in Notification.get_notified_from_activity(activity)
443 test "it does not send notification to mentioned users in likes" do
445 other_user = insert(:user)
446 third_user = insert(:user)
448 {:ok, activity_one} =
449 CommonAPI.post(user, %{
450 "status" => "hey @#{other_user.nickname}!"
453 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
455 assert other_user not in Notification.get_notified_from_activity(activity_two)
458 test "it does not send notification to mentioned users in announces" do
460 other_user = insert(:user)
461 third_user = insert(:user)
463 {:ok, activity_one} =
464 CommonAPI.post(user, %{
465 "status" => "hey @#{other_user.nickname}!"
468 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
470 assert other_user not in Notification.get_notified_from_activity(activity_two)
474 describe "notification lifecycle" do
475 test "liking an activity results in 1 notification, then 0 if the activity is deleted" 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.delete(activity.id, user)
489 assert Enum.empty?(Notification.for_user(user))
492 test "liking an activity results in 1 notification, then 0 if the activity is unliked" 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.favorite(activity.id, other_user)
502 assert length(Notification.for_user(user)) == 1
504 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
506 assert Enum.empty?(Notification.for_user(user))
509 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" 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.delete(activity.id, user)
523 assert Enum.empty?(Notification.for_user(user))
526 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" 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, _, _} = CommonAPI.repeat(activity.id, other_user)
536 assert length(Notification.for_user(user)) == 1
538 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
540 assert Enum.empty?(Notification.for_user(user))
543 test "liking 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.favorite(activity.id, other_user)
557 assert Enum.empty?(Notification.for_user(user))
560 test "repeating an activity which is already deleted does not generate a notification" do
562 other_user = insert(:user)
564 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
566 assert Enum.empty?(Notification.for_user(user))
568 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
570 assert Enum.empty?(Notification.for_user(user))
572 {:error, _} = CommonAPI.repeat(activity.id, other_user)
574 assert Enum.empty?(Notification.for_user(user))
577 test "replying to a deleted post without tagging does not generate a notification" do
579 other_user = insert(:user)
581 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
582 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
584 {:ok, _reply_activity} =
585 CommonAPI.post(other_user, %{
586 "status" => "test reply",
587 "in_reply_to_status_id" => activity.id
590 assert Enum.empty?(Notification.for_user(user))
594 describe "for_user" do
595 test "it returns notifications for muted user without notifications" do
597 muted = insert(:user)
598 {:ok, user} = User.mute(user, muted, false)
600 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
602 assert length(Notification.for_user(user)) == 1
605 test "it doesn't return notifications for muted user with notifications" do
607 muted = insert(:user)
608 {:ok, user} = User.mute(user, muted)
610 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
612 assert Notification.for_user(user) == []
615 test "it doesn't return notifications for blocked user" do
617 blocked = insert(:user)
618 {:ok, user} = User.block(user, blocked)
620 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
622 assert Notification.for_user(user) == []
625 test "it doesn't return notificatitons for blocked domain" do
627 blocked = insert(:user, ap_id: "http://some-domain.com")
628 {:ok, user} = User.block_domain(user, "some-domain.com")
630 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
632 assert Notification.for_user(user) == []
635 test "it doesn't return notifications for muted thread" do
637 another_user = insert(:user)
640 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
642 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
643 assert Notification.for_user(user) == []
646 test "it returns notifications for muted user with notifications and with_muted parameter" do
648 muted = insert(:user)
649 {:ok, user} = User.mute(user, muted)
651 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
653 assert length(Notification.for_user(user, %{with_muted: true})) == 1
656 test "it returns notifications for blocked user and with_muted parameter" do
658 blocked = insert(:user)
659 {:ok, user} = User.block(user, blocked)
661 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
663 assert length(Notification.for_user(user, %{with_muted: true})) == 1
666 test "it returns notificatitons for blocked domain and with_muted parameter" do
668 blocked = insert(:user, ap_id: "http://some-domain.com")
669 {:ok, user} = User.block_domain(user, "some-domain.com")
671 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
673 assert length(Notification.for_user(user, %{with_muted: true})) == 1
676 test "it returns notifications for muted thread with_muted parameter" do
678 another_user = insert(:user)
681 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
683 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
684 assert length(Notification.for_user(user, %{with_muted: true})) == 1