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