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