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