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