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
47 describe "create_notification" do
49 GenServer.start(Streamer, %{}, name: Streamer)
52 if pid = Process.whereis(Streamer) do
53 Process.exit(pid, :kill)
58 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
60 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
61 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
62 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
66 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
69 activity = insert(:note_activity)
71 notify = Notification.create_notification(activity, user)
72 assert notify.user_id == user.id
74 Task.await(task_user_notification)
77 test "it creates a notification for user if the user blocks the activity author" do
78 activity = insert(:note_activity)
79 author = User.get_cached_by_ap_id(activity.data["actor"])
81 {:ok, user} = User.block(user, author)
83 assert Notification.create_notification(activity, user)
86 test "it creates a notificatin for the user if the user mutes the activity author" do
89 {:ok, _} = User.mute(muter, muted)
90 muter = Repo.get(User, muter.id)
91 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
93 assert Notification.create_notification(activity, muter)
96 test "notification created if user is muted without notifications" do
100 {:ok, muter} = User.mute(muter, muted, false)
102 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
104 assert Notification.create_notification(activity, muter)
107 test "it creates a notification for an activity from a muted thread" do
108 muter = insert(:user)
109 other_user = insert(:user)
110 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
111 CommonAPI.add_mute(muter, activity)
114 CommonAPI.post(other_user, %{
115 "status" => "Hi @#{muter.nickname}",
116 "in_reply_to_status_id" => activity.id
119 assert Notification.create_notification(activity, muter)
122 test "it disables notifications from followers" do
123 follower = insert(:user)
124 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
125 User.follow(follower, followed)
126 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
127 refute Notification.create_notification(activity, followed)
130 test "it disables notifications from non-followers" do
131 follower = insert(:user)
132 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
133 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
134 refute Notification.create_notification(activity, followed)
137 test "it disables notifications from people the user follows" do
138 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
139 followed = insert(:user)
140 User.follow(follower, followed)
141 follower = Repo.get(User, follower.id)
142 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
143 refute Notification.create_notification(activity, follower)
146 test "it disables notifications from people the user does not follow" do
147 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
148 followed = insert(:user)
149 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
150 refute Notification.create_notification(activity, follower)
153 test "it doesn't create a notification for user if he is the activity author" do
154 activity = insert(:note_activity)
155 author = User.get_cached_by_ap_id(activity.data["actor"])
157 refute Notification.create_notification(activity, author)
160 test "it doesn't create a notification for follow-unfollow-follow chains" do
162 followed_user = insert(:user)
163 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
164 Notification.create_notification(activity, followed_user)
165 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
166 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
167 refute Notification.create_notification(activity_dupe, followed_user)
170 test "it doesn't create a notification for like-unlike-like chains" do
172 liked_user = insert(:user)
173 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
174 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
175 Notification.create_notification(fav_status, liked_user)
176 TwitterAPI.unfav(user, status.id)
177 {:ok, dupe} = TwitterAPI.fav(user, status.id)
178 refute Notification.create_notification(dupe, liked_user)
181 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
183 retweeted_user = insert(:user)
186 TwitterAPI.create_status(retweeted_user, %{
187 "status" => "Send dupe notifications to the shadow realm"
190 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
191 Notification.create_notification(retweeted_activity, retweeted_user)
192 TwitterAPI.unrepeat(user, status.id)
193 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
194 refute Notification.create_notification(dupe, retweeted_user)
197 test "it doesn't create duplicate notifications for follow+subscribed users" do
199 subscriber = insert(:user)
201 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
202 User.subscribe(subscriber, user)
203 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
204 {:ok, [_notif]} = Notification.create_notifications(status)
207 test "it doesn't create subscription notifications if the recipient cannot see the status" do
209 subscriber = insert(:user)
211 User.subscribe(subscriber, user)
214 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
216 assert {:ok, []} == Notification.create_notifications(status)
220 describe "get notification" do
221 test "it gets a notification that belongs to the user" do
223 other_user = insert(:user)
226 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
228 {:ok, [notification]} = Notification.create_notifications(activity)
229 {:ok, notification} = Notification.get(other_user, notification.id)
231 assert notification.user_id == other_user.id
234 test "it returns error if the notification doesn't belong to the user" do
236 other_user = insert(:user)
239 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
241 {:ok, [notification]} = Notification.create_notifications(activity)
242 {:error, _notification} = Notification.get(user, notification.id)
246 describe "dismiss notification" do
247 test "it dismisses a notification that belongs to the user" do
249 other_user = insert(:user)
252 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
254 {:ok, [notification]} = Notification.create_notifications(activity)
255 {:ok, notification} = Notification.dismiss(other_user, notification.id)
257 assert notification.user_id == other_user.id
260 test "it returns error if the notification doesn't belong to the user" do
262 other_user = insert(:user)
265 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
267 {:ok, [notification]} = Notification.create_notifications(activity)
268 {:error, _notification} = Notification.dismiss(user, notification.id)
272 describe "clear notification" do
273 test "it clears all notifications belonging to the user" do
275 other_user = insert(:user)
276 third_user = insert(:user)
279 TwitterAPI.create_status(user, %{
280 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
283 {:ok, _notifs} = Notification.create_notifications(activity)
286 TwitterAPI.create_status(user, %{
287 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
290 {:ok, _notifs} = Notification.create_notifications(activity)
291 Notification.clear(other_user)
293 assert Notification.for_user(other_user) == []
294 assert Notification.for_user(third_user) != []
298 describe "set_read_up_to()" do
299 test "it sets all notifications as read up to a specified notification ID" do
301 other_user = insert(:user)
304 TwitterAPI.create_status(user, %{
305 "status" => "hey @#{other_user.nickname}!"
309 TwitterAPI.create_status(user, %{
310 "status" => "hey again @#{other_user.nickname}!"
313 [n2, n1] = notifs = Notification.for_user(other_user)
314 assert length(notifs) == 2
319 TwitterAPI.create_status(user, %{
320 "status" => "hey yet again @#{other_user.nickname}!"
323 Notification.set_read_up_to(other_user, n2.id)
325 [n3, n2, n1] = Notification.for_user(other_user)
327 assert n1.seen == true
328 assert n2.seen == true
329 assert n3.seen == false
333 describe "notification target determination" do
334 test "it sends notifications to addressed users in new messages" do
336 other_user = insert(:user)
339 CommonAPI.post(user, %{
340 "status" => "hey @#{other_user.nickname}!"
343 assert other_user in Notification.get_notified_from_activity(activity)
346 test "it sends notifications to mentioned users in new messages" do
348 other_user = insert(:user)
351 "@context" => "https://www.w3.org/ns/activitystreams",
353 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
354 "actor" => user.ap_id,
357 "content" => "message with a Mention tag, but no explicit tagging",
361 "href" => other_user.ap_id,
362 "name" => other_user.nickname
365 "attributedTo" => user.ap_id
369 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
371 assert other_user in Notification.get_notified_from_activity(activity)
374 test "it does not send notifications to users who are only cc in new messages" do
376 other_user = insert(:user)
379 "@context" => "https://www.w3.org/ns/activitystreams",
381 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
382 "cc" => [other_user.ap_id],
383 "actor" => user.ap_id,
386 "content" => "hi everyone",
387 "attributedTo" => user.ap_id
391 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
393 assert other_user not in Notification.get_notified_from_activity(activity)
396 test "it does not send notification to mentioned users in likes" do
398 other_user = insert(:user)
399 third_user = insert(:user)
401 {:ok, activity_one} =
402 CommonAPI.post(user, %{
403 "status" => "hey @#{other_user.nickname}!"
406 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
408 assert other_user not in Notification.get_notified_from_activity(activity_two)
411 test "it does not send notification to mentioned users in announces" do
413 other_user = insert(:user)
414 third_user = insert(:user)
416 {:ok, activity_one} =
417 CommonAPI.post(user, %{
418 "status" => "hey @#{other_user.nickname}!"
421 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
423 assert other_user not in Notification.get_notified_from_activity(activity_two)
427 describe "notification lifecycle" do
428 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
430 other_user = insert(:user)
432 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
434 assert Enum.empty?(Notification.for_user(user))
436 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
438 assert length(Notification.for_user(user)) == 1
440 {:ok, _} = CommonAPI.delete(activity.id, user)
442 assert Enum.empty?(Notification.for_user(user))
445 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
447 other_user = insert(:user)
449 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
451 assert Enum.empty?(Notification.for_user(user))
453 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
455 assert length(Notification.for_user(user)) == 1
457 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
459 assert Enum.empty?(Notification.for_user(user))
462 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
464 other_user = insert(:user)
466 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
468 assert Enum.empty?(Notification.for_user(user))
470 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
472 assert length(Notification.for_user(user)) == 1
474 {:ok, _} = CommonAPI.delete(activity.id, user)
476 assert Enum.empty?(Notification.for_user(user))
479 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
481 other_user = insert(:user)
483 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
485 assert Enum.empty?(Notification.for_user(user))
487 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
489 assert length(Notification.for_user(user)) == 1
491 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
493 assert Enum.empty?(Notification.for_user(user))
496 test "liking an activity which is already deleted does not generate a notification" do
498 other_user = insert(:user)
500 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
502 assert Enum.empty?(Notification.for_user(user))
504 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
506 assert Enum.empty?(Notification.for_user(user))
508 {:error, _} = CommonAPI.favorite(activity.id, other_user)
510 assert Enum.empty?(Notification.for_user(user))
513 test "repeating an activity which is already deleted does not generate a notification" do
515 other_user = insert(:user)
517 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
519 assert Enum.empty?(Notification.for_user(user))
521 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
523 assert Enum.empty?(Notification.for_user(user))
525 {:error, _} = CommonAPI.repeat(activity.id, other_user)
527 assert Enum.empty?(Notification.for_user(user))
530 test "replying to a deleted post without tagging does not generate a notification" do
532 other_user = insert(:user)
534 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
535 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
537 {:ok, _reply_activity} =
538 CommonAPI.post(other_user, %{
539 "status" => "test reply",
540 "in_reply_to_status_id" => activity.id
543 assert Enum.empty?(Notification.for_user(user))
547 describe "for_user" do
548 test "it returns notifications for muted user without notifications" do
550 muted = insert(:user)
551 {:ok, user} = User.mute(user, muted, false)
553 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
555 assert length(Notification.for_user(user)) == 1
558 test "it doesn't return notifications for muted user with notifications" do
560 muted = insert(:user)
561 {:ok, user} = User.mute(user, muted)
563 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
565 assert Notification.for_user(user) == []
568 test "it doesn't return notifications for blocked user" do
570 blocked = insert(:user)
571 {:ok, user} = User.block(user, blocked)
573 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
575 assert Notification.for_user(user) == []
578 test "it doesn't return notificatitons for blocked domain" do
580 blocked = insert(:user, ap_id: "http://some-domain.com")
581 {:ok, user} = User.block_domain(user, "some-domain.com")
583 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
585 assert Notification.for_user(user) == []
588 test "it doesn't return notifications for muted thread" do
590 another_user = insert(:user)
593 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
595 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
596 assert Notification.for_user(user) == []
599 test "it returns notifications for muted user with notifications and with_muted parameter" do
601 muted = insert(:user)
602 {:ok, user} = User.mute(user, muted)
604 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
606 assert length(Notification.for_user(user, %{with_muted: true})) == 1
609 test "it returns notifications for blocked user and with_muted parameter" do
611 blocked = insert(:user)
612 {:ok, user} = User.block(user, blocked)
614 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
616 assert length(Notification.for_user(user, %{with_muted: true})) == 1
619 test "it returns notificatitons for blocked domain and with_muted parameter" do
621 blocked = insert(:user, ap_id: "http://some-domain.com")
622 {:ok, user} = User.block_domain(user, "some-domain.com")
624 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
626 assert length(Notification.for_user(user, %{with_muted: true})) == 1
629 test "it returns notifications for muted thread with_muted parameter" do
631 another_user = insert(:user)
634 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
636 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
637 assert length(Notification.for_user(user, %{with_muted: true})) == 1