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 user_notifications = Notification.for_user(user)
62 assert length(user_notifications) == 1
64 subscriber_notifications = Notification.for_user(subscriber)
65 assert Enum.empty?(subscriber_notifications)
69 describe "create_notification" do
71 GenServer.start(Streamer, %{}, name: Streamer)
74 if pid = Process.whereis(Streamer) do
75 Process.exit(pid, :kill)
80 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
82 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
83 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
84 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
88 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
91 activity = insert(:note_activity)
93 notify = Notification.create_notification(activity, user)
94 assert notify.user_id == user.id
96 Task.await(task_user_notification)
99 test "it creates a notification for user if the user blocks the activity author" do
100 activity = insert(:note_activity)
101 author = User.get_cached_by_ap_id(activity.data["actor"])
103 {:ok, user} = User.block(user, author)
105 assert Notification.create_notification(activity, user)
108 test "it creates a notificatin for the user if the user mutes the activity author" do
109 muter = insert(:user)
110 muted = insert(:user)
111 {:ok, _} = User.mute(muter, muted)
112 muter = Repo.get(User, muter.id)
113 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
115 assert Notification.create_notification(activity, muter)
118 test "notification created if user is muted without notifications" do
119 muter = insert(:user)
120 muted = insert(:user)
122 {:ok, muter} = User.mute(muter, muted, false)
124 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
126 assert Notification.create_notification(activity, muter)
129 test "it creates a notification for an activity from a muted thread" do
130 muter = insert(:user)
131 other_user = insert(:user)
132 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
133 CommonAPI.add_mute(muter, activity)
136 CommonAPI.post(other_user, %{
137 "status" => "Hi @#{muter.nickname}",
138 "in_reply_to_status_id" => activity.id
141 assert Notification.create_notification(activity, muter)
144 test "it disables notifications from followers" do
145 follower = insert(:user)
146 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
147 User.follow(follower, followed)
148 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
149 refute Notification.create_notification(activity, followed)
152 test "it disables notifications from non-followers" do
153 follower = insert(:user)
154 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
155 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
156 refute Notification.create_notification(activity, followed)
159 test "it disables notifications from people the user follows" do
160 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
161 followed = insert(:user)
162 User.follow(follower, followed)
163 follower = Repo.get(User, follower.id)
164 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
165 refute Notification.create_notification(activity, follower)
168 test "it disables notifications from people the user does not follow" do
169 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
170 followed = insert(:user)
171 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
172 refute Notification.create_notification(activity, follower)
175 test "it doesn't create a notification for user if he is the activity author" do
176 activity = insert(:note_activity)
177 author = User.get_cached_by_ap_id(activity.data["actor"])
179 refute Notification.create_notification(activity, author)
182 test "it doesn't create a notification for follow-unfollow-follow chains" do
184 followed_user = insert(:user)
185 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
186 Notification.create_notification(activity, followed_user)
187 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
188 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
189 refute Notification.create_notification(activity_dupe, followed_user)
192 test "it doesn't create a notification for like-unlike-like chains" do
194 liked_user = insert(:user)
195 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
196 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
197 Notification.create_notification(fav_status, liked_user)
198 TwitterAPI.unfav(user, status.id)
199 {:ok, dupe} = TwitterAPI.fav(user, status.id)
200 refute Notification.create_notification(dupe, liked_user)
203 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
205 retweeted_user = insert(:user)
208 TwitterAPI.create_status(retweeted_user, %{
209 "status" => "Send dupe notifications to the shadow realm"
212 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
213 Notification.create_notification(retweeted_activity, retweeted_user)
214 TwitterAPI.unrepeat(user, status.id)
215 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
216 refute Notification.create_notification(dupe, retweeted_user)
219 test "it doesn't create duplicate notifications for follow+subscribed users" do
221 subscriber = insert(:user)
223 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
224 User.subscribe(subscriber, user)
225 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
226 {:ok, [_notif]} = Notification.create_notifications(status)
229 test "it doesn't create subscription notifications if the recipient cannot see the status" do
231 subscriber = insert(:user)
233 User.subscribe(subscriber, user)
236 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
238 assert {:ok, []} == Notification.create_notifications(status)
242 describe "get notification" do
243 test "it gets a notification that belongs to the user" do
245 other_user = insert(:user)
248 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
250 {:ok, [notification]} = Notification.create_notifications(activity)
251 {:ok, notification} = Notification.get(other_user, notification.id)
253 assert notification.user_id == other_user.id
256 test "it returns error if the notification doesn't belong to the user" do
258 other_user = insert(:user)
261 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
263 {:ok, [notification]} = Notification.create_notifications(activity)
264 {:error, _notification} = Notification.get(user, notification.id)
268 describe "dismiss notification" do
269 test "it dismisses a notification that belongs to the user" do
271 other_user = insert(:user)
274 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
276 {:ok, [notification]} = Notification.create_notifications(activity)
277 {:ok, notification} = Notification.dismiss(other_user, notification.id)
279 assert notification.user_id == other_user.id
282 test "it returns error if the notification doesn't belong to the user" do
284 other_user = insert(:user)
287 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
289 {:ok, [notification]} = Notification.create_notifications(activity)
290 {:error, _notification} = Notification.dismiss(user, notification.id)
294 describe "clear notification" do
295 test "it clears all notifications belonging to the user" do
297 other_user = insert(:user)
298 third_user = insert(:user)
301 TwitterAPI.create_status(user, %{
302 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
305 {:ok, _notifs} = Notification.create_notifications(activity)
308 TwitterAPI.create_status(user, %{
309 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
312 {:ok, _notifs} = Notification.create_notifications(activity)
313 Notification.clear(other_user)
315 assert Notification.for_user(other_user) == []
316 assert Notification.for_user(third_user) != []
320 describe "set_read_up_to()" do
321 test "it sets all notifications as read up to a specified notification ID" do
323 other_user = insert(:user)
326 TwitterAPI.create_status(user, %{
327 "status" => "hey @#{other_user.nickname}!"
331 TwitterAPI.create_status(user, %{
332 "status" => "hey again @#{other_user.nickname}!"
335 [n2, n1] = notifs = Notification.for_user(other_user)
336 assert length(notifs) == 2
341 TwitterAPI.create_status(user, %{
342 "status" => "hey yet again @#{other_user.nickname}!"
345 Notification.set_read_up_to(other_user, n2.id)
347 [n3, n2, n1] = Notification.for_user(other_user)
349 assert n1.seen == true
350 assert n2.seen == true
351 assert n3.seen == false
355 describe "notification target determination" do
356 test "it sends notifications to addressed users in new messages" do
358 other_user = insert(:user)
361 CommonAPI.post(user, %{
362 "status" => "hey @#{other_user.nickname}!"
365 assert other_user in Notification.get_notified_from_activity(activity)
368 test "it sends notifications to mentioned users in new messages" do
370 other_user = insert(:user)
373 "@context" => "https://www.w3.org/ns/activitystreams",
375 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
376 "actor" => user.ap_id,
379 "content" => "message with a Mention tag, but no explicit tagging",
383 "href" => other_user.ap_id,
384 "name" => other_user.nickname
387 "attributedTo" => user.ap_id
391 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
393 assert other_user in Notification.get_notified_from_activity(activity)
396 test "it does not send notifications to users who are only cc in new messages" do
398 other_user = insert(:user)
401 "@context" => "https://www.w3.org/ns/activitystreams",
403 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
404 "cc" => [other_user.ap_id],
405 "actor" => user.ap_id,
408 "content" => "hi everyone",
409 "attributedTo" => user.ap_id
413 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
415 assert other_user not in Notification.get_notified_from_activity(activity)
418 test "it does not send notification to mentioned users in likes" do
420 other_user = insert(:user)
421 third_user = insert(:user)
423 {:ok, activity_one} =
424 CommonAPI.post(user, %{
425 "status" => "hey @#{other_user.nickname}!"
428 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
430 assert other_user not in Notification.get_notified_from_activity(activity_two)
433 test "it does not send notification to mentioned users in announces" 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.repeat(activity_one.id, third_user)
445 assert other_user not in Notification.get_notified_from_activity(activity_two)
449 describe "notification lifecycle" do
450 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
452 other_user = insert(:user)
454 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
456 assert Enum.empty?(Notification.for_user(user))
458 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
460 assert length(Notification.for_user(user)) == 1
462 {:ok, _} = CommonAPI.delete(activity.id, user)
464 assert Enum.empty?(Notification.for_user(user))
467 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
469 other_user = insert(:user)
471 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
473 assert Enum.empty?(Notification.for_user(user))
475 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
477 assert length(Notification.for_user(user)) == 1
479 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
481 assert Enum.empty?(Notification.for_user(user))
484 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
486 other_user = insert(:user)
488 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
490 assert Enum.empty?(Notification.for_user(user))
492 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
494 assert length(Notification.for_user(user)) == 1
496 {:ok, _} = CommonAPI.delete(activity.id, user)
498 assert Enum.empty?(Notification.for_user(user))
501 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
503 other_user = insert(:user)
505 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
507 assert Enum.empty?(Notification.for_user(user))
509 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
511 assert length(Notification.for_user(user)) == 1
513 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
515 assert Enum.empty?(Notification.for_user(user))
518 test "liking an activity which is already deleted does not generate a notification" do
520 other_user = insert(:user)
522 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
524 assert Enum.empty?(Notification.for_user(user))
526 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
528 assert Enum.empty?(Notification.for_user(user))
530 {:error, _} = CommonAPI.favorite(activity.id, other_user)
532 assert Enum.empty?(Notification.for_user(user))
535 test "repeating an activity which is already deleted does not generate a notification" do
537 other_user = insert(:user)
539 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
541 assert Enum.empty?(Notification.for_user(user))
543 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
545 assert Enum.empty?(Notification.for_user(user))
547 {:error, _} = CommonAPI.repeat(activity.id, other_user)
549 assert Enum.empty?(Notification.for_user(user))
552 test "replying to a deleted post without tagging does not generate a notification" do
554 other_user = insert(:user)
556 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
557 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
559 {:ok, _reply_activity} =
560 CommonAPI.post(other_user, %{
561 "status" => "test reply",
562 "in_reply_to_status_id" => activity.id
565 assert Enum.empty?(Notification.for_user(user))
569 describe "for_user" do
570 test "it returns notifications for muted user without notifications" do
572 muted = insert(:user)
573 {:ok, user} = User.mute(user, muted, false)
575 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
577 assert length(Notification.for_user(user)) == 1
580 test "it doesn't return notifications for muted user with notifications" do
582 muted = insert(:user)
583 {:ok, user} = User.mute(user, muted)
585 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
587 assert Notification.for_user(user) == []
590 test "it doesn't return notifications for blocked user" do
592 blocked = insert(:user)
593 {:ok, user} = User.block(user, blocked)
595 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
597 assert Notification.for_user(user) == []
600 test "it doesn't return notificatitons for blocked domain" do
602 blocked = insert(:user, ap_id: "http://some-domain.com")
603 {:ok, user} = User.block_domain(user, "some-domain.com")
605 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
607 assert Notification.for_user(user) == []
610 test "it doesn't return notifications for muted thread" do
612 another_user = insert(:user)
615 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
617 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
618 assert Notification.for_user(user) == []
621 test "it returns notifications for muted user with notifications and with_muted parameter" do
623 muted = insert(:user)
624 {:ok, user} = User.mute(user, muted)
626 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
628 assert length(Notification.for_user(user, %{with_muted: true})) == 1
631 test "it returns notifications for blocked user and with_muted parameter" do
633 blocked = insert(:user)
634 {:ok, user} = User.block(user, blocked)
636 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
638 assert length(Notification.for_user(user, %{with_muted: true})) == 1
641 test "it returns notificatitons for blocked domain and with_muted parameter" do
643 blocked = insert(:user, ap_id: "http://some-domain.com")
644 {:ok, user} = User.block_domain(user, "some-domain.com")
646 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
648 assert length(Notification.for_user(user, %{with_muted: true})) == 1
651 test "it returns notifications for muted thread with_muted parameter" do
653 another_user = insert(:user)
656 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
658 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
659 assert length(Notification.for_user(user, %{with_muted: true})) == 1