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
11 alias Pleroma.Notification
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.CommonAPI
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
50 test "it doesn't create a notification for user if the user blocks the activity author" do
51 activity = insert(:note_activity)
52 author = User.get_by_ap_id(activity.data["actor"])
54 {:ok, user} = User.block(user, author)
56 assert nil == Notification.create_notification(activity, user)
59 test "it doesn't create a notificatin for the user if the user mutes the activity author" do
62 {:ok, _} = User.mute(muter, muted)
63 muter = Repo.get(User, muter.id)
64 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
66 assert nil == Notification.create_notification(activity, muter)
69 test "it doesn't create a notification for an activity from a muted thread" do
71 other_user = insert(:user)
72 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
73 CommonAPI.add_mute(muter, activity)
76 CommonAPI.post(other_user, %{
77 "status" => "Hi @#{muter.nickname}",
78 "in_reply_to_status_id" => activity.id
81 assert nil == Notification.create_notification(activity, muter)
84 test "it disables notifications from people on remote instances" do
85 user = insert(:user, info: %{notification_settings: %{"remote" => false}})
86 other_user = insert(:user)
89 "@context" => "https://www.w3.org/ns/activitystreams",
91 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
92 "actor" => other_user.ap_id,
95 "content" => "Hi @#{user.nickname}",
96 "attributedTo" => other_user.ap_id
100 {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
101 assert nil == Notification.create_notification(activity, user)
104 test "it disables notifications from people on the local instance" do
105 user = insert(:user, info: %{notification_settings: %{"local" => false}})
106 other_user = insert(:user)
107 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
108 assert nil == Notification.create_notification(activity, user)
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 assert nil == Notification.create_notification(activity, followed)
119 test "it disables notifications from people the user follows" do
120 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
121 followed = insert(:user)
122 User.follow(follower, followed)
123 follower = Repo.get(User, follower.id)
124 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
125 assert nil == Notification.create_notification(activity, follower)
128 test "it doesn't create a notification for user if he is the activity author" do
129 activity = insert(:note_activity)
130 author = User.get_by_ap_id(activity.data["actor"])
132 assert nil == Notification.create_notification(activity, author)
135 test "it doesn't create a notification for follow-unfollow-follow chains" do
137 followed_user = insert(:user)
138 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
139 Notification.create_notification(activity, followed_user)
140 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
141 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
142 assert nil == Notification.create_notification(activity_dupe, followed_user)
145 test "it doesn't create a notification for like-unlike-like chains" do
147 liked_user = insert(:user)
148 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
149 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
150 Notification.create_notification(fav_status, liked_user)
151 TwitterAPI.unfav(user, status.id)
152 {:ok, dupe} = TwitterAPI.fav(user, status.id)
153 assert nil == Notification.create_notification(dupe, liked_user)
156 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
158 retweeted_user = insert(:user)
161 TwitterAPI.create_status(retweeted_user, %{
162 "status" => "Send dupe notifications to the shadow realm"
165 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
166 Notification.create_notification(retweeted_activity, retweeted_user)
167 TwitterAPI.unrepeat(user, status.id)
168 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
169 assert nil == Notification.create_notification(dupe, retweeted_user)
172 test "it doesn't create duplicate notifications for follow+subscribed users" do
174 subscriber = insert(:user)
176 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
177 User.subscribe(subscriber, user)
178 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
179 {:ok, [_notif]} = Notification.create_notifications(status)
182 test "it doesn't create subscription notifications if the recipient cannot see the status" do
184 subscriber = insert(:user)
186 User.subscribe(subscriber, user)
189 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
191 assert {:ok, []} == Notification.create_notifications(status)
195 describe "get notification" do
196 test "it gets a notification that belongs to the user" do
198 other_user = insert(:user)
201 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
203 {:ok, [notification]} = Notification.create_notifications(activity)
204 {:ok, notification} = Notification.get(other_user, notification.id)
206 assert notification.user_id == other_user.id
209 test "it returns error if the notification doesn't belong to the user" do
211 other_user = insert(:user)
214 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
216 {:ok, [notification]} = Notification.create_notifications(activity)
217 {:error, _notification} = Notification.get(user, notification.id)
221 describe "dismiss notification" do
222 test "it dismisses a notification that belongs to the user" do
224 other_user = insert(:user)
227 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
229 {:ok, [notification]} = Notification.create_notifications(activity)
230 {:ok, notification} = Notification.dismiss(other_user, notification.id)
232 assert notification.user_id == other_user.id
235 test "it returns error if the notification doesn't belong to the user" do
237 other_user = insert(:user)
240 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
242 {:ok, [notification]} = Notification.create_notifications(activity)
243 {:error, _notification} = Notification.dismiss(user, notification.id)
247 describe "clear notification" do
248 test "it clears all notifications belonging to the user" do
250 other_user = insert(:user)
251 third_user = insert(:user)
254 TwitterAPI.create_status(user, %{
255 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
258 {:ok, _notifs} = Notification.create_notifications(activity)
261 TwitterAPI.create_status(user, %{
262 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
265 {:ok, _notifs} = Notification.create_notifications(activity)
266 Notification.clear(other_user)
268 assert Notification.for_user(other_user) == []
269 assert Notification.for_user(third_user) != []
273 describe "set_read_up_to()" do
274 test "it sets all notifications as read up to a specified notification ID" do
276 other_user = insert(:user)
279 TwitterAPI.create_status(user, %{
280 "status" => "hey @#{other_user.nickname}!"
284 TwitterAPI.create_status(user, %{
285 "status" => "hey again @#{other_user.nickname}!"
288 [n2, n1] = notifs = Notification.for_user(other_user)
289 assert length(notifs) == 2
294 TwitterAPI.create_status(user, %{
295 "status" => "hey yet again @#{other_user.nickname}!"
298 Notification.set_read_up_to(other_user, n2.id)
300 [n3, n2, n1] = Notification.for_user(other_user)
302 assert n1.seen == true
303 assert n2.seen == true
304 assert n3.seen == false
307 test "Updates `updated_at` field" do
308 user1 = insert(:user)
309 user2 = insert(:user)
311 Enum.each(0..10, fn i ->
313 TwitterAPI.create_status(user1, %{
314 "status" => "#{i} hi @#{user2.nickname}"
318 [notification | _] = Notification.for_user(user2)
320 utc_now = NaiveDateTime.utc_now()
321 future = NaiveDateTime.add(utc_now, 5, :second)
323 with_mock NaiveDateTime, utc_now: fn -> future end do
324 Notification.set_read_up_to(user2, notification.id)
326 Notification.for_user(user2)
327 |> Enum.each(fn notification ->
328 assert notification.updated_at > notification.inserted_at
334 describe "notification target determination" do
335 test "it sends notifications to addressed users in new messages" do
337 other_user = insert(:user)
340 CommonAPI.post(user, %{
341 "status" => "hey @#{other_user.nickname}!"
344 assert other_user in Notification.get_notified_from_activity(activity)
347 test "it sends notifications to mentioned users in new messages" do
349 other_user = insert(:user)
352 "@context" => "https://www.w3.org/ns/activitystreams",
354 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
355 "actor" => user.ap_id,
358 "content" => "message with a Mention tag, but no explicit tagging",
362 "href" => other_user.ap_id,
363 "name" => other_user.nickname
366 "attributedTo" => user.ap_id
370 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
372 assert other_user in Notification.get_notified_from_activity(activity)
375 test "it does not send notifications to users who are only cc in new messages" do
377 other_user = insert(:user)
380 "@context" => "https://www.w3.org/ns/activitystreams",
382 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
383 "cc" => [other_user.ap_id],
384 "actor" => user.ap_id,
387 "content" => "hi everyone",
388 "attributedTo" => user.ap_id
392 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
394 assert other_user not in Notification.get_notified_from_activity(activity)
397 test "it does not send notification to mentioned users in likes" do
399 other_user = insert(:user)
400 third_user = insert(:user)
402 {:ok, activity_one} =
403 CommonAPI.post(user, %{
404 "status" => "hey @#{other_user.nickname}!"
407 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
409 assert other_user not in Notification.get_notified_from_activity(activity_two)
412 test "it does not send notification to mentioned users in announces" do
414 other_user = insert(:user)
415 third_user = insert(:user)
417 {:ok, activity_one} =
418 CommonAPI.post(user, %{
419 "status" => "hey @#{other_user.nickname}!"
422 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
424 assert other_user not in Notification.get_notified_from_activity(activity_two)
428 describe "notification lifecycle" do
429 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
431 other_user = insert(:user)
433 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
435 assert Enum.empty?(Notification.for_user(user))
437 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
439 assert length(Notification.for_user(user)) == 1
441 {:ok, _} = CommonAPI.delete(activity.id, user)
443 assert Enum.empty?(Notification.for_user(user))
446 test "liking an activity results in 1 notification, then 0 if the activity is unliked" 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.unfavorite(activity.id, other_user)
460 assert Enum.empty?(Notification.for_user(user))
463 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" 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.repeat(activity.id, other_user)
473 assert length(Notification.for_user(user)) == 1
475 {:ok, _} = CommonAPI.delete(activity.id, user)
477 assert Enum.empty?(Notification.for_user(user))
480 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" 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.unrepeat(activity.id, other_user)
494 assert Enum.empty?(Notification.for_user(user))
497 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
507 assert Enum.empty?(Notification.for_user(user))
509 {:error, _} = CommonAPI.favorite(activity.id, other_user)
511 assert Enum.empty?(Notification.for_user(user))
514 test "repeating 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.repeat(activity.id, other_user)
528 assert Enum.empty?(Notification.for_user(user))
531 test "replying to a deleted post without tagging does not generate a notification" do
533 other_user = insert(:user)
535 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
536 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
538 {:ok, _reply_activity} =
539 CommonAPI.post(other_user, %{
540 "status" => "test reply",
541 "in_reply_to_status_id" => activity.id
544 assert Enum.empty?(Notification.for_user(user))