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