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 notification for user if he is the activity author" do
57 activity = insert(:note_activity)
58 author = User.get_by_ap_id(activity.data["actor"])
60 assert nil == Notification.create_notification(activity, author)
63 test "it doesn't create a notification for follow-unfollow-follow chains" do
65 followed_user = insert(:user)
66 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
67 Notification.create_notification(activity, followed_user)
68 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
69 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
70 assert nil == Notification.create_notification(activity_dupe, followed_user)
73 test "it doesn't create a notification for like-unlike-like chains" do
75 liked_user = insert(:user)
76 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
77 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
78 Notification.create_notification(fav_status, liked_user)
79 TwitterAPI.unfav(user, status.id)
80 {:ok, dupe} = TwitterAPI.fav(user, status.id)
81 assert nil == Notification.create_notification(dupe, liked_user)
84 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
86 retweeted_user = insert(:user)
89 TwitterAPI.create_status(retweeted_user, %{
90 "status" => "Send dupe notifications to the shadow realm"
93 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
94 Notification.create_notification(retweeted_activity, retweeted_user)
95 TwitterAPI.unrepeat(user, status.id)
96 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
97 assert nil == Notification.create_notification(dupe, retweeted_user)
100 test "it doesn't create duplicate notifications for follow+subscribed users" do
102 subscriber = insert(:user)
104 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
105 User.subscribe(subscriber, user)
106 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
107 {:ok, [_notif]} = Notification.create_notifications(status)
111 describe "get notification" do
112 test "it gets a notification that belongs to the user" do
114 other_user = insert(:user)
117 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
119 {:ok, [notification]} = Notification.create_notifications(activity)
120 {:ok, notification} = Notification.get(other_user, notification.id)
122 assert notification.user_id == other_user.id
125 test "it returns error if the notification doesn't belong to the user" do
127 other_user = insert(:user)
130 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
132 {:ok, [notification]} = Notification.create_notifications(activity)
133 {:error, _notification} = Notification.get(user, notification.id)
137 describe "dismiss notification" do
138 test "it dismisses a notification that belongs to the user" do
140 other_user = insert(:user)
143 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
145 {:ok, [notification]} = Notification.create_notifications(activity)
146 {:ok, notification} = Notification.dismiss(other_user, notification.id)
148 assert notification.user_id == other_user.id
151 test "it returns error if the notification doesn't belong to the user" do
153 other_user = insert(:user)
156 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
158 {:ok, [notification]} = Notification.create_notifications(activity)
159 {:error, _notification} = Notification.dismiss(user, notification.id)
163 describe "clear notification" do
164 test "it clears all notifications belonging to the user" do
166 other_user = insert(:user)
167 third_user = insert(:user)
170 TwitterAPI.create_status(user, %{
171 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
174 {:ok, _notifs} = Notification.create_notifications(activity)
177 TwitterAPI.create_status(user, %{
178 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
181 {:ok, _notifs} = Notification.create_notifications(activity)
182 Notification.clear(other_user)
184 assert Notification.for_user(other_user) == []
185 assert Notification.for_user(third_user) != []
189 describe "set_read_up_to()" do
190 test "it sets all notifications as read up to a specified notification ID" do
192 other_user = insert(:user)
195 TwitterAPI.create_status(user, %{
196 "status" => "hey @#{other_user.nickname}!"
200 TwitterAPI.create_status(user, %{
201 "status" => "hey again @#{other_user.nickname}!"
204 [n2, n1] = notifs = Notification.for_user(other_user)
205 assert length(notifs) == 2
210 TwitterAPI.create_status(user, %{
211 "status" => "hey yet again @#{other_user.nickname}!"
214 Notification.set_read_up_to(other_user, n2.id)
216 [n3, n2, n1] = Notification.for_user(other_user)
218 assert n1.seen == true
219 assert n2.seen == true
220 assert n3.seen == false
224 describe "notification target determination" do
225 test "it sends notifications to addressed users in new messages" do
227 other_user = insert(:user)
230 CommonAPI.post(user, %{
231 "status" => "hey @#{other_user.nickname}!"
234 assert other_user in Notification.get_notified_from_activity(activity)
237 test "it sends notifications to mentioned users in new messages" do
239 other_user = insert(:user)
242 "@context" => "https://www.w3.org/ns/activitystreams",
244 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
245 "actor" => user.ap_id,
248 "content" => "message with a Mention tag, but no explicit tagging",
252 "href" => other_user.ap_id,
253 "name" => other_user.nickname
256 "attributedTo" => user.ap_id
260 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
262 assert other_user in Notification.get_notified_from_activity(activity)
265 test "it does not send notifications to users who are only cc in new messages" do
267 other_user = insert(:user)
270 "@context" => "https://www.w3.org/ns/activitystreams",
272 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
273 "cc" => [other_user.ap_id],
274 "actor" => user.ap_id,
277 "content" => "hi everyone",
278 "attributedTo" => user.ap_id
282 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
284 assert other_user not in Notification.get_notified_from_activity(activity)
287 test "it does not send notification to mentioned users in likes" do
289 other_user = insert(:user)
290 third_user = insert(:user)
292 {:ok, activity_one} =
293 CommonAPI.post(user, %{
294 "status" => "hey @#{other_user.nickname}!"
297 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
299 assert other_user not in Notification.get_notified_from_activity(activity_two)
302 test "it does not send notification to mentioned users in announces" do
304 other_user = insert(:user)
305 third_user = insert(:user)
307 {:ok, activity_one} =
308 CommonAPI.post(user, %{
309 "status" => "hey @#{other_user.nickname}!"
312 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
314 assert other_user not in Notification.get_notified_from_activity(activity_two)
318 describe "notification lifecycle" do
319 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
321 other_user = insert(:user)
323 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
325 assert Enum.empty?(Notification.for_user(user))
327 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
329 assert length(Notification.for_user(user)) == 1
331 {:ok, _} = CommonAPI.delete(activity.id, user)
333 assert Enum.empty?(Notification.for_user(user))
336 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
338 other_user = insert(:user)
340 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
342 assert Enum.empty?(Notification.for_user(user))
344 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
346 assert length(Notification.for_user(user)) == 1
348 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
350 assert Enum.empty?(Notification.for_user(user))
353 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
355 other_user = insert(:user)
357 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
359 assert Enum.empty?(Notification.for_user(user))
361 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
363 assert length(Notification.for_user(user)) == 1
365 {:ok, _} = CommonAPI.delete(activity.id, user)
367 assert Enum.empty?(Notification.for_user(user))
370 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
372 other_user = insert(:user)
374 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
376 assert Enum.empty?(Notification.for_user(user))
378 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
380 assert length(Notification.for_user(user)) == 1
382 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
384 assert Enum.empty?(Notification.for_user(user))
387 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
397 assert Enum.empty?(Notification.for_user(user))
399 {:error, _} = CommonAPI.favorite(activity.id, other_user)
401 assert Enum.empty?(Notification.for_user(user))
404 test "repeating an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
414 assert Enum.empty?(Notification.for_user(user))
416 {:error, _} = CommonAPI.repeat(activity.id, other_user)
418 assert Enum.empty?(Notification.for_user(user))
421 test "replying to a deleted post without tagging does not generate a notification" do
423 other_user = insert(:user)
425 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
426 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
428 {:ok, _reply_activity} =
429 CommonAPI.post(other_user, %{
430 "status" => "test reply",
431 "in_reply_to_status_id" => activity.id
434 assert Enum.empty?(Notification.for_user(user))