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
7 alias Pleroma.Notification
9 alias Pleroma.Web.ActivityPub.Transmogrifier
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.Streamer
12 alias Pleroma.Web.TwitterAPI.TwitterAPI
13 import Pleroma.Factory
15 describe "create_notifications" do
16 test "notifies someone when they are directly addressed" do
18 other_user = insert(:user)
19 third_user = insert(:user)
22 TwitterAPI.create_status(user, %{
23 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
26 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
28 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
29 assert notified_ids == [other_user.id, third_user.id]
30 assert notification.activity_id == activity.id
31 assert other_notification.activity_id == activity.id
34 test "it creates a notification for subscribed users" do
36 subscriber = insert(:user)
38 User.subscribe(subscriber, user)
40 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
41 {:ok, [notification]} = Notification.create_notifications(status)
43 assert notification.user_id == subscriber.id
46 test "does not create a notification for subscribed users if status is a reply" do
48 other_user = insert(:user)
49 subscriber = insert(:user)
51 User.subscribe(subscriber, other_user)
53 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
55 {:ok, reply_activity} =
56 CommonAPI.post(other_user, %{
57 "status" => "test reply",
58 "in_reply_to_status_id" => activity.id
61 refute Notification.create_notification(reply_activity, subscriber)
65 describe "create_notification" do
67 GenServer.start(Streamer, %{}, name: Streamer)
70 if pid = Process.whereis(Streamer) do
71 Process.exit(pid, :kill)
76 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
78 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
79 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
80 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
84 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
87 activity = insert(:note_activity)
89 notify = Notification.create_notification(activity, user)
90 assert notify.user_id == user.id
92 Task.await(task_user_notification)
95 test "it creates a notification for user if the user blocks the activity author" do
96 activity = insert(:note_activity)
97 author = User.get_cached_by_ap_id(activity.data["actor"])
99 {:ok, user} = User.block(user, author)
101 assert Notification.create_notification(activity, user)
104 test "it creates a notificatin for the user if the user mutes the activity author" do
105 muter = insert(:user)
106 muted = insert(:user)
107 {:ok, _} = User.mute(muter, muted)
108 muter = Repo.get(User, muter.id)
109 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
111 assert Notification.create_notification(activity, muter)
114 test "notification created if user is muted without notifications" do
115 muter = insert(:user)
116 muted = insert(:user)
118 {:ok, muter} = User.mute(muter, muted, false)
120 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
122 assert Notification.create_notification(activity, muter)
125 test "it creates a notification for an activity from a muted thread" do
126 muter = insert(:user)
127 other_user = insert(:user)
128 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
129 CommonAPI.add_mute(muter, activity)
132 CommonAPI.post(other_user, %{
133 "status" => "Hi @#{muter.nickname}",
134 "in_reply_to_status_id" => activity.id
137 assert Notification.create_notification(activity, muter)
140 test "it disables notifications from followers" do
141 follower = insert(:user)
142 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
143 User.follow(follower, followed)
144 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
145 refute Notification.create_notification(activity, followed)
148 test "it disables notifications from non-followers" do
149 follower = insert(:user)
150 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
151 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
152 refute Notification.create_notification(activity, followed)
155 test "it disables notifications from people the user follows" do
156 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
157 followed = insert(:user)
158 User.follow(follower, followed)
159 follower = Repo.get(User, follower.id)
160 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
161 refute Notification.create_notification(activity, follower)
164 test "it disables notifications from people the user does not follow" do
165 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
166 followed = insert(:user)
167 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
168 refute Notification.create_notification(activity, follower)
171 test "it doesn't create a notification for user if he is the activity author" do
172 activity = insert(:note_activity)
173 author = User.get_cached_by_ap_id(activity.data["actor"])
175 refute Notification.create_notification(activity, author)
178 test "it doesn't create a notification for follow-unfollow-follow chains" do
180 followed_user = insert(:user)
181 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
182 Notification.create_notification(activity, followed_user)
183 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
184 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
185 refute Notification.create_notification(activity_dupe, followed_user)
188 test "it doesn't create a notification for like-unlike-like chains" do
190 liked_user = insert(:user)
191 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
192 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
193 Notification.create_notification(fav_status, liked_user)
194 TwitterAPI.unfav(user, status.id)
195 {:ok, dupe} = TwitterAPI.fav(user, status.id)
196 refute Notification.create_notification(dupe, liked_user)
199 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
201 retweeted_user = insert(:user)
204 TwitterAPI.create_status(retweeted_user, %{
205 "status" => "Send dupe notifications to the shadow realm"
208 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
209 Notification.create_notification(retweeted_activity, retweeted_user)
210 TwitterAPI.unrepeat(user, status.id)
211 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
212 refute Notification.create_notification(dupe, retweeted_user)
215 test "it doesn't create duplicate notifications for follow+subscribed users" do
217 subscriber = insert(:user)
219 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
220 User.subscribe(subscriber, user)
221 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
222 {:ok, [_notif]} = Notification.create_notifications(status)
225 test "it doesn't create subscription notifications if the recipient cannot see the status" do
227 subscriber = insert(:user)
229 User.subscribe(subscriber, user)
232 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
234 assert {:ok, []} == Notification.create_notifications(status)
238 describe "get notification" do
239 test "it gets a notification that belongs to the user" do
241 other_user = insert(:user)
244 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
246 {:ok, [notification]} = Notification.create_notifications(activity)
247 {:ok, notification} = Notification.get(other_user, notification.id)
249 assert notification.user_id == other_user.id
252 test "it returns error if the notification doesn't belong to the user" do
254 other_user = insert(:user)
257 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
259 {:ok, [notification]} = Notification.create_notifications(activity)
260 {:error, _notification} = Notification.get(user, notification.id)
264 describe "dismiss notification" do
265 test "it dismisses a notification that belongs to the user" do
267 other_user = insert(:user)
270 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
272 {:ok, [notification]} = Notification.create_notifications(activity)
273 {:ok, notification} = Notification.dismiss(other_user, notification.id)
275 assert notification.user_id == other_user.id
278 test "it returns error if the notification doesn't belong to the user" do
280 other_user = insert(:user)
283 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
285 {:ok, [notification]} = Notification.create_notifications(activity)
286 {:error, _notification} = Notification.dismiss(user, notification.id)
290 describe "clear notification" do
291 test "it clears all notifications belonging to the user" do
293 other_user = insert(:user)
294 third_user = insert(:user)
297 TwitterAPI.create_status(user, %{
298 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
301 {:ok, _notifs} = Notification.create_notifications(activity)
304 TwitterAPI.create_status(user, %{
305 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
308 {:ok, _notifs} = Notification.create_notifications(activity)
309 Notification.clear(other_user)
311 assert Notification.for_user(other_user) == []
312 assert Notification.for_user(third_user) != []
316 describe "set_read_up_to()" do
317 test "it sets all notifications as read up to a specified notification ID" do
319 other_user = insert(:user)
322 TwitterAPI.create_status(user, %{
323 "status" => "hey @#{other_user.nickname}!"
327 TwitterAPI.create_status(user, %{
328 "status" => "hey again @#{other_user.nickname}!"
331 [n2, n1] = notifs = Notification.for_user(other_user)
332 assert length(notifs) == 2
337 TwitterAPI.create_status(user, %{
338 "status" => "hey yet again @#{other_user.nickname}!"
341 Notification.set_read_up_to(other_user, n2.id)
343 [n3, n2, n1] = Notification.for_user(other_user)
345 assert n1.seen == true
346 assert n2.seen == true
347 assert n3.seen == false
351 describe "notification target determination" do
352 test "it sends notifications to addressed users in new messages" do
354 other_user = insert(:user)
357 CommonAPI.post(user, %{
358 "status" => "hey @#{other_user.nickname}!"
361 assert other_user in Notification.get_notified_from_activity(activity)
364 test "it sends notifications to mentioned users in new messages" do
366 other_user = insert(:user)
369 "@context" => "https://www.w3.org/ns/activitystreams",
371 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
372 "actor" => user.ap_id,
375 "content" => "message with a Mention tag, but no explicit tagging",
379 "href" => other_user.ap_id,
380 "name" => other_user.nickname
383 "attributedTo" => user.ap_id
387 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
389 assert other_user in Notification.get_notified_from_activity(activity)
392 test "it does not send notifications to users who are only cc in new messages" do
394 other_user = insert(:user)
397 "@context" => "https://www.w3.org/ns/activitystreams",
399 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
400 "cc" => [other_user.ap_id],
401 "actor" => user.ap_id,
404 "content" => "hi everyone",
405 "attributedTo" => user.ap_id
409 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
411 assert other_user not in Notification.get_notified_from_activity(activity)
414 test "it does not send notification to mentioned users in likes" do
416 other_user = insert(:user)
417 third_user = insert(:user)
419 {:ok, activity_one} =
420 CommonAPI.post(user, %{
421 "status" => "hey @#{other_user.nickname}!"
424 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
426 assert other_user not in Notification.get_notified_from_activity(activity_two)
429 test "it does not send notification to mentioned users in announces" do
431 other_user = insert(:user)
432 third_user = insert(:user)
434 {:ok, activity_one} =
435 CommonAPI.post(user, %{
436 "status" => "hey @#{other_user.nickname}!"
439 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
441 assert other_user not in Notification.get_notified_from_activity(activity_two)
445 describe "notification lifecycle" do
446 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
448 other_user = insert(:user)
450 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
452 assert Enum.empty?(Notification.for_user(user))
454 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
456 assert length(Notification.for_user(user)) == 1
458 {:ok, _} = CommonAPI.delete(activity.id, user)
460 assert Enum.empty?(Notification.for_user(user))
463 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
465 other_user = insert(:user)
467 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
469 assert Enum.empty?(Notification.for_user(user))
471 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
473 assert length(Notification.for_user(user)) == 1
475 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
477 assert Enum.empty?(Notification.for_user(user))
480 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
482 other_user = insert(:user)
484 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
486 assert Enum.empty?(Notification.for_user(user))
488 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
490 assert length(Notification.for_user(user)) == 1
492 {:ok, _} = CommonAPI.delete(activity.id, user)
494 assert Enum.empty?(Notification.for_user(user))
497 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" 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.repeat(activity.id, other_user)
507 assert length(Notification.for_user(user)) == 1
509 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
511 assert Enum.empty?(Notification.for_user(user))
514 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
524 assert Enum.empty?(Notification.for_user(user))
526 {:error, _} = CommonAPI.favorite(activity.id, other_user)
528 assert Enum.empty?(Notification.for_user(user))
531 test "repeating an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
541 assert Enum.empty?(Notification.for_user(user))
543 {:error, _} = CommonAPI.repeat(activity.id, other_user)
545 assert Enum.empty?(Notification.for_user(user))
548 test "replying to a deleted post without tagging does not generate a notification" do
550 other_user = insert(:user)
552 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
553 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
555 {:ok, _reply_activity} =
556 CommonAPI.post(other_user, %{
557 "status" => "test reply",
558 "in_reply_to_status_id" => activity.id
561 assert Enum.empty?(Notification.for_user(user))
565 describe "for_user" do
566 test "it returns notifications for muted user without notifications" do
568 muted = insert(:user)
569 {:ok, user} = User.mute(user, muted, false)
571 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
573 assert length(Notification.for_user(user)) == 1
576 test "it doesn't return notifications for muted user with notifications" do
578 muted = insert(:user)
579 {:ok, user} = User.mute(user, muted)
581 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
583 assert Notification.for_user(user) == []
586 test "it doesn't return notifications for blocked user" do
588 blocked = insert(:user)
589 {:ok, user} = User.block(user, blocked)
591 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
593 assert Notification.for_user(user) == []
596 test "it doesn't return notificatitons for blocked domain" do
598 blocked = insert(:user, ap_id: "http://some-domain.com")
599 {:ok, user} = User.block_domain(user, "some-domain.com")
601 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
603 assert Notification.for_user(user) == []
606 test "it doesn't return notifications for muted thread" do
608 another_user = insert(:user)
611 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
613 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
614 assert Notification.for_user(user) == []
617 test "it returns notifications for muted user with notifications and with_muted parameter" do
619 muted = insert(:user)
620 {:ok, user} = User.mute(user, muted)
622 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
624 assert length(Notification.for_user(user, %{with_muted: true})) == 1
627 test "it returns notifications for blocked user and with_muted parameter" do
629 blocked = insert(:user)
630 {:ok, user} = User.block(user, blocked)
632 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
634 assert length(Notification.for_user(user, %{with_muted: true})) == 1
637 test "it returns notificatitons for blocked domain and with_muted parameter" do
639 blocked = insert(:user, ap_id: "http://some-domain.com")
640 {:ok, user} = User.block_domain(user, "some-domain.com")
642 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
644 assert length(Notification.for_user(user, %{with_muted: true})) == 1
647 test "it returns notifications for muted thread with_muted parameter" do
649 another_user = insert(:user)
652 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
654 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
655 assert length(Notification.for_user(user, %{with_muted: true})) == 1