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