Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / notification_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.NotificationTest do
6 use Pleroma.DataCase
7 alias Pleroma.Notification
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.Transmogrifier
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.TwitterAPI.TwitterAPI
12 import Pleroma.Factory
13
14 describe "create_notifications" do
15 test "notifies someone when they are directly addressed" do
16 user = insert(:user)
17 other_user = insert(:user)
18 third_user = insert(:user)
19
20 {:ok, activity} =
21 TwitterAPI.create_status(user, %{
22 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
23 })
24
25 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
26
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
31 end
32
33 test "it creates a notification for subscribed users" do
34 user = insert(:user)
35 subscriber = insert(:user)
36
37 User.subscribe(subscriber, user)
38
39 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
40 {:ok, [notification]} = Notification.create_notifications(status)
41
42 assert notification.user_id == subscriber.id
43 end
44 end
45
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"])
50 user = insert(:user)
51 {:ok, user} = User.block(user, author)
52
53 assert nil == Notification.create_notification(activity, user)
54 end
55
56 test "it doesn't create a notificatin for the user if the user mutes the activity author" do
57 muter = insert(:user)
58 muted = insert(:user)
59 {:ok, _} = User.mute(muter, muted)
60 muter = Repo.get(User, muter.id)
61 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
62
63 assert nil == Notification.create_notification(activity, muter)
64 end
65
66 test "it doesn't create a notification for an activity from a muted thread" do
67 muter = insert(:user)
68 other_user = insert(:user)
69 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
70 CommonAPI.add_mute(muter, activity)
71
72 {:ok, activity} =
73 CommonAPI.post(other_user, %{
74 "status" => "Hi @#{muter.nickname}",
75 "in_reply_to_status_id" => activity.id
76 })
77
78 assert nil == Notification.create_notification(activity, muter)
79 end
80
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)
84
85 create_activity = %{
86 "@context" => "https://www.w3.org/ns/activitystreams",
87 "type" => "Create",
88 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
89 "actor" => other_user.ap_id,
90 "object" => %{
91 "type" => "Note",
92 "content" => "Hi @#{user.nickname}",
93 "attributedTo" => other_user.ap_id
94 }
95 }
96
97 {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
98 assert nil == Notification.create_notification(activity, user)
99 end
100
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)
106 end
107
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)
114 end
115
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)
123 end
124
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_cached_by_ap_id(activity.data["actor"])
128
129 assert nil == Notification.create_notification(activity, author)
130 end
131
132 test "it doesn't create a notification for follow-unfollow-follow chains" do
133 user = insert(:user)
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)
140 end
141
142 test "it doesn't create a notification for like-unlike-like chains" do
143 user = insert(:user)
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)
151 end
152
153 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
154 user = insert(:user)
155 retweeted_user = insert(:user)
156
157 {:ok, status} =
158 TwitterAPI.create_status(retweeted_user, %{
159 "status" => "Send dupe notifications to the shadow realm"
160 })
161
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)
167 end
168
169 test "it doesn't create duplicate notifications for follow+subscribed users" do
170 user = insert(:user)
171 subscriber = insert(:user)
172
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)
177 end
178
179 test "it doesn't create subscription notifications if the recipient cannot see the status" do
180 user = insert(:user)
181 subscriber = insert(:user)
182
183 User.subscribe(subscriber, user)
184
185 {:ok, status} =
186 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
187
188 assert {:ok, []} == Notification.create_notifications(status)
189 end
190 end
191
192 describe "get notification" do
193 test "it gets a notification that belongs to the user" do
194 user = insert(:user)
195 other_user = insert(:user)
196
197 {:ok, activity} =
198 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
199
200 {:ok, [notification]} = Notification.create_notifications(activity)
201 {:ok, notification} = Notification.get(other_user, notification.id)
202
203 assert notification.user_id == other_user.id
204 end
205
206 test "it returns error if the notification doesn't belong to the user" do
207 user = insert(:user)
208 other_user = insert(:user)
209
210 {:ok, activity} =
211 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
212
213 {:ok, [notification]} = Notification.create_notifications(activity)
214 {:error, _notification} = Notification.get(user, notification.id)
215 end
216 end
217
218 describe "dismiss notification" do
219 test "it dismisses a notification that belongs to the user" do
220 user = insert(:user)
221 other_user = insert(:user)
222
223 {:ok, activity} =
224 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
225
226 {:ok, [notification]} = Notification.create_notifications(activity)
227 {:ok, notification} = Notification.dismiss(other_user, notification.id)
228
229 assert notification.user_id == other_user.id
230 end
231
232 test "it returns error if the notification doesn't belong to the user" do
233 user = insert(:user)
234 other_user = insert(:user)
235
236 {:ok, activity} =
237 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
238
239 {:ok, [notification]} = Notification.create_notifications(activity)
240 {:error, _notification} = Notification.dismiss(user, notification.id)
241 end
242 end
243
244 describe "clear notification" do
245 test "it clears all notifications belonging to the user" do
246 user = insert(:user)
247 other_user = insert(:user)
248 third_user = insert(:user)
249
250 {:ok, activity} =
251 TwitterAPI.create_status(user, %{
252 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
253 })
254
255 {:ok, _notifs} = Notification.create_notifications(activity)
256
257 {:ok, activity} =
258 TwitterAPI.create_status(user, %{
259 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
260 })
261
262 {:ok, _notifs} = Notification.create_notifications(activity)
263 Notification.clear(other_user)
264
265 assert Notification.for_user(other_user) == []
266 assert Notification.for_user(third_user) != []
267 end
268 end
269
270 describe "set_read_up_to()" do
271 test "it sets all notifications as read up to a specified notification ID" do
272 user = insert(:user)
273 other_user = insert(:user)
274
275 {:ok, _activity} =
276 TwitterAPI.create_status(user, %{
277 "status" => "hey @#{other_user.nickname}!"
278 })
279
280 {:ok, _activity} =
281 TwitterAPI.create_status(user, %{
282 "status" => "hey again @#{other_user.nickname}!"
283 })
284
285 [n2, n1] = notifs = Notification.for_user(other_user)
286 assert length(notifs) == 2
287
288 assert n2.id > n1.id
289
290 {:ok, _activity} =
291 TwitterAPI.create_status(user, %{
292 "status" => "hey yet again @#{other_user.nickname}!"
293 })
294
295 Notification.set_read_up_to(other_user, n2.id)
296
297 [n3, n2, n1] = Notification.for_user(other_user)
298
299 assert n1.seen == true
300 assert n2.seen == true
301 assert n3.seen == false
302 end
303 end
304
305 describe "notification target determination" do
306 test "it sends notifications to addressed users in new messages" do
307 user = insert(:user)
308 other_user = insert(:user)
309
310 {:ok, activity} =
311 CommonAPI.post(user, %{
312 "status" => "hey @#{other_user.nickname}!"
313 })
314
315 assert other_user in Notification.get_notified_from_activity(activity)
316 end
317
318 test "it sends notifications to mentioned users in new messages" do
319 user = insert(:user)
320 other_user = insert(:user)
321
322 create_activity = %{
323 "@context" => "https://www.w3.org/ns/activitystreams",
324 "type" => "Create",
325 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
326 "actor" => user.ap_id,
327 "object" => %{
328 "type" => "Note",
329 "content" => "message with a Mention tag, but no explicit tagging",
330 "tag" => [
331 %{
332 "type" => "Mention",
333 "href" => other_user.ap_id,
334 "name" => other_user.nickname
335 }
336 ],
337 "attributedTo" => user.ap_id
338 }
339 }
340
341 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
342
343 assert other_user in Notification.get_notified_from_activity(activity)
344 end
345
346 test "it does not send notifications to users who are only cc in new messages" do
347 user = insert(:user)
348 other_user = insert(:user)
349
350 create_activity = %{
351 "@context" => "https://www.w3.org/ns/activitystreams",
352 "type" => "Create",
353 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
354 "cc" => [other_user.ap_id],
355 "actor" => user.ap_id,
356 "object" => %{
357 "type" => "Note",
358 "content" => "hi everyone",
359 "attributedTo" => user.ap_id
360 }
361 }
362
363 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
364
365 assert other_user not in Notification.get_notified_from_activity(activity)
366 end
367
368 test "it does not send notification to mentioned users in likes" do
369 user = insert(:user)
370 other_user = insert(:user)
371 third_user = insert(:user)
372
373 {:ok, activity_one} =
374 CommonAPI.post(user, %{
375 "status" => "hey @#{other_user.nickname}!"
376 })
377
378 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
379
380 assert other_user not in Notification.get_notified_from_activity(activity_two)
381 end
382
383 test "it does not send notification to mentioned users in announces" do
384 user = insert(:user)
385 other_user = insert(:user)
386 third_user = insert(:user)
387
388 {:ok, activity_one} =
389 CommonAPI.post(user, %{
390 "status" => "hey @#{other_user.nickname}!"
391 })
392
393 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
394
395 assert other_user not in Notification.get_notified_from_activity(activity_two)
396 end
397 end
398
399 describe "notification lifecycle" do
400 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
401 user = insert(:user)
402 other_user = insert(:user)
403
404 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
405
406 assert Enum.empty?(Notification.for_user(user))
407
408 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
409
410 assert length(Notification.for_user(user)) == 1
411
412 {:ok, _} = CommonAPI.delete(activity.id, user)
413
414 assert Enum.empty?(Notification.for_user(user))
415 end
416
417 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
418 user = insert(:user)
419 other_user = insert(:user)
420
421 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
422
423 assert Enum.empty?(Notification.for_user(user))
424
425 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
426
427 assert length(Notification.for_user(user)) == 1
428
429 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
430
431 assert Enum.empty?(Notification.for_user(user))
432 end
433
434 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
435 user = insert(:user)
436 other_user = insert(:user)
437
438 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
439
440 assert Enum.empty?(Notification.for_user(user))
441
442 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
443
444 assert length(Notification.for_user(user)) == 1
445
446 {:ok, _} = CommonAPI.delete(activity.id, user)
447
448 assert Enum.empty?(Notification.for_user(user))
449 end
450
451 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
452 user = insert(:user)
453 other_user = insert(:user)
454
455 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
456
457 assert Enum.empty?(Notification.for_user(user))
458
459 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
460
461 assert length(Notification.for_user(user)) == 1
462
463 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
464
465 assert Enum.empty?(Notification.for_user(user))
466 end
467
468 test "liking an activity which is already deleted does not generate a notification" do
469 user = insert(:user)
470 other_user = insert(:user)
471
472 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
473
474 assert Enum.empty?(Notification.for_user(user))
475
476 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
477
478 assert Enum.empty?(Notification.for_user(user))
479
480 {:error, _} = CommonAPI.favorite(activity.id, other_user)
481
482 assert Enum.empty?(Notification.for_user(user))
483 end
484
485 test "repeating an activity which is already deleted does not generate a notification" do
486 user = insert(:user)
487 other_user = insert(:user)
488
489 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
490
491 assert Enum.empty?(Notification.for_user(user))
492
493 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
494
495 assert Enum.empty?(Notification.for_user(user))
496
497 {:error, _} = CommonAPI.repeat(activity.id, other_user)
498
499 assert Enum.empty?(Notification.for_user(user))
500 end
501
502 test "replying to a deleted post without tagging does not generate a notification" do
503 user = insert(:user)
504 other_user = insert(:user)
505
506 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
507 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
508
509 {:ok, _reply_activity} =
510 CommonAPI.post(other_user, %{
511 "status" => "test reply",
512 "in_reply_to_status_id" => activity.id
513 })
514
515 assert Enum.empty?(Notification.for_user(user))
516 end
517 end
518 end