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