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_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 people on remote instances" do
82 user = insert(:user, info: %{notification_settings: %{"remote" => false}})
83 other_user = insert(:user)
86 "@context" => "https://www.w3.org/ns/activitystreams",
88 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
89 "actor" => other_user.ap_id,
92 "content" => "Hi @#{user.nickname}",
93 "attributedTo" => other_user.ap_id
97 {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
98 assert nil == Notification.create_notification(activity, user)
101 test "it disables notifications from people on the local instance" do
102 user = insert(:user, info: %{notification_settings: %{"local" => false}})
103 other_user = insert(:user)
104 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
105 assert nil == Notification.create_notification(activity, user)
108 test "it disables notifications from followers" do
109 follower = insert(:user)
110 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
111 User.follow(follower, followed)
112 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
113 assert nil == Notification.create_notification(activity, followed)
116 test "it disables notifications from people the user follows" do
117 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
118 followed = insert(:user)
119 User.follow(follower, followed)
120 follower = Repo.get(User, follower.id)
121 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
122 assert nil == Notification.create_notification(activity, follower)
125 test "it doesn't create a notification for user if he is the activity author" do
126 activity = insert(:note_activity)
127 author = User.get_by_ap_id(activity.data["actor"])
129 assert nil == Notification.create_notification(activity, author)
132 test "it doesn't create a notification for follow-unfollow-follow chains" do
134 followed_user = insert(:user)
135 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
136 Notification.create_notification(activity, followed_user)
137 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
138 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
139 assert nil == Notification.create_notification(activity_dupe, followed_user)
142 test "it doesn't create a notification for like-unlike-like chains" do
144 liked_user = insert(:user)
145 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
146 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
147 Notification.create_notification(fav_status, liked_user)
148 TwitterAPI.unfav(user, status.id)
149 {:ok, dupe} = TwitterAPI.fav(user, status.id)
150 assert nil == Notification.create_notification(dupe, liked_user)
153 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
155 retweeted_user = insert(:user)
158 TwitterAPI.create_status(retweeted_user, %{
159 "status" => "Send dupe notifications to the shadow realm"
162 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
163 Notification.create_notification(retweeted_activity, retweeted_user)
164 TwitterAPI.unrepeat(user, status.id)
165 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
166 assert nil == Notification.create_notification(dupe, retweeted_user)
169 test "it doesn't create duplicate notifications for follow+subscribed users" do
171 subscriber = insert(:user)
173 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
174 User.subscribe(subscriber, user)
175 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
176 {:ok, [_notif]} = Notification.create_notifications(status)
179 test "it doesn't create subscription notifications if the recipient cannot see the status" do
181 subscriber = insert(:user)
183 User.subscribe(subscriber, user)
186 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
188 assert {:ok, []} == Notification.create_notifications(status)
192 describe "get notification" do
193 test "it gets a notification that belongs 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 {:ok, notification} = Notification.get(other_user, notification.id)
203 assert notification.user_id == other_user.id
206 test "it returns error if the notification doesn't belong 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 {:error, _notification} = Notification.get(user, notification.id)
218 describe "dismiss notification" do
219 test "it dismisses a notification that belongs 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 {:ok, notification} = Notification.dismiss(other_user, notification.id)
229 assert notification.user_id == other_user.id
232 test "it returns error if the notification doesn't belong to the user" do
234 other_user = insert(:user)
237 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
239 {:ok, [notification]} = Notification.create_notifications(activity)
240 {:error, _notification} = Notification.dismiss(user, notification.id)
244 describe "clear notification" do
245 test "it clears all notifications belonging to the user" do
247 other_user = insert(:user)
248 third_user = insert(:user)
251 TwitterAPI.create_status(user, %{
252 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
255 {:ok, _notifs} = Notification.create_notifications(activity)
258 TwitterAPI.create_status(user, %{
259 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
262 {:ok, _notifs} = Notification.create_notifications(activity)
263 Notification.clear(other_user)
265 assert Notification.for_user(other_user) == []
266 assert Notification.for_user(third_user) != []
270 describe "set_read_up_to()" do
271 test "it sets all notifications as read up to a specified notification ID" do
273 other_user = insert(:user)
276 TwitterAPI.create_status(user, %{
277 "status" => "hey @#{other_user.nickname}!"
281 TwitterAPI.create_status(user, %{
282 "status" => "hey again @#{other_user.nickname}!"
285 [n2, n1] = notifs = Notification.for_user(other_user)
286 assert length(notifs) == 2
291 TwitterAPI.create_status(user, %{
292 "status" => "hey yet again @#{other_user.nickname}!"
295 Notification.set_read_up_to(other_user, n2.id)
297 [n3, n2, n1] = Notification.for_user(other_user)
299 assert n1.seen == true
300 assert n2.seen == true
301 assert n3.seen == false
305 describe "notification target determination" do
306 test "it sends notifications to addressed users in new messages" do
308 other_user = insert(:user)
311 CommonAPI.post(user, %{
312 "status" => "hey @#{other_user.nickname}!"
315 assert other_user in Notification.get_notified_from_activity(activity)
318 test "it sends notifications to mentioned users in new messages" do
320 other_user = insert(:user)
323 "@context" => "https://www.w3.org/ns/activitystreams",
325 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
326 "actor" => user.ap_id,
329 "content" => "message with a Mention tag, but no explicit tagging",
333 "href" => other_user.ap_id,
334 "name" => other_user.nickname
337 "attributedTo" => user.ap_id
341 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
343 assert other_user in Notification.get_notified_from_activity(activity)
346 test "it does not send notifications to users who are only cc 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 "cc" => [other_user.ap_id],
355 "actor" => user.ap_id,
358 "content" => "hi everyone",
359 "attributedTo" => user.ap_id
363 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
365 assert other_user not in Notification.get_notified_from_activity(activity)
368 test "it does not send notification to mentioned users in likes" do
370 other_user = insert(:user)
371 third_user = insert(:user)
373 {:ok, activity_one} =
374 CommonAPI.post(user, %{
375 "status" => "hey @#{other_user.nickname}!"
378 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
380 assert other_user not in Notification.get_notified_from_activity(activity_two)
383 test "it does not send notification to mentioned users in announces" do
385 other_user = insert(:user)
386 third_user = insert(:user)
388 {:ok, activity_one} =
389 CommonAPI.post(user, %{
390 "status" => "hey @#{other_user.nickname}!"
393 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
395 assert other_user not in Notification.get_notified_from_activity(activity_two)
399 describe "notification lifecycle" do
400 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
402 other_user = insert(:user)
404 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
406 assert Enum.empty?(Notification.for_user(user))
408 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
410 assert length(Notification.for_user(user)) == 1
412 {:ok, _} = CommonAPI.delete(activity.id, user)
414 assert Enum.empty?(Notification.for_user(user))
417 test "liking an activity results in 1 notification, then 0 if the activity is unliked" 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.unfavorite(activity.id, other_user)
431 assert Enum.empty?(Notification.for_user(user))
434 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" 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.repeat(activity.id, other_user)
444 assert length(Notification.for_user(user)) == 1
446 {:ok, _} = CommonAPI.delete(activity.id, user)
448 assert Enum.empty?(Notification.for_user(user))
451 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" 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.unrepeat(activity.id, other_user)
465 assert Enum.empty?(Notification.for_user(user))
468 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
478 assert Enum.empty?(Notification.for_user(user))
480 {:error, _} = CommonAPI.favorite(activity.id, other_user)
482 assert Enum.empty?(Notification.for_user(user))
485 test "repeating 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.repeat(activity.id, other_user)
499 assert Enum.empty?(Notification.for_user(user))
502 test "replying to a deleted post without tagging does not generate a notification" do
504 other_user = insert(:user)
506 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
507 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
509 {:ok, _reply_activity} =
510 CommonAPI.post(other_user, %{
511 "status" => "test reply",
512 "in_reply_to_status_id" => activity.id
515 assert Enum.empty?(Notification.for_user(user))