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.TwitterAPI.TwitterAPI
12 import Pleroma.Factory
14 describe "create_notifications" do
15 test "notifies someone when they are directly addressed" do
17 other_user = insert(:user)
18 third_user = insert(:user)
21 TwitterAPI.create_status(user, %{
22 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
25 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
27 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
28 assert notified_ids == [other_user.id, third_user.id]
29 assert notification.activity_id == activity.id
30 assert other_notification.activity_id == activity.id
33 test "it creates a notification for subscribed users" do
35 subscriber = insert(:user)
37 User.subscribe(subscriber, user)
39 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
40 {:ok, [notification]} = Notification.create_notifications(status)
42 assert notification.user_id == subscriber.id
46 describe "create_notification" do
47 test "it doesn't create a notification for user if the user blocks the activity author" do
48 activity = insert(:note_activity)
49 author = User.get_cached_by_ap_id(activity.data["actor"])
51 {:ok, user} = User.block(user, author)
53 assert nil == Notification.create_notification(activity, user)
56 test "it doesn't create a notificatin for the user if the user mutes the activity author" do
59 {:ok, _} = User.mute(muter, muted)
60 muter = Repo.get(User, muter.id)
61 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
63 assert nil == Notification.create_notification(activity, muter)
66 test "it doesn't create a notification for an activity from a muted thread" do
68 other_user = insert(:user)
69 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
70 CommonAPI.add_mute(muter, activity)
73 CommonAPI.post(other_user, %{
74 "status" => "Hi @#{muter.nickname}",
75 "in_reply_to_status_id" => activity.id
78 assert nil == Notification.create_notification(activity, muter)
81 test "it disables notifications from followers" do
82 follower = insert(:user)
83 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
84 User.follow(follower, followed)
85 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
86 assert nil == Notification.create_notification(activity, followed)
89 test "it disables notifications from non-followers" do
90 follower = insert(:user)
91 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
92 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
93 assert nil == Notification.create_notification(activity, followed)
96 test "it disables notifications from people the user follows" do
97 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
98 followed = insert(:user)
99 User.follow(follower, followed)
100 follower = Repo.get(User, follower.id)
101 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
102 assert nil == Notification.create_notification(activity, follower)
105 test "it disables notifications from people the user does not follow" do
106 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
107 followed = insert(:user)
108 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
109 assert nil == Notification.create_notification(activity, follower)
112 test "it doesn't create a notification for user if he is the activity author" do
113 activity = insert(:note_activity)
114 author = User.get_cached_by_ap_id(activity.data["actor"])
116 assert nil == Notification.create_notification(activity, author)
119 test "it doesn't create a notification for follow-unfollow-follow chains" do
121 followed_user = insert(:user)
122 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
123 Notification.create_notification(activity, followed_user)
124 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
125 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
126 assert nil == Notification.create_notification(activity_dupe, followed_user)
129 test "it doesn't create a notification for like-unlike-like chains" do
131 liked_user = insert(:user)
132 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
133 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
134 Notification.create_notification(fav_status, liked_user)
135 TwitterAPI.unfav(user, status.id)
136 {:ok, dupe} = TwitterAPI.fav(user, status.id)
137 assert nil == Notification.create_notification(dupe, liked_user)
140 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
142 retweeted_user = insert(:user)
145 TwitterAPI.create_status(retweeted_user, %{
146 "status" => "Send dupe notifications to the shadow realm"
149 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
150 Notification.create_notification(retweeted_activity, retweeted_user)
151 TwitterAPI.unrepeat(user, status.id)
152 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
153 assert nil == Notification.create_notification(dupe, retweeted_user)
156 test "it doesn't create duplicate notifications for follow+subscribed users" do
158 subscriber = insert(:user)
160 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
161 User.subscribe(subscriber, user)
162 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
163 {:ok, [_notif]} = Notification.create_notifications(status)
166 test "it doesn't create subscription notifications if the recipient cannot see the status" do
168 subscriber = insert(:user)
170 User.subscribe(subscriber, user)
173 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
175 assert {:ok, []} == Notification.create_notifications(status)
179 describe "get notification" do
180 test "it gets a notification that belongs to the user" do
182 other_user = insert(:user)
185 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
187 {:ok, [notification]} = Notification.create_notifications(activity)
188 {:ok, notification} = Notification.get(other_user, notification.id)
190 assert notification.user_id == other_user.id
193 test "it returns error if the notification doesn't belong to the user" do
195 other_user = insert(:user)
198 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
200 {:ok, [notification]} = Notification.create_notifications(activity)
201 {:error, _notification} = Notification.get(user, notification.id)
205 describe "dismiss notification" do
206 test "it dismisses a notification that belongs to the user" do
208 other_user = insert(:user)
211 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
213 {:ok, [notification]} = Notification.create_notifications(activity)
214 {:ok, notification} = Notification.dismiss(other_user, notification.id)
216 assert notification.user_id == other_user.id
219 test "it returns error if the notification doesn't belong to the user" do
221 other_user = insert(:user)
224 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
226 {:ok, [notification]} = Notification.create_notifications(activity)
227 {:error, _notification} = Notification.dismiss(user, notification.id)
231 describe "clear notification" do
232 test "it clears all notifications belonging to the user" do
234 other_user = insert(:user)
235 third_user = insert(:user)
238 TwitterAPI.create_status(user, %{
239 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
242 {:ok, _notifs} = Notification.create_notifications(activity)
245 TwitterAPI.create_status(user, %{
246 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
249 {:ok, _notifs} = Notification.create_notifications(activity)
250 Notification.clear(other_user)
252 assert Notification.for_user(other_user) == []
253 assert Notification.for_user(third_user) != []
257 describe "set_read_up_to()" do
258 test "it sets all notifications as read up to a specified notification ID" do
260 other_user = insert(:user)
263 TwitterAPI.create_status(user, %{
264 "status" => "hey @#{other_user.nickname}!"
268 TwitterAPI.create_status(user, %{
269 "status" => "hey again @#{other_user.nickname}!"
272 [n2, n1] = notifs = Notification.for_user(other_user)
273 assert length(notifs) == 2
278 TwitterAPI.create_status(user, %{
279 "status" => "hey yet again @#{other_user.nickname}!"
282 Notification.set_read_up_to(other_user, n2.id)
284 [n3, n2, n1] = Notification.for_user(other_user)
286 assert n1.seen == true
287 assert n2.seen == true
288 assert n3.seen == false
292 describe "notification target determination" do
293 test "it sends notifications to addressed users in new messages" do
295 other_user = insert(:user)
298 CommonAPI.post(user, %{
299 "status" => "hey @#{other_user.nickname}!"
302 assert other_user in Notification.get_notified_from_activity(activity)
305 test "it sends notifications to mentioned users in new messages" do
307 other_user = insert(:user)
310 "@context" => "https://www.w3.org/ns/activitystreams",
312 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
313 "actor" => user.ap_id,
316 "content" => "message with a Mention tag, but no explicit tagging",
320 "href" => other_user.ap_id,
321 "name" => other_user.nickname
324 "attributedTo" => user.ap_id
328 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
330 assert other_user in Notification.get_notified_from_activity(activity)
333 test "it does not send notifications to users who are only cc in new messages" do
335 other_user = insert(:user)
338 "@context" => "https://www.w3.org/ns/activitystreams",
340 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
341 "cc" => [other_user.ap_id],
342 "actor" => user.ap_id,
345 "content" => "hi everyone",
346 "attributedTo" => user.ap_id
350 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
352 assert other_user not in Notification.get_notified_from_activity(activity)
355 test "it does not send notification to mentioned users in likes" do
357 other_user = insert(:user)
358 third_user = insert(:user)
360 {:ok, activity_one} =
361 CommonAPI.post(user, %{
362 "status" => "hey @#{other_user.nickname}!"
365 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
367 assert other_user not in Notification.get_notified_from_activity(activity_two)
370 test "it does not send notification to mentioned users in announces" do
372 other_user = insert(:user)
373 third_user = insert(:user)
375 {:ok, activity_one} =
376 CommonAPI.post(user, %{
377 "status" => "hey @#{other_user.nickname}!"
380 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
382 assert other_user not in Notification.get_notified_from_activity(activity_two)
386 describe "notification lifecycle" do
387 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
389 other_user = insert(:user)
391 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
393 assert Enum.empty?(Notification.for_user(user))
395 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
397 assert length(Notification.for_user(user)) == 1
399 {:ok, _} = CommonAPI.delete(activity.id, user)
401 assert Enum.empty?(Notification.for_user(user))
404 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
406 other_user = insert(:user)
408 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
410 assert Enum.empty?(Notification.for_user(user))
412 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
414 assert length(Notification.for_user(user)) == 1
416 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
418 assert Enum.empty?(Notification.for_user(user))
421 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
423 other_user = insert(:user)
425 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
427 assert Enum.empty?(Notification.for_user(user))
429 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
431 assert length(Notification.for_user(user)) == 1
433 {:ok, _} = CommonAPI.delete(activity.id, user)
435 assert Enum.empty?(Notification.for_user(user))
438 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
440 other_user = insert(:user)
442 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
444 assert Enum.empty?(Notification.for_user(user))
446 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
448 assert length(Notification.for_user(user)) == 1
450 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
452 assert Enum.empty?(Notification.for_user(user))
455 test "liking an activity which is already deleted does not generate a notification" do
457 other_user = insert(:user)
459 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
461 assert Enum.empty?(Notification.for_user(user))
463 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
465 assert Enum.empty?(Notification.for_user(user))
467 {:error, _} = CommonAPI.favorite(activity.id, other_user)
469 assert Enum.empty?(Notification.for_user(user))
472 test "repeating an activity which is already deleted does not generate a notification" do
474 other_user = insert(:user)
476 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
478 assert Enum.empty?(Notification.for_user(user))
480 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
482 assert Enum.empty?(Notification.for_user(user))
484 {:error, _} = CommonAPI.repeat(activity.id, other_user)
486 assert Enum.empty?(Notification.for_user(user))
489 test "replying to a deleted post without tagging does not generate a notification" do
491 other_user = insert(:user)
493 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
494 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
496 {:ok, _reply_activity} =
497 CommonAPI.post(other_user, %{
498 "status" => "test reply",
499 "in_reply_to_status_id" => activity.id
502 assert Enum.empty?(Notification.for_user(user))