total rebase
[anni] / test / pleroma / web / mastodon_api / controllers / notification_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
6   use Pleroma.Web.ConnCase, async: false
7
8   alias Pleroma.Notification
9   alias Pleroma.Repo
10   alias Pleroma.User
11   alias Pleroma.Web.CommonAPI
12
13   import Pleroma.Factory
14
15   setup do
16     Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
17     :ok
18   end
19
20   test "does NOT render account/pleroma/relationship by default" do
21     %{user: user, conn: conn} = oauth_access(["read:notifications"])
22     other_user = insert(:user)
23
24     {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
25     {:ok, [_notification]} = Notification.create_notifications(activity)
26
27     response =
28       conn
29       |> assign(:user, user)
30       |> get("/api/v1/notifications")
31       |> json_response_and_validate_schema(200)
32
33     assert Enum.all?(response, fn n ->
34              get_in(n, ["account", "pleroma", "relationship"]) == %{}
35            end)
36   end
37
38   test "list of notifications" do
39     %{user: user, conn: conn} = oauth_access(["read:notifications"])
40     other_user = insert(:user)
41
42     {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
43
44     {:ok, [_notification]} = Notification.create_notifications(activity)
45
46     conn =
47       conn
48       |> assign(:user, user)
49       |> get("/api/v1/notifications")
50
51     expected_response =
52       "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{user.ap_id}\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
53
54     assert [%{"status" => %{"content" => response}} | _rest] =
55              json_response_and_validate_schema(conn, 200)
56
57     assert response == expected_response
58   end
59
60   test "by default, does not contain pleroma:chat_mention" do
61     %{user: user, conn: conn} = oauth_access(["read:notifications"])
62     other_user = insert(:user)
63
64     {:ok, _activity} = CommonAPI.post_chat_message(other_user, user, "hey")
65
66     result =
67       conn
68       |> get("/api/v1/notifications")
69       |> json_response_and_validate_schema(200)
70
71     assert [] == result
72
73     result =
74       conn
75       |> get("/api/v1/notifications?include_types[]=pleroma:chat_mention")
76       |> json_response_and_validate_schema(200)
77
78     assert [_] = result
79   end
80
81   test "by default, does not contain pleroma:report" do
82     clear_config([:instance, :moderator_privileges], [:reports_manage_reports])
83
84     user = insert(:user)
85     other_user = insert(:user)
86     third_user = insert(:user)
87
88     {:ok, user} = user |> User.admin_api_update(%{is_moderator: true})
89
90     %{conn: conn} = oauth_access(["read:notifications"], user: user)
91
92     {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
93
94     {:ok, _report} =
95       CommonAPI.report(third_user, %{account_id: other_user.id, status_ids: [activity.id]})
96
97     result =
98       conn
99       |> get("/api/v1/notifications")
100       |> json_response_and_validate_schema(200)
101
102     assert [] == result
103
104     result =
105       conn
106       |> get("/api/v1/notifications?include_types[]=pleroma:report")
107       |> json_response_and_validate_schema(200)
108
109     assert [_] = result
110   end
111
112   test "Pleroma:report is hidden for non-privileged users" do
113     clear_config([:instance, :moderator_privileges], [:reports_manage_reports])
114
115     user = insert(:user)
116     other_user = insert(:user)
117     third_user = insert(:user)
118
119     {:ok, user} = user |> User.admin_api_update(%{is_moderator: true})
120
121     %{conn: conn} = oauth_access(["read:notifications"], user: user)
122
123     {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
124
125     {:ok, _report} =
126       CommonAPI.report(third_user, %{account_id: other_user.id, status_ids: [activity.id]})
127
128     result =
129       conn
130       |> get("/api/v1/notifications?include_types[]=pleroma:report")
131       |> json_response_and_validate_schema(200)
132
133     assert [_] = result
134
135     clear_config([:instance, :moderator_privileges], [])
136
137     result =
138       conn
139       |> get("/api/v1/notifications?include_types[]=pleroma:report")
140       |> json_response_and_validate_schema(200)
141
142     assert [] == result
143   end
144
145   test "excludes mentions from blockers when blockers_visible is false" do
146     clear_config([:activitypub, :blockers_visible], false)
147
148     %{user: user, conn: conn} = oauth_access(["read:notifications"])
149     blocker = insert(:user)
150
151     {:ok, _} = CommonAPI.block(blocker, user)
152     {:ok, activity} = CommonAPI.post(blocker, %{status: "hi @#{user.nickname}"})
153
154     {:ok, [_notification]} = Notification.create_notifications(activity)
155
156     conn =
157       conn
158       |> assign(:user, user)
159       |> get("/api/v1/notifications")
160
161     assert [] == json_response_and_validate_schema(conn, 200)
162   end
163
164   test "getting a single notification" do
165     %{user: user, conn: conn} = oauth_access(["read:notifications"])
166     other_user = insert(:user)
167
168     {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
169
170     {:ok, [notification]} = Notification.create_notifications(activity)
171
172     conn = get(conn, "/api/v1/notifications/#{notification.id}")
173
174     expected_response =
175       "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{user.ap_id}\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
176
177     assert %{"status" => %{"content" => response}} = json_response_and_validate_schema(conn, 200)
178     assert response == expected_response
179   end
180
181   test "dismissing a single notification (deprecated endpoint)" do
182     %{user: user, conn: conn} = oauth_access(["write:notifications"])
183     other_user = insert(:user)
184
185     {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
186
187     {:ok, [notification]} = Notification.create_notifications(activity)
188
189     conn =
190       conn
191       |> assign(:user, user)
192       |> put_req_header("content-type", "application/json")
193       |> post("/api/v1/notifications/dismiss", %{"id" => to_string(notification.id)})
194
195     assert %{} = json_response_and_validate_schema(conn, 200)
196   end
197
198   test "dismissing a single notification" do
199     %{user: user, conn: conn} = oauth_access(["write:notifications"])
200     other_user = insert(:user)
201
202     {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
203
204     {:ok, [notification]} = Notification.create_notifications(activity)
205
206     conn =
207       conn
208       |> assign(:user, user)
209       |> post("/api/v1/notifications/#{notification.id}/dismiss")
210
211     assert %{} = json_response_and_validate_schema(conn, 200)
212   end
213
214   test "clearing all notifications" do
215     %{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
216     other_user = insert(:user)
217
218     {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
219
220     {:ok, [_notification]} = Notification.create_notifications(activity)
221
222     ret_conn = post(conn, "/api/v1/notifications/clear")
223
224     assert %{} = json_response_and_validate_schema(ret_conn, 200)
225
226     ret_conn = get(conn, "/api/v1/notifications")
227
228     assert all = json_response_and_validate_schema(ret_conn, 200)
229     assert all == []
230   end
231
232   test "paginates notifications using min_id, since_id, max_id, and limit" do
233     %{user: user, conn: conn} = oauth_access(["read:notifications"])
234     other_user = insert(:user)
235
236     {:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
237     {:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
238     {:ok, activity3} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
239     {:ok, activity4} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
240
241     notification1_id = get_notification_id_by_activity(activity1)
242     notification2_id = get_notification_id_by_activity(activity2)
243     notification3_id = get_notification_id_by_activity(activity3)
244     notification4_id = get_notification_id_by_activity(activity4)
245
246     conn = assign(conn, :user, user)
247
248     # min_id
249     result =
250       conn
251       |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
252       |> json_response_and_validate_schema(:ok)
253
254     assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
255
256     # since_id
257     result =
258       conn
259       |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
260       |> json_response_and_validate_schema(:ok)
261
262     assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
263
264     # max_id
265     result =
266       conn
267       |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
268       |> json_response_and_validate_schema(:ok)
269
270     assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
271   end
272
273   describe "exclude_visibilities" do
274     test "filters notifications for mentions" do
275       %{user: user, conn: conn} = oauth_access(["read:notifications"])
276       other_user = insert(:user)
277
278       {:ok, public_activity} =
279         CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "public"})
280
281       {:ok, direct_activity} =
282         CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
283
284       {:ok, unlisted_activity} =
285         CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "unlisted"})
286
287       {:ok, private_activity} =
288         CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "private"})
289
290       query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "private"]})
291       conn_res = get(conn, "/api/v1/notifications?" <> query)
292
293       assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
294       assert id == direct_activity.id
295
296       query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "direct"]})
297       conn_res = get(conn, "/api/v1/notifications?" <> query)
298
299       assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
300       assert id == private_activity.id
301
302       query = params_to_query(%{exclude_visibilities: ["public", "private", "direct"]})
303       conn_res = get(conn, "/api/v1/notifications?" <> query)
304
305       assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
306       assert id == unlisted_activity.id
307
308       query = params_to_query(%{exclude_visibilities: ["unlisted", "private", "direct"]})
309       conn_res = get(conn, "/api/v1/notifications?" <> query)
310
311       assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
312       assert id == public_activity.id
313     end
314
315     test "filters notifications for Like activities" do
316       user = insert(:user)
317       %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
318
319       {:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
320
321       {:ok, direct_activity} =
322         CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
323
324       {:ok, unlisted_activity} =
325         CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
326
327       {:ok, private_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "private"})
328
329       {:ok, _} = CommonAPI.favorite(user, public_activity.id)
330       {:ok, _} = CommonAPI.favorite(user, direct_activity.id)
331       {:ok, _} = CommonAPI.favorite(user, unlisted_activity.id)
332       {:ok, _} = CommonAPI.favorite(user, private_activity.id)
333
334       activity_ids =
335         conn
336         |> get("/api/v1/notifications?exclude_visibilities[]=direct")
337         |> json_response_and_validate_schema(200)
338         |> Enum.map(& &1["status"]["id"])
339
340       assert public_activity.id in activity_ids
341       assert unlisted_activity.id in activity_ids
342       assert private_activity.id in activity_ids
343       refute direct_activity.id in activity_ids
344
345       activity_ids =
346         conn
347         |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
348         |> json_response_and_validate_schema(200)
349         |> Enum.map(& &1["status"]["id"])
350
351       assert public_activity.id in activity_ids
352       refute unlisted_activity.id in activity_ids
353       assert private_activity.id in activity_ids
354       assert direct_activity.id in activity_ids
355
356       activity_ids =
357         conn
358         |> get("/api/v1/notifications?exclude_visibilities[]=private")
359         |> json_response_and_validate_schema(200)
360         |> Enum.map(& &1["status"]["id"])
361
362       assert public_activity.id in activity_ids
363       assert unlisted_activity.id in activity_ids
364       refute private_activity.id in activity_ids
365       assert direct_activity.id in activity_ids
366
367       activity_ids =
368         conn
369         |> get("/api/v1/notifications?exclude_visibilities[]=public")
370         |> json_response_and_validate_schema(200)
371         |> Enum.map(& &1["status"]["id"])
372
373       refute public_activity.id in activity_ids
374       assert unlisted_activity.id in activity_ids
375       assert private_activity.id in activity_ids
376       assert direct_activity.id in activity_ids
377     end
378
379     test "filters notifications for Announce activities" do
380       user = insert(:user)
381       %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
382
383       {:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
384
385       {:ok, unlisted_activity} =
386         CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
387
388       {:ok, _} = CommonAPI.repeat(public_activity.id, user)
389       {:ok, _} = CommonAPI.repeat(unlisted_activity.id, user)
390
391       activity_ids =
392         conn
393         |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
394         |> json_response_and_validate_schema(200)
395         |> Enum.map(& &1["status"]["id"])
396
397       assert public_activity.id in activity_ids
398       refute unlisted_activity.id in activity_ids
399     end
400
401     test "doesn't return less than the requested amount of records when the user's reply is liked" do
402       user = insert(:user)
403       %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
404
405       {:ok, mention} =
406         CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "public"})
407
408       {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
409
410       {:ok, reply} =
411         CommonAPI.post(other_user, %{
412           status: ".",
413           visibility: "public",
414           in_reply_to_status_id: activity.id
415         })
416
417       {:ok, _favorite} = CommonAPI.favorite(user, reply.id)
418
419       activity_ids =
420         conn
421         |> get("/api/v1/notifications?exclude_visibilities[]=direct&limit=2")
422         |> json_response_and_validate_schema(200)
423         |> Enum.map(& &1["status"]["id"])
424
425       assert [reply.id, mention.id] == activity_ids
426     end
427   end
428
429   test "filters notifications using exclude_types" do
430     %{user: user, conn: conn} = oauth_access(["read:notifications"])
431     other_user = insert(:user)
432
433     {:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
434     {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
435     {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
436     {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
437     {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
438
439     mention_notification_id = get_notification_id_by_activity(mention_activity)
440     favorite_notification_id = get_notification_id_by_activity(favorite_activity)
441     reblog_notification_id = get_notification_id_by_activity(reblog_activity)
442     follow_notification_id = get_notification_id_by_activity(follow_activity)
443
444     query = params_to_query(%{exclude_types: ["mention", "favourite", "reblog"]})
445     conn_res = get(conn, "/api/v1/notifications?" <> query)
446
447     assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
448
449     query = params_to_query(%{exclude_types: ["favourite", "reblog", "follow"]})
450     conn_res = get(conn, "/api/v1/notifications?" <> query)
451
452     assert [%{"id" => ^mention_notification_id}] =
453              json_response_and_validate_schema(conn_res, 200)
454
455     query = params_to_query(%{exclude_types: ["reblog", "follow", "mention"]})
456     conn_res = get(conn, "/api/v1/notifications?" <> query)
457
458     assert [%{"id" => ^favorite_notification_id}] =
459              json_response_and_validate_schema(conn_res, 200)
460
461     query = params_to_query(%{exclude_types: ["follow", "mention", "favourite"]})
462     conn_res = get(conn, "/api/v1/notifications?" <> query)
463
464     assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
465   end
466
467   test "filters notifications using types" do
468     %{user: user, conn: conn} = oauth_access(["read:notifications"])
469     other_user = insert(:user)
470
471     {:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
472     {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
473     {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
474     {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
475     {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
476
477     mention_notification_id = get_notification_id_by_activity(mention_activity)
478     favorite_notification_id = get_notification_id_by_activity(favorite_activity)
479     reblog_notification_id = get_notification_id_by_activity(reblog_activity)
480     follow_notification_id = get_notification_id_by_activity(follow_activity)
481
482     conn_res = get(conn, "/api/v1/notifications?types[]=follow")
483
484     assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
485
486     conn_res = get(conn, "/api/v1/notifications?types[]=mention")
487
488     assert [%{"id" => ^mention_notification_id}] =
489              json_response_and_validate_schema(conn_res, 200)
490
491     conn_res = get(conn, "/api/v1/notifications?types[]=favourite")
492
493     assert [%{"id" => ^favorite_notification_id}] =
494              json_response_and_validate_schema(conn_res, 200)
495
496     conn_res = get(conn, "/api/v1/notifications?types[]=reblog")
497
498     assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
499
500     result = conn |> get("/api/v1/notifications") |> json_response_and_validate_schema(200)
501
502     assert length(result) == 4
503
504     query = params_to_query(%{types: ["follow", "mention", "favourite", "reblog"]})
505
506     result =
507       conn
508       |> get("/api/v1/notifications?" <> query)
509       |> json_response_and_validate_schema(200)
510
511     assert length(result) == 4
512   end
513
514   test "filtering falls back to include_types" do
515     %{user: user, conn: conn} = oauth_access(["read:notifications"])
516     other_user = insert(:user)
517
518     {:ok, _activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
519     {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
520     {:ok, _activity} = CommonAPI.favorite(other_user, create_activity.id)
521     {:ok, _activity} = CommonAPI.repeat(create_activity.id, other_user)
522     {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
523
524     follow_notification_id = get_notification_id_by_activity(follow_activity)
525
526     conn_res = get(conn, "/api/v1/notifications?include_types[]=follow")
527
528     assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
529   end
530
531   test "destroy multiple" do
532     %{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
533     other_user = insert(:user)
534
535     {:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
536     {:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
537     {:ok, activity3} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
538     {:ok, activity4} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
539
540     notification1_id = get_notification_id_by_activity(activity1)
541     notification2_id = get_notification_id_by_activity(activity2)
542     notification3_id = get_notification_id_by_activity(activity3)
543     notification4_id = get_notification_id_by_activity(activity4)
544
545     result =
546       conn
547       |> get("/api/v1/notifications")
548       |> json_response_and_validate_schema(:ok)
549
550     assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
551
552     conn2 =
553       conn
554       |> assign(:user, other_user)
555       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
556
557     result =
558       conn2
559       |> get("/api/v1/notifications")
560       |> json_response_and_validate_schema(:ok)
561
562     assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
563
564     query = params_to_query(%{ids: [notification1_id, notification2_id]})
565     conn_destroy = delete(conn, "/api/v1/notifications/destroy_multiple?" <> query)
566
567     assert json_response_and_validate_schema(conn_destroy, 200) == %{}
568
569     result =
570       conn2
571       |> get("/api/v1/notifications")
572       |> json_response_and_validate_schema(:ok)
573
574     assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
575   end
576
577   test "doesn't see notifications after muting user with notifications" do
578     %{user: user, conn: conn} = oauth_access(["read:notifications"])
579     user2 = insert(:user)
580
581     {:ok, _, _, _} = CommonAPI.follow(user, user2)
582     {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
583
584     ret_conn = get(conn, "/api/v1/notifications")
585
586     assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
587
588     {:ok, _user_relationships} = User.mute(user, user2)
589
590     conn = get(conn, "/api/v1/notifications")
591
592     assert json_response_and_validate_schema(conn, 200) == []
593   end
594
595   test "see notifications after muting user without notifications" do
596     %{user: user, conn: conn} = oauth_access(["read:notifications"])
597     user2 = insert(:user)
598
599     {:ok, _, _, _} = CommonAPI.follow(user, user2)
600     {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
601
602     ret_conn = get(conn, "/api/v1/notifications")
603
604     assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
605
606     {:ok, _user_relationships} = User.mute(user, user2, %{notifications: false})
607
608     conn = get(conn, "/api/v1/notifications")
609
610     assert length(json_response_and_validate_schema(conn, 200)) == 1
611   end
612
613   test "see notifications after muting user with notifications and with_muted parameter" do
614     %{user: user, conn: conn} = oauth_access(["read:notifications"])
615     user2 = insert(:user)
616
617     {:ok, _, _, _} = CommonAPI.follow(user, user2)
618     {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
619
620     ret_conn = get(conn, "/api/v1/notifications")
621
622     assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
623
624     {:ok, _user_relationships} = User.mute(user, user2)
625
626     conn = get(conn, "/api/v1/notifications?with_muted=true")
627
628     assert length(json_response_and_validate_schema(conn, 200)) == 1
629   end
630
631   test "see move notifications" do
632     old_user = insert(:user)
633     new_user = insert(:user, also_known_as: [old_user.ap_id])
634     %{user: follower, conn: conn} = oauth_access(["read:notifications"])
635
636     User.follow(follower, old_user)
637     Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
638     Pleroma.Tests.ObanHelpers.perform_all()
639
640     conn = get(conn, "/api/v1/notifications")
641
642     assert length(json_response_and_validate_schema(conn, 200)) == 1
643   end
644
645   describe "link headers" do
646     test "preserves parameters in link headers" do
647       %{user: user, conn: conn} = oauth_access(["read:notifications"])
648       other_user = insert(:user)
649
650       {:ok, activity1} =
651         CommonAPI.post(other_user, %{
652           status: "hi @#{user.nickname}",
653           visibility: "public"
654         })
655
656       {:ok, activity2} =
657         CommonAPI.post(other_user, %{
658           status: "hi @#{user.nickname}",
659           visibility: "public"
660         })
661
662       notification1 = Repo.get_by(Notification, activity_id: activity1.id)
663       notification2 = Repo.get_by(Notification, activity_id: activity2.id)
664
665       conn =
666         conn
667         |> assign(:user, user)
668         |> get("/api/v1/notifications?limit=5")
669
670       assert [link_header] = get_resp_header(conn, "link")
671       assert link_header =~ ~r/limit=5/
672       assert link_header =~ ~r/min_id=#{notification2.id}/
673       assert link_header =~ ~r/max_id=#{notification1.id}/
674     end
675   end
676
677   describe "from specified user" do
678     test "account_id" do
679       %{user: user, conn: conn} = oauth_access(["read:notifications"])
680
681       %{id: account_id} = other_user1 = insert(:user)
682       other_user2 = insert(:user)
683
684       {:ok, _activity} = CommonAPI.post(other_user1, %{status: "hi @#{user.nickname}"})
685       {:ok, _activity} = CommonAPI.post(other_user2, %{status: "bye @#{user.nickname}"})
686
687       assert [%{"account" => %{"id" => ^account_id}}] =
688                conn
689                |> assign(:user, user)
690                |> get("/api/v1/notifications?account_id=#{account_id}")
691                |> json_response_and_validate_schema(200)
692
693       assert %{"error" => "Account is not found"} =
694                conn
695                |> assign(:user, user)
696                |> get("/api/v1/notifications?account_id=cofe")
697                |> json_response_and_validate_schema(404)
698     end
699   end
700
701   defp get_notification_id_by_activity(%{id: id}) do
702     Notification
703     |> Repo.get_by(activity_id: id)
704     |> Map.get(:id)
705     |> to_string()
706   end
707
708   defp params_to_query(%{} = params) do
709     Enum.map_join(params, "&", fn
710       {k, v} when is_list(v) -> Enum.map_join(v, "&", &"#{k}[]=#{&1}")
711       {k, v} -> k <> "=" <> v
712     end)
713   end
714 end