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