b54414dcd1085ad390c01278ffe75ef6239aeb71
[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_cached_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 non-followers" do
117 follower = insert(:user)
118 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
119 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
120 assert nil == Notification.create_notification(activity, followed)
121 end
122
123 test "it disables notifications from people the user follows" do
124 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
125 followed = insert(:user)
126 User.follow(follower, followed)
127 follower = Repo.get(User, follower.id)
128 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
129 assert nil == Notification.create_notification(activity, follower)
130 end
131
132 test "it disables notifications from people the user does not follow" do
133 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
134 followed = insert(:user)
135 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
136 assert nil == Notification.create_notification(activity, follower)
137 end
138
139 test "it doesn't create a notification for user if he is the activity author" do
140 activity = insert(:note_activity)
141 author = User.get_cached_by_ap_id(activity.data["actor"])
142
143 assert nil == Notification.create_notification(activity, author)
144 end
145
146 test "it doesn't create a notification for follow-unfollow-follow chains" do
147 user = insert(:user)
148 followed_user = insert(:user)
149 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
150 Notification.create_notification(activity, followed_user)
151 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
152 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
153 assert nil == Notification.create_notification(activity_dupe, followed_user)
154 end
155
156 test "it doesn't create a notification for like-unlike-like chains" do
157 user = insert(:user)
158 liked_user = insert(:user)
159 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
160 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
161 Notification.create_notification(fav_status, liked_user)
162 TwitterAPI.unfav(user, status.id)
163 {:ok, dupe} = TwitterAPI.fav(user, status.id)
164 assert nil == Notification.create_notification(dupe, liked_user)
165 end
166
167 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
168 user = insert(:user)
169 retweeted_user = insert(:user)
170
171 {:ok, status} =
172 TwitterAPI.create_status(retweeted_user, %{
173 "status" => "Send dupe notifications to the shadow realm"
174 })
175
176 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
177 Notification.create_notification(retweeted_activity, retweeted_user)
178 TwitterAPI.unrepeat(user, status.id)
179 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
180 assert nil == Notification.create_notification(dupe, retweeted_user)
181 end
182
183 test "it doesn't create duplicate notifications for follow+subscribed users" do
184 user = insert(:user)
185 subscriber = insert(:user)
186
187 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
188 User.subscribe(subscriber, user)
189 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
190 {:ok, [_notif]} = Notification.create_notifications(status)
191 end
192
193 test "it doesn't create subscription notifications if the recipient cannot see the status" do
194 user = insert(:user)
195 subscriber = insert(:user)
196
197 User.subscribe(subscriber, user)
198
199 {:ok, status} =
200 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
201
202 assert {:ok, []} == Notification.create_notifications(status)
203 end
204 end
205
206 describe "get notification" do
207 test "it gets a notification that belongs to the user" do
208 user = insert(:user)
209 other_user = insert(:user)
210
211 {:ok, activity} =
212 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
213
214 {:ok, [notification]} = Notification.create_notifications(activity)
215 {:ok, notification} = Notification.get(other_user, notification.id)
216
217 assert notification.user_id == other_user.id
218 end
219
220 test "it returns error if the notification doesn't belong to the user" do
221 user = insert(:user)
222 other_user = insert(:user)
223
224 {:ok, activity} =
225 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
226
227 {:ok, [notification]} = Notification.create_notifications(activity)
228 {:error, _notification} = Notification.get(user, notification.id)
229 end
230 end
231
232 describe "dismiss notification" do
233 test "it dismisses a notification that belongs to the user" do
234 user = insert(:user)
235 other_user = insert(:user)
236
237 {:ok, activity} =
238 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
239
240 {:ok, [notification]} = Notification.create_notifications(activity)
241 {:ok, notification} = Notification.dismiss(other_user, notification.id)
242
243 assert notification.user_id == other_user.id
244 end
245
246 test "it returns error if the notification doesn't belong to the user" do
247 user = insert(:user)
248 other_user = insert(:user)
249
250 {:ok, activity} =
251 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
252
253 {:ok, [notification]} = Notification.create_notifications(activity)
254 {:error, _notification} = Notification.dismiss(user, notification.id)
255 end
256 end
257
258 describe "clear notification" do
259 test "it clears all notifications belonging to the user" do
260 user = insert(:user)
261 other_user = insert(:user)
262 third_user = insert(:user)
263
264 {:ok, activity} =
265 TwitterAPI.create_status(user, %{
266 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
267 })
268
269 {:ok, _notifs} = Notification.create_notifications(activity)
270
271 {:ok, activity} =
272 TwitterAPI.create_status(user, %{
273 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
274 })
275
276 {:ok, _notifs} = Notification.create_notifications(activity)
277 Notification.clear(other_user)
278
279 assert Notification.for_user(other_user) == []
280 assert Notification.for_user(third_user) != []
281 end
282 end
283
284 describe "set_read_up_to()" do
285 test "it sets all notifications as read up to a specified notification ID" do
286 user = insert(:user)
287 other_user = insert(:user)
288
289 {:ok, _activity} =
290 TwitterAPI.create_status(user, %{
291 "status" => "hey @#{other_user.nickname}!"
292 })
293
294 {:ok, _activity} =
295 TwitterAPI.create_status(user, %{
296 "status" => "hey again @#{other_user.nickname}!"
297 })
298
299 [n2, n1] = notifs = Notification.for_user(other_user)
300 assert length(notifs) == 2
301
302 assert n2.id > n1.id
303
304 {:ok, _activity} =
305 TwitterAPI.create_status(user, %{
306 "status" => "hey yet again @#{other_user.nickname}!"
307 })
308
309 Notification.set_read_up_to(other_user, n2.id)
310
311 [n3, n2, n1] = Notification.for_user(other_user)
312
313 assert n1.seen == true
314 assert n2.seen == true
315 assert n3.seen == false
316 end
317 end
318
319 describe "notification target determination" do
320 test "it sends notifications to addressed users in new messages" do
321 user = insert(:user)
322 other_user = insert(:user)
323
324 {:ok, activity} =
325 CommonAPI.post(user, %{
326 "status" => "hey @#{other_user.nickname}!"
327 })
328
329 assert other_user in Notification.get_notified_from_activity(activity)
330 end
331
332 test "it sends notifications to mentioned users in new messages" do
333 user = insert(:user)
334 other_user = insert(:user)
335
336 create_activity = %{
337 "@context" => "https://www.w3.org/ns/activitystreams",
338 "type" => "Create",
339 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
340 "actor" => user.ap_id,
341 "object" => %{
342 "type" => "Note",
343 "content" => "message with a Mention tag, but no explicit tagging",
344 "tag" => [
345 %{
346 "type" => "Mention",
347 "href" => other_user.ap_id,
348 "name" => other_user.nickname
349 }
350 ],
351 "attributedTo" => user.ap_id
352 }
353 }
354
355 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
356
357 assert other_user in Notification.get_notified_from_activity(activity)
358 end
359
360 test "it does not send notifications to users who are only cc in new messages" do
361 user = insert(:user)
362 other_user = insert(:user)
363
364 create_activity = %{
365 "@context" => "https://www.w3.org/ns/activitystreams",
366 "type" => "Create",
367 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
368 "cc" => [other_user.ap_id],
369 "actor" => user.ap_id,
370 "object" => %{
371 "type" => "Note",
372 "content" => "hi everyone",
373 "attributedTo" => user.ap_id
374 }
375 }
376
377 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
378
379 assert other_user not in Notification.get_notified_from_activity(activity)
380 end
381
382 test "it does not send notification to mentioned users in likes" do
383 user = insert(:user)
384 other_user = insert(:user)
385 third_user = insert(:user)
386
387 {:ok, activity_one} =
388 CommonAPI.post(user, %{
389 "status" => "hey @#{other_user.nickname}!"
390 })
391
392 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
393
394 assert other_user not in Notification.get_notified_from_activity(activity_two)
395 end
396
397 test "it does not send notification to mentioned users in announces" 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.repeat(activity_one.id, third_user)
408
409 assert other_user not in Notification.get_notified_from_activity(activity_two)
410 end
411 end
412
413 describe "notification lifecycle" do
414 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
415 user = insert(:user)
416 other_user = insert(:user)
417
418 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
419
420 assert Enum.empty?(Notification.for_user(user))
421
422 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
423
424 assert length(Notification.for_user(user)) == 1
425
426 {:ok, _} = CommonAPI.delete(activity.id, user)
427
428 assert Enum.empty?(Notification.for_user(user))
429 end
430
431 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
432 user = insert(:user)
433 other_user = insert(:user)
434
435 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
436
437 assert Enum.empty?(Notification.for_user(user))
438
439 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
440
441 assert length(Notification.for_user(user)) == 1
442
443 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
444
445 assert Enum.empty?(Notification.for_user(user))
446 end
447
448 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
449 user = insert(:user)
450 other_user = insert(:user)
451
452 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
453
454 assert Enum.empty?(Notification.for_user(user))
455
456 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
457
458 assert length(Notification.for_user(user)) == 1
459
460 {:ok, _} = CommonAPI.delete(activity.id, user)
461
462 assert Enum.empty?(Notification.for_user(user))
463 end
464
465 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
466 user = insert(:user)
467 other_user = insert(:user)
468
469 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
470
471 assert Enum.empty?(Notification.for_user(user))
472
473 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
474
475 assert length(Notification.for_user(user)) == 1
476
477 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
478
479 assert Enum.empty?(Notification.for_user(user))
480 end
481
482 test "liking an activity which is already deleted does not generate a notification" do
483 user = insert(:user)
484 other_user = insert(:user)
485
486 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
487
488 assert Enum.empty?(Notification.for_user(user))
489
490 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
491
492 assert Enum.empty?(Notification.for_user(user))
493
494 {:error, _} = CommonAPI.favorite(activity.id, other_user)
495
496 assert Enum.empty?(Notification.for_user(user))
497 end
498
499 test "repeating an activity which is already deleted does not generate a notification" do
500 user = insert(:user)
501 other_user = insert(:user)
502
503 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
504
505 assert Enum.empty?(Notification.for_user(user))
506
507 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
508
509 assert Enum.empty?(Notification.for_user(user))
510
511 {:error, _} = CommonAPI.repeat(activity.id, other_user)
512
513 assert Enum.empty?(Notification.for_user(user))
514 end
515
516 test "replying to a deleted post without tagging does not generate a notification" do
517 user = insert(:user)
518 other_user = insert(:user)
519
520 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
521 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
522
523 {:ok, _reply_activity} =
524 CommonAPI.post(other_user, %{
525 "status" => "test reply",
526 "in_reply_to_status_id" => activity.id
527 })
528
529 assert Enum.empty?(Notification.for_user(user))
530 end
531 end
532 end