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