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