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