Merge branch 'develop' into feature/digest-email
[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
8 import Pleroma.Factory
9
10 alias Pleroma.Notification
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Streamer
15 alias Pleroma.Web.TwitterAPI.TwitterAPI
16
17 describe "create_notifications" do
18 test "notifies someone when they are directly addressed" do
19 user = insert(:user)
20 other_user = insert(:user)
21 third_user = insert(:user)
22
23 {:ok, activity} =
24 TwitterAPI.create_status(user, %{
25 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
26 })
27
28 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
29
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
34 end
35
36 test "it creates a notification for subscribed users" do
37 user = insert(:user)
38 subscriber = insert(:user)
39
40 User.subscribe(subscriber, user)
41
42 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
43 {:ok, [notification]} = Notification.create_notifications(status)
44
45 assert notification.user_id == subscriber.id
46 end
47 end
48
49 describe "create_notification" do
50 setup do
51 GenServer.start(Streamer, %{}, name: Streamer)
52
53 on_exit(fn ->
54 if pid = Process.whereis(Streamer) do
55 Process.exit(pid, :kill)
56 end
57 end)
58 end
59
60 test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
61 user = insert(:user)
62 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
63 task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
64 Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
65
66 Streamer.add_socket(
67 "user:notification",
68 %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
69 )
70
71 activity = insert(:note_activity)
72
73 notify = Notification.create_notification(activity, user)
74 assert notify.user_id == user.id
75 Task.await(task)
76 Task.await(task_user_notification)
77 end
78
79 test "it creates a notification for user if the user blocks the activity author" do
80 activity = insert(:note_activity)
81 author = User.get_cached_by_ap_id(activity.data["actor"])
82 user = insert(:user)
83 {:ok, user} = User.block(user, author)
84
85 assert Notification.create_notification(activity, user)
86 end
87
88 test "it creates a notificatin for the user if the user mutes the activity author" do
89 muter = insert(:user)
90 muted = insert(:user)
91 {:ok, _} = User.mute(muter, muted)
92 muter = Repo.get(User, muter.id)
93 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
94
95 assert Notification.create_notification(activity, muter)
96 end
97
98 test "notification created if user is muted without notifications" do
99 muter = insert(:user)
100 muted = insert(:user)
101
102 {:ok, muter} = User.mute(muter, muted, false)
103
104 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
105
106 assert Notification.create_notification(activity, muter)
107 end
108
109 test "it creates a notification for an activity from a muted thread" do
110 muter = insert(:user)
111 other_user = insert(:user)
112 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
113 CommonAPI.add_mute(muter, activity)
114
115 {:ok, activity} =
116 CommonAPI.post(other_user, %{
117 "status" => "Hi @#{muter.nickname}",
118 "in_reply_to_status_id" => activity.id
119 })
120
121 assert Notification.create_notification(activity, muter)
122 end
123
124 test "it disables notifications from followers" do
125 follower = insert(:user)
126 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
127 User.follow(follower, followed)
128 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
129 refute Notification.create_notification(activity, followed)
130 end
131
132 test "it disables notifications from non-followers" do
133 follower = insert(:user)
134 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
135 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
136 refute Notification.create_notification(activity, followed)
137 end
138
139 test "it disables notifications from people the user follows" do
140 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
141 followed = insert(:user)
142 User.follow(follower, followed)
143 follower = Repo.get(User, follower.id)
144 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
145 refute Notification.create_notification(activity, follower)
146 end
147
148 test "it disables notifications from people the user does not follow" do
149 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
150 followed = insert(:user)
151 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
152 refute Notification.create_notification(activity, follower)
153 end
154
155 test "it doesn't create a notification for user if he is the activity author" do
156 activity = insert(:note_activity)
157 author = User.get_cached_by_ap_id(activity.data["actor"])
158
159 refute Notification.create_notification(activity, author)
160 end
161
162 test "it doesn't create a notification for follow-unfollow-follow chains" do
163 user = insert(:user)
164 followed_user = insert(:user)
165 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
166 Notification.create_notification(activity, followed_user)
167 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
168 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
169 refute Notification.create_notification(activity_dupe, followed_user)
170 end
171
172 test "it doesn't create a notification for like-unlike-like chains" do
173 user = insert(:user)
174 liked_user = insert(:user)
175 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
176 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
177 Notification.create_notification(fav_status, liked_user)
178 TwitterAPI.unfav(user, status.id)
179 {:ok, dupe} = TwitterAPI.fav(user, status.id)
180 refute Notification.create_notification(dupe, liked_user)
181 end
182
183 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
184 user = insert(:user)
185 retweeted_user = insert(:user)
186
187 {:ok, status} =
188 TwitterAPI.create_status(retweeted_user, %{
189 "status" => "Send dupe notifications to the shadow realm"
190 })
191
192 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
193 Notification.create_notification(retweeted_activity, retweeted_user)
194 TwitterAPI.unrepeat(user, status.id)
195 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
196 refute Notification.create_notification(dupe, retweeted_user)
197 end
198
199 test "it doesn't create duplicate notifications for follow+subscribed users" do
200 user = insert(:user)
201 subscriber = insert(:user)
202
203 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
204 User.subscribe(subscriber, user)
205 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
206 {:ok, [_notif]} = Notification.create_notifications(status)
207 end
208
209 test "it doesn't create subscription notifications if the recipient cannot see the status" do
210 user = insert(:user)
211 subscriber = insert(:user)
212
213 User.subscribe(subscriber, user)
214
215 {:ok, status} =
216 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
217
218 assert {:ok, []} == Notification.create_notifications(status)
219 end
220 end
221
222 describe "get notification" do
223 test "it gets a notification that belongs to the user" do
224 user = insert(:user)
225 other_user = insert(:user)
226
227 {:ok, activity} =
228 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
229
230 {:ok, [notification]} = Notification.create_notifications(activity)
231 {:ok, notification} = Notification.get(other_user, notification.id)
232
233 assert notification.user_id == other_user.id
234 end
235
236 test "it returns error if the notification doesn't belong to the user" do
237 user = insert(:user)
238 other_user = insert(:user)
239
240 {:ok, activity} =
241 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
242
243 {:ok, [notification]} = Notification.create_notifications(activity)
244 {:error, _notification} = Notification.get(user, notification.id)
245 end
246 end
247
248 describe "dismiss notification" do
249 test "it dismisses a notification that belongs to the user" do
250 user = insert(:user)
251 other_user = insert(:user)
252
253 {:ok, activity} =
254 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
255
256 {:ok, [notification]} = Notification.create_notifications(activity)
257 {:ok, notification} = Notification.dismiss(other_user, notification.id)
258
259 assert notification.user_id == other_user.id
260 end
261
262 test "it returns error if the notification doesn't belong to the user" do
263 user = insert(:user)
264 other_user = insert(:user)
265
266 {:ok, activity} =
267 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
268
269 {:ok, [notification]} = Notification.create_notifications(activity)
270 {:error, _notification} = Notification.dismiss(user, notification.id)
271 end
272 end
273
274 describe "clear notification" do
275 test "it clears all notifications belonging to the user" do
276 user = insert(:user)
277 other_user = insert(:user)
278 third_user = insert(:user)
279
280 {:ok, activity} =
281 TwitterAPI.create_status(user, %{
282 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
283 })
284
285 {:ok, _notifs} = Notification.create_notifications(activity)
286
287 {:ok, activity} =
288 TwitterAPI.create_status(user, %{
289 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
290 })
291
292 {:ok, _notifs} = Notification.create_notifications(activity)
293 Notification.clear(other_user)
294
295 assert Notification.for_user(other_user) == []
296 assert Notification.for_user(third_user) != []
297 end
298 end
299
300 describe "set_read_up_to()" do
301 test "it sets all notifications as read up to a specified notification ID" do
302 user = insert(:user)
303 other_user = insert(:user)
304
305 {:ok, _activity} =
306 TwitterAPI.create_status(user, %{
307 "status" => "hey @#{other_user.nickname}!"
308 })
309
310 {:ok, _activity} =
311 TwitterAPI.create_status(user, %{
312 "status" => "hey again @#{other_user.nickname}!"
313 })
314
315 [n2, n1] = notifs = Notification.for_user(other_user)
316 assert length(notifs) == 2
317
318 assert n2.id > n1.id
319
320 {:ok, _activity} =
321 TwitterAPI.create_status(user, %{
322 "status" => "hey yet again @#{other_user.nickname}!"
323 })
324
325 Notification.set_read_up_to(other_user, n2.id)
326
327 [n3, n2, n1] = Notification.for_user(other_user)
328
329 assert n1.seen == true
330 assert n2.seen == true
331 assert n3.seen == false
332 end
333 end
334
335 describe "for_user_since/2" do
336 defp days_ago(days) do
337 NaiveDateTime.add(
338 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
339 -days * 60 * 60 * 24,
340 :second
341 )
342 end
343
344 test "Returns recent notifications" do
345 user1 = insert(:user)
346 user2 = insert(:user)
347
348 Enum.each(0..10, fn i ->
349 {:ok, _activity} =
350 CommonAPI.post(user1, %{
351 "status" => "hey ##{i} @#{user2.nickname}!"
352 })
353 end)
354
355 {old, new} = Enum.split(Notification.for_user(user2), 5)
356
357 Enum.each(old, fn notification ->
358 notification
359 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
360 |> Pleroma.Repo.update!()
361 end)
362
363 recent_notifications_ids =
364 user2
365 |> Notification.for_user_since(
366 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
367 )
368 |> Enum.map(& &1.id)
369
370 Enum.each(old, fn %{id: id} ->
371 refute id in recent_notifications_ids
372 end)
373
374 Enum.each(new, fn %{id: id} ->
375 assert id in recent_notifications_ids
376 end)
377 end
378 end
379
380 describe "notification target determination" do
381 test "it sends notifications to addressed users in new messages" do
382 user = insert(:user)
383 other_user = insert(:user)
384
385 {:ok, activity} =
386 CommonAPI.post(user, %{
387 "status" => "hey @#{other_user.nickname}!"
388 })
389
390 assert other_user in Notification.get_notified_from_activity(activity)
391 end
392
393 test "it sends notifications to mentioned users in new messages" do
394 user = insert(:user)
395 other_user = insert(:user)
396
397 create_activity = %{
398 "@context" => "https://www.w3.org/ns/activitystreams",
399 "type" => "Create",
400 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
401 "actor" => user.ap_id,
402 "object" => %{
403 "type" => "Note",
404 "content" => "message with a Mention tag, but no explicit tagging",
405 "tag" => [
406 %{
407 "type" => "Mention",
408 "href" => other_user.ap_id,
409 "name" => other_user.nickname
410 }
411 ],
412 "attributedTo" => user.ap_id
413 }
414 }
415
416 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
417
418 assert other_user in Notification.get_notified_from_activity(activity)
419 end
420
421 test "it does not send notifications to users who are only cc in new messages" do
422 user = insert(:user)
423 other_user = insert(:user)
424
425 create_activity = %{
426 "@context" => "https://www.w3.org/ns/activitystreams",
427 "type" => "Create",
428 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
429 "cc" => [other_user.ap_id],
430 "actor" => user.ap_id,
431 "object" => %{
432 "type" => "Note",
433 "content" => "hi everyone",
434 "attributedTo" => user.ap_id
435 }
436 }
437
438 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
439
440 assert other_user not in Notification.get_notified_from_activity(activity)
441 end
442
443 test "it does not send notification to mentioned users in likes" do
444 user = insert(:user)
445 other_user = insert(:user)
446 third_user = insert(:user)
447
448 {:ok, activity_one} =
449 CommonAPI.post(user, %{
450 "status" => "hey @#{other_user.nickname}!"
451 })
452
453 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
454
455 assert other_user not in Notification.get_notified_from_activity(activity_two)
456 end
457
458 test "it does not send notification to mentioned users in announces" do
459 user = insert(:user)
460 other_user = insert(:user)
461 third_user = insert(:user)
462
463 {:ok, activity_one} =
464 CommonAPI.post(user, %{
465 "status" => "hey @#{other_user.nickname}!"
466 })
467
468 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
469
470 assert other_user not in Notification.get_notified_from_activity(activity_two)
471 end
472 end
473
474 describe "notification lifecycle" do
475 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
476 user = insert(:user)
477 other_user = insert(:user)
478
479 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
480
481 assert Enum.empty?(Notification.for_user(user))
482
483 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
484
485 assert length(Notification.for_user(user)) == 1
486
487 {:ok, _} = CommonAPI.delete(activity.id, user)
488
489 assert Enum.empty?(Notification.for_user(user))
490 end
491
492 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
493 user = insert(:user)
494 other_user = insert(:user)
495
496 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
497
498 assert Enum.empty?(Notification.for_user(user))
499
500 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
501
502 assert length(Notification.for_user(user)) == 1
503
504 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
505
506 assert Enum.empty?(Notification.for_user(user))
507 end
508
509 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
510 user = insert(:user)
511 other_user = insert(:user)
512
513 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
514
515 assert Enum.empty?(Notification.for_user(user))
516
517 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
518
519 assert length(Notification.for_user(user)) == 1
520
521 {:ok, _} = CommonAPI.delete(activity.id, user)
522
523 assert Enum.empty?(Notification.for_user(user))
524 end
525
526 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
527 user = insert(:user)
528 other_user = insert(:user)
529
530 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
531
532 assert Enum.empty?(Notification.for_user(user))
533
534 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
535
536 assert length(Notification.for_user(user)) == 1
537
538 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
539
540 assert Enum.empty?(Notification.for_user(user))
541 end
542
543 test "liking an activity which is already deleted does not generate a notification" do
544 user = insert(:user)
545 other_user = insert(:user)
546
547 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
548
549 assert Enum.empty?(Notification.for_user(user))
550
551 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
552
553 assert Enum.empty?(Notification.for_user(user))
554
555 {:error, _} = CommonAPI.favorite(activity.id, other_user)
556
557 assert Enum.empty?(Notification.for_user(user))
558 end
559
560 test "repeating an activity which is already deleted does not generate a notification" do
561 user = insert(:user)
562 other_user = insert(:user)
563
564 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
565
566 assert Enum.empty?(Notification.for_user(user))
567
568 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
569
570 assert Enum.empty?(Notification.for_user(user))
571
572 {:error, _} = CommonAPI.repeat(activity.id, other_user)
573
574 assert Enum.empty?(Notification.for_user(user))
575 end
576
577 test "replying to a deleted post without tagging does not generate a notification" do
578 user = insert(:user)
579 other_user = insert(:user)
580
581 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
582 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
583
584 {:ok, _reply_activity} =
585 CommonAPI.post(other_user, %{
586 "status" => "test reply",
587 "in_reply_to_status_id" => activity.id
588 })
589
590 assert Enum.empty?(Notification.for_user(user))
591 end
592 end
593
594 describe "for_user" do
595 test "it returns notifications for muted user without notifications" do
596 user = insert(:user)
597 muted = insert(:user)
598 {:ok, user} = User.mute(user, muted, false)
599
600 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
601
602 assert length(Notification.for_user(user)) == 1
603 end
604
605 test "it doesn't return notifications for muted user with notifications" do
606 user = insert(:user)
607 muted = insert(:user)
608 {:ok, user} = User.mute(user, muted)
609
610 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
611
612 assert Notification.for_user(user) == []
613 end
614
615 test "it doesn't return notifications for blocked user" do
616 user = insert(:user)
617 blocked = insert(:user)
618 {:ok, user} = User.block(user, blocked)
619
620 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
621
622 assert Notification.for_user(user) == []
623 end
624
625 test "it doesn't return notificatitons for blocked domain" do
626 user = insert(:user)
627 blocked = insert(:user, ap_id: "http://some-domain.com")
628 {:ok, user} = User.block_domain(user, "some-domain.com")
629
630 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
631
632 assert Notification.for_user(user) == []
633 end
634
635 test "it doesn't return notifications for muted thread" do
636 user = insert(:user)
637 another_user = insert(:user)
638
639 {:ok, activity} =
640 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
641
642 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
643 assert Notification.for_user(user) == []
644 end
645
646 test "it returns notifications for muted user with notifications and with_muted parameter" do
647 user = insert(:user)
648 muted = insert(:user)
649 {:ok, user} = User.mute(user, muted)
650
651 {:ok, _activity} = TwitterAPI.create_status(muted, %{"status" => "hey @#{user.nickname}"})
652
653 assert length(Notification.for_user(user, %{with_muted: true})) == 1
654 end
655
656 test "it returns notifications for blocked user and with_muted parameter" do
657 user = insert(:user)
658 blocked = insert(:user)
659 {:ok, user} = User.block(user, blocked)
660
661 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
662
663 assert length(Notification.for_user(user, %{with_muted: true})) == 1
664 end
665
666 test "it returns notificatitons for blocked domain and with_muted parameter" do
667 user = insert(:user)
668 blocked = insert(:user, ap_id: "http://some-domain.com")
669 {:ok, user} = User.block_domain(user, "some-domain.com")
670
671 {:ok, _activity} = TwitterAPI.create_status(blocked, %{"status" => "hey @#{user.nickname}"})
672
673 assert length(Notification.for_user(user, %{with_muted: true})) == 1
674 end
675
676 test "it returns notifications for muted thread with_muted parameter" do
677 user = insert(:user)
678 another_user = insert(:user)
679
680 {:ok, activity} =
681 TwitterAPI.create_status(another_user, %{"status" => "hey @#{user.nickname}"})
682
683 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
684 assert length(Notification.for_user(user, %{with_muted: true})) == 1
685 end
686 end
687 end