Merge branch 'develop' into issue/1276
[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
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{} =
342 m =
343 Pleroma.Repo.get_by(
344 Pleroma.Marker,
345 user_id: other_user.id,
346 timeline: "notifications"
347 )
348
349 assert m.last_read_id == to_string(n2.id)
350 end
351 end
352
353 describe "for_user_since/2" do
354 defp days_ago(days) do
355 NaiveDateTime.add(
356 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
357 -days * 60 * 60 * 24,
358 :second
359 )
360 end
361
362 test "Returns recent notifications" do
363 user1 = insert(:user)
364 user2 = insert(:user)
365
366 Enum.each(0..10, fn i ->
367 {:ok, _activity} =
368 CommonAPI.post(user1, %{
369 "status" => "hey ##{i} @#{user2.nickname}!"
370 })
371 end)
372
373 {old, new} = Enum.split(Notification.for_user(user2), 5)
374
375 Enum.each(old, fn notification ->
376 notification
377 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
378 |> Pleroma.Repo.update!()
379 end)
380
381 recent_notifications_ids =
382 user2
383 |> Notification.for_user_since(
384 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
385 )
386 |> Enum.map(& &1.id)
387
388 Enum.each(old, fn %{id: id} ->
389 refute id in recent_notifications_ids
390 end)
391
392 Enum.each(new, fn %{id: id} ->
393 assert id in recent_notifications_ids
394 end)
395 end
396 end
397
398 describe "notification target determination" do
399 test "it sends notifications to addressed users in new messages" do
400 user = insert(:user)
401 other_user = insert(:user)
402
403 {:ok, activity} =
404 CommonAPI.post(user, %{
405 "status" => "hey @#{other_user.nickname}!"
406 })
407
408 assert other_user in Notification.get_notified_from_activity(activity)
409 end
410
411 test "it sends notifications to mentioned users in new messages" do
412 user = insert(:user)
413 other_user = insert(:user)
414
415 create_activity = %{
416 "@context" => "https://www.w3.org/ns/activitystreams",
417 "type" => "Create",
418 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
419 "actor" => user.ap_id,
420 "object" => %{
421 "type" => "Note",
422 "content" => "message with a Mention tag, but no explicit tagging",
423 "tag" => [
424 %{
425 "type" => "Mention",
426 "href" => other_user.ap_id,
427 "name" => other_user.nickname
428 }
429 ],
430 "attributedTo" => user.ap_id
431 }
432 }
433
434 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
435
436 assert other_user in Notification.get_notified_from_activity(activity)
437 end
438
439 test "it does not send notifications to users who are only cc in new messages" do
440 user = insert(:user)
441 other_user = insert(:user)
442
443 create_activity = %{
444 "@context" => "https://www.w3.org/ns/activitystreams",
445 "type" => "Create",
446 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
447 "cc" => [other_user.ap_id],
448 "actor" => user.ap_id,
449 "object" => %{
450 "type" => "Note",
451 "content" => "hi everyone",
452 "attributedTo" => user.ap_id
453 }
454 }
455
456 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
457
458 assert other_user not in Notification.get_notified_from_activity(activity)
459 end
460
461 test "it does not send notification to mentioned users in likes" do
462 user = insert(:user)
463 other_user = insert(:user)
464 third_user = insert(:user)
465
466 {:ok, activity_one} =
467 CommonAPI.post(user, %{
468 "status" => "hey @#{other_user.nickname}!"
469 })
470
471 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
472
473 assert other_user not in Notification.get_notified_from_activity(activity_two)
474 end
475
476 test "it does not send notification to mentioned users in announces" do
477 user = insert(:user)
478 other_user = insert(:user)
479 third_user = insert(:user)
480
481 {:ok, activity_one} =
482 CommonAPI.post(user, %{
483 "status" => "hey @#{other_user.nickname}!"
484 })
485
486 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
487
488 assert other_user not in Notification.get_notified_from_activity(activity_two)
489 end
490 end
491
492 describe "notification lifecycle" do
493 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
494 user = insert(:user)
495 other_user = insert(:user)
496
497 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
498
499 assert Enum.empty?(Notification.for_user(user))
500
501 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
502
503 assert length(Notification.for_user(user)) == 1
504
505 {:ok, _} = CommonAPI.delete(activity.id, user)
506
507 assert Enum.empty?(Notification.for_user(user))
508 end
509
510 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
511 user = insert(:user)
512 other_user = insert(:user)
513
514 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
515
516 assert Enum.empty?(Notification.for_user(user))
517
518 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
519
520 assert length(Notification.for_user(user)) == 1
521
522 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
523
524 assert Enum.empty?(Notification.for_user(user))
525 end
526
527 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
528 user = insert(:user)
529 other_user = insert(:user)
530
531 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
532
533 assert Enum.empty?(Notification.for_user(user))
534
535 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
536
537 assert length(Notification.for_user(user)) == 1
538
539 {:ok, _} = CommonAPI.delete(activity.id, user)
540
541 assert Enum.empty?(Notification.for_user(user))
542 end
543
544 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
545 user = insert(:user)
546 other_user = insert(:user)
547
548 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
549
550 assert Enum.empty?(Notification.for_user(user))
551
552 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
553
554 assert length(Notification.for_user(user)) == 1
555
556 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
557
558 assert Enum.empty?(Notification.for_user(user))
559 end
560
561 test "liking an activity which is already deleted does not generate a notification" do
562 user = insert(:user)
563 other_user = insert(:user)
564
565 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
566
567 assert Enum.empty?(Notification.for_user(user))
568
569 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
570
571 assert Enum.empty?(Notification.for_user(user))
572
573 {:error, _} = CommonAPI.favorite(activity.id, other_user)
574
575 assert Enum.empty?(Notification.for_user(user))
576 end
577
578 test "repeating an activity which is already deleted does not generate a notification" do
579 user = insert(:user)
580 other_user = insert(:user)
581
582 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
583
584 assert Enum.empty?(Notification.for_user(user))
585
586 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
587
588 assert Enum.empty?(Notification.for_user(user))
589
590 {:error, _} = CommonAPI.repeat(activity.id, other_user)
591
592 assert Enum.empty?(Notification.for_user(user))
593 end
594
595 test "replying to a deleted post without tagging does not generate a notification" do
596 user = insert(:user)
597 other_user = insert(:user)
598
599 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
600 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
601
602 {:ok, _reply_activity} =
603 CommonAPI.post(other_user, %{
604 "status" => "test reply",
605 "in_reply_to_status_id" => activity.id
606 })
607
608 assert Enum.empty?(Notification.for_user(user))
609 end
610
611 test "notifications are deleted if a local user is deleted" do
612 user = insert(:user)
613 other_user = insert(:user)
614
615 {:ok, _activity} =
616 CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
617
618 refute Enum.empty?(Notification.for_user(other_user))
619
620 {:ok, job} = User.delete(user)
621 ObanHelpers.perform(job)
622
623 assert Enum.empty?(Notification.for_user(other_user))
624 end
625
626 test "notifications are deleted if a remote user is deleted" do
627 remote_user = insert(:user)
628 local_user = insert(:user)
629
630 dm_message = %{
631 "@context" => "https://www.w3.org/ns/activitystreams",
632 "type" => "Create",
633 "actor" => remote_user.ap_id,
634 "id" => remote_user.ap_id <> "/activities/test",
635 "to" => [local_user.ap_id],
636 "cc" => [],
637 "object" => %{
638 "type" => "Note",
639 "content" => "Hello!",
640 "tag" => [
641 %{
642 "type" => "Mention",
643 "href" => local_user.ap_id,
644 "name" => "@#{local_user.nickname}"
645 }
646 ],
647 "to" => [local_user.ap_id],
648 "cc" => [],
649 "attributedTo" => remote_user.ap_id
650 }
651 }
652
653 {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
654
655 refute Enum.empty?(Notification.for_user(local_user))
656
657 delete_user_message = %{
658 "@context" => "https://www.w3.org/ns/activitystreams",
659 "id" => remote_user.ap_id <> "/activities/delete",
660 "actor" => remote_user.ap_id,
661 "type" => "Delete",
662 "object" => remote_user.ap_id
663 }
664
665 {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
666 ObanHelpers.perform_all()
667
668 assert Enum.empty?(Notification.for_user(local_user))
669 end
670
671 test "move activity generates a notification" do
672 %{ap_id: old_ap_id} = old_user = insert(:user)
673 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
674 follower = insert(:user)
675 other_follower = insert(:user, %{allow_following_move: false})
676
677 User.follow(follower, old_user)
678 User.follow(other_follower, old_user)
679
680 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
681 ObanHelpers.perform_all()
682
683 assert [] = Notification.for_user(follower)
684
685 assert [
686 %{
687 activity: %{
688 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
689 }
690 }
691 ] = Notification.for_user(follower, %{with_move: true})
692
693 assert [] = Notification.for_user(other_follower)
694
695 assert [
696 %{
697 activity: %{
698 data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
699 }
700 }
701 ] = Notification.for_user(other_follower, %{with_move: true})
702 end
703 end
704
705 describe "for_user" do
706 test "it returns notifications for muted user without notifications" do
707 user = insert(:user)
708 muted = insert(:user)
709 {:ok, _user_relationships} = User.mute(user, muted, false)
710
711 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
712
713 assert length(Notification.for_user(user)) == 1
714 end
715
716 test "it doesn't return notifications for muted user with notifications" do
717 user = insert(:user)
718 muted = insert(:user)
719 {:ok, _user_relationships} = User.mute(user, muted)
720
721 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
722
723 assert Notification.for_user(user) == []
724 end
725
726 test "it doesn't return notifications for blocked user" do
727 user = insert(:user)
728 blocked = insert(:user)
729 {:ok, _user_relationship} = User.block(user, blocked)
730
731 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
732
733 assert Notification.for_user(user) == []
734 end
735
736 test "it doesn't return notificatitons for blocked domain" do
737 user = insert(:user)
738 blocked = insert(:user, ap_id: "http://some-domain.com")
739 {:ok, user} = User.block_domain(user, "some-domain.com")
740
741 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
742
743 assert Notification.for_user(user) == []
744 end
745
746 test "it doesn't return notifications for muted thread" do
747 user = insert(:user)
748 another_user = insert(:user)
749
750 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
751
752 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
753 assert Notification.for_user(user) == []
754 end
755
756 test "it returns notifications from a muted user when with_muted is set" do
757 user = insert(:user)
758 muted = insert(:user)
759 {:ok, _user_relationships} = User.mute(user, muted)
760
761 {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
762
763 assert length(Notification.for_user(user, %{with_muted: true})) == 1
764 end
765
766 test "it doesn't return notifications from a blocked user when with_muted is set" do
767 user = insert(:user)
768 blocked = insert(:user)
769 {:ok, _user_relationship} = User.block(user, blocked)
770
771 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
772
773 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
774 end
775
776 test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
777 user = insert(:user)
778 blocked = insert(:user, ap_id: "http://some-domain.com")
779 {:ok, user} = User.block_domain(user, "some-domain.com")
780
781 {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
782
783 assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
784 end
785
786 test "it returns notifications from muted threads when with_muted is set" do
787 user = insert(:user)
788 another_user = insert(:user)
789
790 {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
791
792 {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
793 assert length(Notification.for_user(user, %{with_muted: true})) == 1
794 end
795 end
796 end