27d8cace706aac541da9023d2169ab4def35f2bb
[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 "notification target determination" do
335 test "it sends notifications to addressed users in new messages" do
336 user = insert(:user)
337 other_user = insert(:user)
338
339 {:ok, activity} =
340 CommonAPI.post(user, %{
341 "status" => "hey @#{other_user.nickname}!"
342 })
343
344 assert other_user in Notification.get_notified_from_activity(activity)
345 end
346
347 test "it sends notifications to mentioned users in new messages" do
348 user = insert(:user)
349 other_user = insert(:user)
350
351 create_activity = %{
352 "@context" => "https://www.w3.org/ns/activitystreams",
353 "type" => "Create",
354 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
355 "actor" => user.ap_id,
356 "object" => %{
357 "type" => "Note",
358 "content" => "message with a Mention tag, but no explicit tagging",
359 "tag" => [
360 %{
361 "type" => "Mention",
362 "href" => other_user.ap_id,
363 "name" => other_user.nickname
364 }
365 ],
366 "attributedTo" => user.ap_id
367 }
368 }
369
370 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
371
372 assert other_user in Notification.get_notified_from_activity(activity)
373 end
374
375 test "it does not send notifications to users who are only cc in new messages" do
376 user = insert(:user)
377 other_user = insert(:user)
378
379 create_activity = %{
380 "@context" => "https://www.w3.org/ns/activitystreams",
381 "type" => "Create",
382 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
383 "cc" => [other_user.ap_id],
384 "actor" => user.ap_id,
385 "object" => %{
386 "type" => "Note",
387 "content" => "hi everyone",
388 "attributedTo" => user.ap_id
389 }
390 }
391
392 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
393
394 assert other_user not in Notification.get_notified_from_activity(activity)
395 end
396
397 test "it does not send notification to mentioned users in likes" do
398 user = insert(:user)
399 other_user = insert(:user)
400 third_user = insert(:user)
401
402 {:ok, activity_one} =
403 CommonAPI.post(user, %{
404 "status" => "hey @#{other_user.nickname}!"
405 })
406
407 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
408
409 assert other_user not in Notification.get_notified_from_activity(activity_two)
410 end
411
412 test "it does not send notification to mentioned users in announces" do
413 user = insert(:user)
414 other_user = insert(:user)
415 third_user = insert(:user)
416
417 {:ok, activity_one} =
418 CommonAPI.post(user, %{
419 "status" => "hey @#{other_user.nickname}!"
420 })
421
422 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
423
424 assert other_user not in Notification.get_notified_from_activity(activity_two)
425 end
426 end
427
428 describe "notification lifecycle" do
429 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
430 user = insert(:user)
431 other_user = insert(:user)
432
433 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
434
435 assert Enum.empty?(Notification.for_user(user))
436
437 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
438
439 assert length(Notification.for_user(user)) == 1
440
441 {:ok, _} = CommonAPI.delete(activity.id, user)
442
443 assert Enum.empty?(Notification.for_user(user))
444 end
445
446 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
447 user = insert(:user)
448 other_user = insert(:user)
449
450 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
451
452 assert Enum.empty?(Notification.for_user(user))
453
454 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
455
456 assert length(Notification.for_user(user)) == 1
457
458 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
459
460 assert Enum.empty?(Notification.for_user(user))
461 end
462
463 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
464 user = insert(:user)
465 other_user = insert(:user)
466
467 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
468
469 assert Enum.empty?(Notification.for_user(user))
470
471 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
472
473 assert length(Notification.for_user(user)) == 1
474
475 {:ok, _} = CommonAPI.delete(activity.id, user)
476
477 assert Enum.empty?(Notification.for_user(user))
478 end
479
480 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
481 user = insert(:user)
482 other_user = insert(:user)
483
484 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
485
486 assert Enum.empty?(Notification.for_user(user))
487
488 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
489
490 assert length(Notification.for_user(user)) == 1
491
492 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
493
494 assert Enum.empty?(Notification.for_user(user))
495 end
496
497 test "liking an activity which is already deleted does not generate a notification" do
498 user = insert(:user)
499 other_user = insert(:user)
500
501 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
502
503 assert Enum.empty?(Notification.for_user(user))
504
505 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
506
507 assert Enum.empty?(Notification.for_user(user))
508
509 {:error, _} = CommonAPI.favorite(activity.id, other_user)
510
511 assert Enum.empty?(Notification.for_user(user))
512 end
513
514 test "repeating an activity which is already deleted does not generate a notification" do
515 user = insert(:user)
516 other_user = insert(:user)
517
518 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
519
520 assert Enum.empty?(Notification.for_user(user))
521
522 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
523
524 assert Enum.empty?(Notification.for_user(user))
525
526 {:error, _} = CommonAPI.repeat(activity.id, other_user)
527
528 assert Enum.empty?(Notification.for_user(user))
529 end
530
531 test "replying to a deleted post without tagging does not generate a notification" do
532 user = insert(:user)
533 other_user = insert(:user)
534
535 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
536 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
537
538 {:ok, _reply_activity} =
539 CommonAPI.post(other_user, %{
540 "status" => "test reply",
541 "in_reply_to_status_id" => activity.id
542 })
543
544 assert Enum.empty?(Notification.for_user(user))
545 end
546 end
547 end