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