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 doesn't create 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 refute Notification.create_notification(activity, user)
86 test "it doesn't create 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 refute Notification.create_notification(activity, muter)
96 test "it doesn't create a notification for an activity from a muted thread" do
98 other_user = insert(:user)
99 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
100 CommonAPI.add_mute(muter, activity)
103 CommonAPI.post(other_user, %{
104 "status" => "Hi @#{muter.nickname}",
105 "in_reply_to_status_id" => activity.id
108 refute Notification.create_notification(activity, muter)
111 test "it disables notifications from followers" do
112 follower = insert(:user)
113 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
114 User.follow(follower, followed)
115 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
116 refute Notification.create_notification(activity, followed)
119 test "it disables notifications from non-followers" do
120 follower = insert(:user)
121 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
122 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
123 refute Notification.create_notification(activity, followed)
126 test "it disables notifications from people the user follows" do
127 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
128 followed = insert(:user)
129 User.follow(follower, followed)
130 follower = Repo.get(User, follower.id)
131 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
132 refute Notification.create_notification(activity, follower)
135 test "it disables notifications from people the user does not follow" do
136 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
137 followed = insert(:user)
138 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
139 refute Notification.create_notification(activity, follower)
142 test "it doesn't create a notification for user if he is the activity author" do
143 activity = insert(:note_activity)
144 author = User.get_cached_by_ap_id(activity.data["actor"])
146 refute Notification.create_notification(activity, author)
149 test "it doesn't create a notification for follow-unfollow-follow chains" do
151 followed_user = insert(:user)
152 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
153 Notification.create_notification(activity, followed_user)
154 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
155 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
156 refute Notification.create_notification(activity_dupe, followed_user)
159 test "it doesn't create a notification for like-unlike-like chains" do
161 liked_user = insert(:user)
162 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
163 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
164 Notification.create_notification(fav_status, liked_user)
165 TwitterAPI.unfav(user, status.id)
166 {:ok, dupe} = TwitterAPI.fav(user, status.id)
167 refute Notification.create_notification(dupe, liked_user)
170 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
172 retweeted_user = insert(:user)
175 TwitterAPI.create_status(retweeted_user, %{
176 "status" => "Send dupe notifications to the shadow realm"
179 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
180 Notification.create_notification(retweeted_activity, retweeted_user)
181 TwitterAPI.unrepeat(user, status.id)
182 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
183 refute Notification.create_notification(dupe, retweeted_user)
186 test "it doesn't create duplicate notifications for follow+subscribed users" do
188 subscriber = insert(:user)
190 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
191 User.subscribe(subscriber, user)
192 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
193 {:ok, [_notif]} = Notification.create_notifications(status)
196 test "it doesn't create subscription notifications if the recipient cannot see the status" do
198 subscriber = insert(:user)
200 User.subscribe(subscriber, user)
203 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
205 assert {:ok, []} == Notification.create_notifications(status)
209 describe "get notification" do
210 test "it gets a notification that belongs to the user" do
212 other_user = insert(:user)
215 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
217 {:ok, [notification]} = Notification.create_notifications(activity)
218 {:ok, notification} = Notification.get(other_user, notification.id)
220 assert notification.user_id == other_user.id
223 test "it returns error if the notification doesn't belong 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 {:error, _notification} = Notification.get(user, notification.id)
235 describe "dismiss notification" do
236 test "it dismisses a notification that belongs 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 {:ok, notification} = Notification.dismiss(other_user, notification.id)
246 assert notification.user_id == other_user.id
249 test "it returns error if the notification doesn't belong 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 {:error, _notification} = Notification.dismiss(user, notification.id)
261 describe "clear notification" do
262 test "it clears all notifications belonging to the user" do
264 other_user = insert(:user)
265 third_user = insert(:user)
268 TwitterAPI.create_status(user, %{
269 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
272 {:ok, _notifs} = Notification.create_notifications(activity)
275 TwitterAPI.create_status(user, %{
276 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
279 {:ok, _notifs} = Notification.create_notifications(activity)
280 Notification.clear(other_user)
282 assert Notification.for_user(other_user) == []
283 assert Notification.for_user(third_user) != []
287 describe "set_read_up_to()" do
288 test "it sets all notifications as read up to a specified notification ID" do
290 other_user = insert(:user)
293 TwitterAPI.create_status(user, %{
294 "status" => "hey @#{other_user.nickname}!"
298 TwitterAPI.create_status(user, %{
299 "status" => "hey again @#{other_user.nickname}!"
302 [n2, n1] = notifs = Notification.for_user(other_user)
303 assert length(notifs) == 2
308 TwitterAPI.create_status(user, %{
309 "status" => "hey yet again @#{other_user.nickname}!"
312 Notification.set_read_up_to(other_user, n2.id)
314 [n3, n2, n1] = Notification.for_user(other_user)
316 assert n1.seen == true
317 assert n2.seen == true
318 assert n3.seen == false
322 describe "notification target determination" do
323 test "it sends notifications to addressed users in new messages" do
325 other_user = insert(:user)
328 CommonAPI.post(user, %{
329 "status" => "hey @#{other_user.nickname}!"
332 assert other_user in Notification.get_notified_from_activity(activity)
335 test "it sends notifications to mentioned users in new messages" do
337 other_user = insert(:user)
340 "@context" => "https://www.w3.org/ns/activitystreams",
342 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
343 "actor" => user.ap_id,
346 "content" => "message with a Mention tag, but no explicit tagging",
350 "href" => other_user.ap_id,
351 "name" => other_user.nickname
354 "attributedTo" => user.ap_id
358 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
360 assert other_user in Notification.get_notified_from_activity(activity)
363 test "it does not send notifications to users who are only cc in new messages" do
365 other_user = insert(:user)
368 "@context" => "https://www.w3.org/ns/activitystreams",
370 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
371 "cc" => [other_user.ap_id],
372 "actor" => user.ap_id,
375 "content" => "hi everyone",
376 "attributedTo" => user.ap_id
380 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
382 assert other_user not in Notification.get_notified_from_activity(activity)
385 test "it does not send notification to mentioned users in likes" do
387 other_user = insert(:user)
388 third_user = insert(:user)
390 {:ok, activity_one} =
391 CommonAPI.post(user, %{
392 "status" => "hey @#{other_user.nickname}!"
395 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
397 assert other_user not in Notification.get_notified_from_activity(activity_two)
400 test "it does not send notification to mentioned users in announces" do
402 other_user = insert(:user)
403 third_user = insert(:user)
405 {:ok, activity_one} =
406 CommonAPI.post(user, %{
407 "status" => "hey @#{other_user.nickname}!"
410 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
412 assert other_user not in Notification.get_notified_from_activity(activity_two)
416 describe "notification lifecycle" do
417 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
419 other_user = insert(:user)
421 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
423 assert Enum.empty?(Notification.for_user(user))
425 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
427 assert length(Notification.for_user(user)) == 1
429 {:ok, _} = CommonAPI.delete(activity.id, user)
431 assert Enum.empty?(Notification.for_user(user))
434 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
436 other_user = insert(:user)
438 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
440 assert Enum.empty?(Notification.for_user(user))
442 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
444 assert length(Notification.for_user(user)) == 1
446 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
448 assert Enum.empty?(Notification.for_user(user))
451 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
453 other_user = insert(:user)
455 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
457 assert Enum.empty?(Notification.for_user(user))
459 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
461 assert length(Notification.for_user(user)) == 1
463 {:ok, _} = CommonAPI.delete(activity.id, user)
465 assert Enum.empty?(Notification.for_user(user))
468 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
470 other_user = insert(:user)
472 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
474 assert Enum.empty?(Notification.for_user(user))
476 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
478 assert length(Notification.for_user(user)) == 1
480 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
482 assert Enum.empty?(Notification.for_user(user))
485 test "liking an activity which is already deleted does not generate a notification" do
487 other_user = insert(:user)
489 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
491 assert Enum.empty?(Notification.for_user(user))
493 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
495 assert Enum.empty?(Notification.for_user(user))
497 {:error, _} = CommonAPI.favorite(activity.id, other_user)
499 assert Enum.empty?(Notification.for_user(user))
502 test "repeating an activity which is already deleted does not generate a notification" do
504 other_user = insert(:user)
506 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
508 assert Enum.empty?(Notification.for_user(user))
510 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
512 assert Enum.empty?(Notification.for_user(user))
514 {:error, _} = CommonAPI.repeat(activity.id, other_user)
516 assert Enum.empty?(Notification.for_user(user))
519 test "replying to a deleted post without tagging does not generate a notification" do
521 other_user = insert(:user)
523 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
524 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
526 {:ok, _reply_activity} =
527 CommonAPI.post(other_user, %{
528 "status" => "test reply",
529 "in_reply_to_status_id" => activity.id
532 assert Enum.empty?(Notification.for_user(user))