Merge 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
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 people on remote instances" do
84 user = insert(:user, info: %{notification_settings: %{"remote" => false}})
85 other_user = insert(:user)
86
87 create_activity = %{
88 "@context" => "https://www.w3.org/ns/activitystreams",
89 "type" => "Create",
90 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
91 "actor" => other_user.ap_id,
92 "object" => %{
93 "type" => "Note",
94 "content" => "Hi @#{user.nickname}",
95 "attributedTo" => other_user.ap_id
96 }
97 }
98
99 {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
100 assert nil == Notification.create_notification(activity, user)
101 end
102
103 test "it disables notifications from people on the local instance" do
104 user = insert(:user, info: %{notification_settings: %{"local" => false}})
105 other_user = insert(:user)
106 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
107 assert nil == Notification.create_notification(activity, user)
108 end
109
110 test "it disables notifications from followers" do
111 follower = insert(:user)
112 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
113 User.follow(follower, followed)
114 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
115 assert nil == Notification.create_notification(activity, followed)
116 end
117
118 test "it disables notifications from people the user follows" do
119 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
120 followed = insert(:user)
121 User.follow(follower, followed)
122 follower = Repo.get(User, follower.id)
123 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
124 assert nil == Notification.create_notification(activity, follower)
125 end
126
127 test "it doesn't create a notification for user if he is the activity author" do
128 activity = insert(:note_activity)
129 author = User.get_cached_by_ap_id(activity.data["actor"])
130
131 assert nil == Notification.create_notification(activity, author)
132 end
133
134 test "it doesn't create a notification for follow-unfollow-follow chains" do
135 user = insert(:user)
136 followed_user = insert(:user)
137 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
138 Notification.create_notification(activity, followed_user)
139 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
140 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
141 assert nil == Notification.create_notification(activity_dupe, followed_user)
142 end
143
144 test "it doesn't create a notification for like-unlike-like chains" do
145 user = insert(:user)
146 liked_user = insert(:user)
147 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
148 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
149 Notification.create_notification(fav_status, liked_user)
150 TwitterAPI.unfav(user, status.id)
151 {:ok, dupe} = TwitterAPI.fav(user, status.id)
152 assert nil == Notification.create_notification(dupe, liked_user)
153 end
154
155 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
156 user = insert(:user)
157 retweeted_user = insert(:user)
158
159 {:ok, status} =
160 TwitterAPI.create_status(retweeted_user, %{
161 "status" => "Send dupe notifications to the shadow realm"
162 })
163
164 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
165 Notification.create_notification(retweeted_activity, retweeted_user)
166 TwitterAPI.unrepeat(user, status.id)
167 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
168 assert nil == Notification.create_notification(dupe, retweeted_user)
169 end
170
171 test "it doesn't create duplicate notifications for follow+subscribed users" do
172 user = insert(:user)
173 subscriber = insert(:user)
174
175 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
176 User.subscribe(subscriber, user)
177 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
178 {:ok, [_notif]} = Notification.create_notifications(status)
179 end
180
181 test "it doesn't create subscription notifications if the recipient cannot see the status" do
182 user = insert(:user)
183 subscriber = insert(:user)
184
185 User.subscribe(subscriber, user)
186
187 {:ok, status} =
188 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
189
190 assert {:ok, []} == Notification.create_notifications(status)
191 end
192 end
193
194 describe "get notification" do
195 test "it gets a notification that belongs 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 {:ok, notification} = Notification.get(other_user, notification.id)
204
205 assert notification.user_id == other_user.id
206 end
207
208 test "it returns error if the notification doesn't belong 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 {:error, _notification} = Notification.get(user, notification.id)
217 end
218 end
219
220 describe "dismiss notification" do
221 test "it dismisses 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.dismiss(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.dismiss(user, notification.id)
243 end
244 end
245
246 describe "clear notification" do
247 test "it clears all notifications belonging to the user" do
248 user = insert(:user)
249 other_user = insert(:user)
250 third_user = insert(:user)
251
252 {:ok, activity} =
253 TwitterAPI.create_status(user, %{
254 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
255 })
256
257 {:ok, _notifs} = Notification.create_notifications(activity)
258
259 {:ok, activity} =
260 TwitterAPI.create_status(user, %{
261 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
262 })
263
264 {:ok, _notifs} = Notification.create_notifications(activity)
265 Notification.clear(other_user)
266
267 assert Notification.for_user(other_user) == []
268 assert Notification.for_user(third_user) != []
269 end
270 end
271
272 describe "set_read_up_to()" do
273 test "it sets all notifications as read up to a specified notification ID" do
274 user = insert(:user)
275 other_user = insert(:user)
276
277 {:ok, _activity} =
278 TwitterAPI.create_status(user, %{
279 "status" => "hey @#{other_user.nickname}!"
280 })
281
282 {:ok, _activity} =
283 TwitterAPI.create_status(user, %{
284 "status" => "hey again @#{other_user.nickname}!"
285 })
286
287 [n2, n1] = notifs = Notification.for_user(other_user)
288 assert length(notifs) == 2
289
290 assert n2.id > n1.id
291
292 {:ok, _activity} =
293 TwitterAPI.create_status(user, %{
294 "status" => "hey yet again @#{other_user.nickname}!"
295 })
296
297 Notification.set_read_up_to(other_user, n2.id)
298
299 [n3, n2, n1] = Notification.for_user(other_user)
300
301 assert n1.seen == true
302 assert n2.seen == true
303 assert n3.seen == false
304 end
305 end
306
307 describe "for_user_since/2" do
308 defp days_ago(days) do
309 NaiveDateTime.add(
310 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
311 -days * 60 * 60 * 24,
312 :second
313 )
314 end
315
316 test "Returns recent notifications" do
317 user1 = insert(:user)
318 user2 = insert(:user)
319
320 Enum.each(0..10, fn i ->
321 {:ok, _activity} =
322 CommonAPI.post(user1, %{
323 "status" => "hey ##{i} @#{user2.nickname}!"
324 })
325 end)
326
327 {old, new} = Enum.split(Notification.for_user(user2), 5)
328
329 Enum.each(old, fn notification ->
330 notification
331 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
332 |> Pleroma.Repo.update!()
333 end)
334
335 recent_notifications_ids =
336 user2
337 |> Notification.for_user_since(
338 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
339 )
340 |> Enum.map(& &1.id)
341
342 Enum.each(old, fn %{id: id} ->
343 refute id in recent_notifications_ids
344 end)
345
346 Enum.each(new, fn %{id: id} ->
347 assert id in recent_notifications_ids
348 end)
349 end
350 end
351
352 describe "notification target determination" do
353 test "it sends notifications to addressed users in new messages" do
354 user = insert(:user)
355 other_user = insert(:user)
356
357 {:ok, activity} =
358 CommonAPI.post(user, %{
359 "status" => "hey @#{other_user.nickname}!"
360 })
361
362 assert other_user in Notification.get_notified_from_activity(activity)
363 end
364
365 test "it sends notifications to mentioned users in new messages" do
366 user = insert(:user)
367 other_user = insert(:user)
368
369 create_activity = %{
370 "@context" => "https://www.w3.org/ns/activitystreams",
371 "type" => "Create",
372 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
373 "actor" => user.ap_id,
374 "object" => %{
375 "type" => "Note",
376 "content" => "message with a Mention tag, but no explicit tagging",
377 "tag" => [
378 %{
379 "type" => "Mention",
380 "href" => other_user.ap_id,
381 "name" => other_user.nickname
382 }
383 ],
384 "attributedTo" => user.ap_id
385 }
386 }
387
388 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
389
390 assert other_user in Notification.get_notified_from_activity(activity)
391 end
392
393 test "it does not send notifications to users who are only cc in new messages" do
394 user = insert(:user)
395 other_user = insert(:user)
396
397 create_activity = %{
398 "@context" => "https://www.w3.org/ns/activitystreams",
399 "type" => "Create",
400 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
401 "cc" => [other_user.ap_id],
402 "actor" => user.ap_id,
403 "object" => %{
404 "type" => "Note",
405 "content" => "hi everyone",
406 "attributedTo" => user.ap_id
407 }
408 }
409
410 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
411
412 assert other_user not in Notification.get_notified_from_activity(activity)
413 end
414
415 test "it does not send notification to mentioned users in likes" do
416 user = insert(:user)
417 other_user = insert(:user)
418 third_user = insert(:user)
419
420 {:ok, activity_one} =
421 CommonAPI.post(user, %{
422 "status" => "hey @#{other_user.nickname}!"
423 })
424
425 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
426
427 assert other_user not in Notification.get_notified_from_activity(activity_two)
428 end
429
430 test "it does not send notification to mentioned users in announces" do
431 user = insert(:user)
432 other_user = insert(:user)
433 third_user = insert(:user)
434
435 {:ok, activity_one} =
436 CommonAPI.post(user, %{
437 "status" => "hey @#{other_user.nickname}!"
438 })
439
440 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
441
442 assert other_user not in Notification.get_notified_from_activity(activity_two)
443 end
444 end
445
446 describe "notification lifecycle" do
447 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
448 user = insert(:user)
449 other_user = insert(:user)
450
451 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
452
453 assert Enum.empty?(Notification.for_user(user))
454
455 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
456
457 assert length(Notification.for_user(user)) == 1
458
459 {:ok, _} = CommonAPI.delete(activity.id, user)
460
461 assert Enum.empty?(Notification.for_user(user))
462 end
463
464 test "liking an activity results in 1 notification, then 0 if the activity is unliked" 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.unfavorite(activity.id, other_user)
477
478 assert Enum.empty?(Notification.for_user(user))
479 end
480
481 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" 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.repeat(activity.id, other_user)
490
491 assert length(Notification.for_user(user)) == 1
492
493 {:ok, _} = CommonAPI.delete(activity.id, 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 unrepeated" 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.unrepeat(activity.id, other_user)
511
512 assert Enum.empty?(Notification.for_user(user))
513 end
514
515 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
524
525 assert Enum.empty?(Notification.for_user(user))
526
527 {:error, _} = CommonAPI.favorite(activity.id, other_user)
528
529 assert Enum.empty?(Notification.for_user(user))
530 end
531
532 test "repeating 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.repeat(activity.id, other_user)
545
546 assert Enum.empty?(Notification.for_user(user))
547 end
548
549 test "replying to a deleted post without tagging 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 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
555
556 {:ok, _reply_activity} =
557 CommonAPI.post(other_user, %{
558 "status" => "test reply",
559 "in_reply_to_status_id" => activity.id
560 })
561
562 assert Enum.empty?(Notification.for_user(user))
563 end
564 end
565 end