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