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