1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
6 use Pleroma.Web.ConnCase
8 alias Pleroma.Notification
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 test "does NOT render account/pleroma/relationship by default" do
16 %{user: user, conn: conn} = oauth_access(["read:notifications"])
17 other_user = insert(:user)
19 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
20 {:ok, [_notification]} = Notification.create_notifications(activity)
24 |> assign(:user, user)
25 |> get("/api/v1/notifications")
26 |> json_response_and_validate_schema(200)
28 assert Enum.all?(response, fn n ->
29 get_in(n, ["account", "pleroma", "relationship"]) == %{}
33 test "list of notifications" do
34 %{user: user, conn: conn} = oauth_access(["read:notifications"])
35 other_user = insert(:user)
37 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
39 {:ok, [_notification]} = Notification.create_notifications(activity)
43 |> assign(:user, user)
44 |> get("/api/v1/notifications")
47 "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>"
49 assert [%{"status" => %{"content" => response}} | _rest] =
50 json_response_and_validate_schema(conn, 200)
52 assert response == expected_response
55 test "by default, does not contain pleroma:report" do
56 %{user: user, conn: conn} = oauth_access(["read:notifications"])
57 other_user = insert(:user)
58 third_user = insert(:user)
61 |> User.admin_api_update(%{is_moderator: true})
63 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
66 CommonAPI.report(third_user, %{account_id: other_user.id, status_ids: [activity.id]})
70 |> get("/api/v1/notifications")
71 |> json_response_and_validate_schema(200)
77 |> get("/api/v1/notifications?include_types[]=pleroma:report")
78 |> json_response_and_validate_schema(200)
83 test "excludes mentions from blockers when blockers_visible is false" do
84 clear_config([:activitypub, :blockers_visible], false)
86 %{user: user, conn: conn} = oauth_access(["read:notifications"])
87 blocker = insert(:user)
89 {:ok, _} = CommonAPI.block(blocker, user)
90 {:ok, activity} = CommonAPI.post(blocker, %{status: "hi @#{user.nickname}"})
92 {:ok, [_notification]} = Notification.create_notifications(activity)
96 |> assign(:user, user)
97 |> get("/api/v1/notifications")
99 assert [] == json_response_and_validate_schema(conn, 200)
102 test "getting a single notification" do
103 %{user: user, conn: conn} = oauth_access(["read:notifications"])
104 other_user = insert(:user)
106 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
108 {:ok, [notification]} = Notification.create_notifications(activity)
110 conn = get(conn, "/api/v1/notifications/#{notification.id}")
113 "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>"
115 assert %{"status" => %{"content" => response}} = json_response_and_validate_schema(conn, 200)
116 assert response == expected_response
119 test "dismissing a single notification" do
120 %{user: user, conn: conn} = oauth_access(["write:notifications"])
121 other_user = insert(:user)
123 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
125 {:ok, [notification]} = Notification.create_notifications(activity)
129 |> assign(:user, user)
130 |> post("/api/v1/notifications/#{notification.id}/dismiss")
132 assert %{} = json_response_and_validate_schema(conn, 200)
135 test "clearing all notifications" do
136 %{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
137 other_user = insert(:user)
139 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
141 {:ok, [_notification]} = Notification.create_notifications(activity)
143 ret_conn = post(conn, "/api/v1/notifications/clear")
145 assert %{} = json_response_and_validate_schema(ret_conn, 200)
147 ret_conn = get(conn, "/api/v1/notifications")
149 assert all = json_response_and_validate_schema(ret_conn, 200)
153 test "paginates notifications using min_id, since_id, max_id, and limit" do
154 %{user: user, conn: conn} = oauth_access(["read:notifications"])
155 other_user = insert(:user)
157 {:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
158 {:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
159 {:ok, activity3} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
160 {:ok, activity4} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
162 notification1_id = get_notification_id_by_activity(activity1)
163 notification2_id = get_notification_id_by_activity(activity2)
164 notification3_id = get_notification_id_by_activity(activity3)
165 notification4_id = get_notification_id_by_activity(activity4)
167 conn = assign(conn, :user, user)
172 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
173 |> json_response_and_validate_schema(:ok)
175 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
180 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
181 |> json_response_and_validate_schema(:ok)
183 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
188 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
189 |> json_response_and_validate_schema(:ok)
191 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
194 describe "exclude_visibilities" do
195 test "filters notifications for mentions" do
196 %{user: user, conn: conn} = oauth_access(["read:notifications"])
197 other_user = insert(:user)
199 {:ok, public_activity} =
200 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "public"})
202 {:ok, direct_activity} =
203 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
205 {:ok, unlisted_activity} =
206 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "unlisted"})
208 {:ok, private_activity} =
209 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "private"})
211 query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "private"]})
212 conn_res = get(conn, "/api/v1/notifications?" <> query)
214 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
215 assert id == direct_activity.id
217 query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "direct"]})
218 conn_res = get(conn, "/api/v1/notifications?" <> query)
220 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
221 assert id == private_activity.id
223 query = params_to_query(%{exclude_visibilities: ["public", "private", "direct"]})
224 conn_res = get(conn, "/api/v1/notifications?" <> query)
226 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
227 assert id == unlisted_activity.id
229 query = params_to_query(%{exclude_visibilities: ["unlisted", "private", "direct"]})
230 conn_res = get(conn, "/api/v1/notifications?" <> query)
232 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
233 assert id == public_activity.id
236 test "filters notifications for Like activities" do
238 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
240 {:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
242 {:ok, direct_activity} =
243 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
245 {:ok, unlisted_activity} =
246 CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
248 {:ok, private_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "private"})
250 {:ok, _} = CommonAPI.favorite(user, public_activity.id)
251 {:ok, _} = CommonAPI.favorite(user, direct_activity.id)
252 {:ok, _} = CommonAPI.favorite(user, unlisted_activity.id)
253 {:ok, _} = CommonAPI.favorite(user, private_activity.id)
257 |> get("/api/v1/notifications?exclude_visibilities[]=direct")
258 |> json_response_and_validate_schema(200)
259 |> Enum.map(& &1["status"]["id"])
261 assert public_activity.id in activity_ids
262 assert unlisted_activity.id in activity_ids
263 assert private_activity.id in activity_ids
264 refute direct_activity.id in activity_ids
268 |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
269 |> json_response_and_validate_schema(200)
270 |> Enum.map(& &1["status"]["id"])
272 assert public_activity.id in activity_ids
273 refute unlisted_activity.id in activity_ids
274 assert private_activity.id in activity_ids
275 assert direct_activity.id in activity_ids
279 |> get("/api/v1/notifications?exclude_visibilities[]=private")
280 |> json_response_and_validate_schema(200)
281 |> Enum.map(& &1["status"]["id"])
283 assert public_activity.id in activity_ids
284 assert unlisted_activity.id in activity_ids
285 refute private_activity.id in activity_ids
286 assert direct_activity.id in activity_ids
290 |> get("/api/v1/notifications?exclude_visibilities[]=public")
291 |> json_response_and_validate_schema(200)
292 |> Enum.map(& &1["status"]["id"])
294 refute public_activity.id in activity_ids
295 assert unlisted_activity.id in activity_ids
296 assert private_activity.id in activity_ids
297 assert direct_activity.id in activity_ids
300 test "filters notifications for Announce activities" do
302 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
304 {:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
306 {:ok, unlisted_activity} =
307 CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
309 {:ok, _} = CommonAPI.repeat(public_activity.id, user)
310 {:ok, _} = CommonAPI.repeat(unlisted_activity.id, user)
314 |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
315 |> json_response_and_validate_schema(200)
316 |> Enum.map(& &1["status"]["id"])
318 assert public_activity.id in activity_ids
319 refute unlisted_activity.id in activity_ids
322 test "doesn't return less than the requested amount of records when the user's reply is liked" do
324 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
327 CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "public"})
329 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
332 CommonAPI.post(other_user, %{
334 visibility: "public",
335 in_reply_to_status_id: activity.id
338 {:ok, _favorite} = CommonAPI.favorite(user, reply.id)
342 |> get("/api/v1/notifications?exclude_visibilities[]=direct&limit=2")
343 |> json_response_and_validate_schema(200)
344 |> Enum.map(& &1["status"]["id"])
346 assert [reply.id, mention.id] == activity_ids
350 test "filters notifications using exclude_types" do
351 %{user: user, conn: conn} = oauth_access(["read:notifications"])
352 other_user = insert(:user)
354 {:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
355 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
356 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
357 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
358 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
360 mention_notification_id = get_notification_id_by_activity(mention_activity)
361 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
362 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
363 follow_notification_id = get_notification_id_by_activity(follow_activity)
365 query = params_to_query(%{exclude_types: ["mention", "favourite", "reblog"]})
366 conn_res = get(conn, "/api/v1/notifications?" <> query)
368 assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
370 query = params_to_query(%{exclude_types: ["favourite", "reblog", "follow"]})
371 conn_res = get(conn, "/api/v1/notifications?" <> query)
373 assert [%{"id" => ^mention_notification_id}] =
374 json_response_and_validate_schema(conn_res, 200)
376 query = params_to_query(%{exclude_types: ["reblog", "follow", "mention"]})
377 conn_res = get(conn, "/api/v1/notifications?" <> query)
379 assert [%{"id" => ^favorite_notification_id}] =
380 json_response_and_validate_schema(conn_res, 200)
382 query = params_to_query(%{exclude_types: ["follow", "mention", "favourite"]})
383 conn_res = get(conn, "/api/v1/notifications?" <> query)
385 assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
388 test "filters notifications using include_types" do
389 %{user: user, conn: conn} = oauth_access(["read:notifications"])
390 other_user = insert(:user)
392 {:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
393 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
394 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
395 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
396 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
398 mention_notification_id = get_notification_id_by_activity(mention_activity)
399 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
400 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
401 follow_notification_id = get_notification_id_by_activity(follow_activity)
403 conn_res = get(conn, "/api/v1/notifications?include_types[]=follow")
405 assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
407 conn_res = get(conn, "/api/v1/notifications?include_types[]=mention")
409 assert [%{"id" => ^mention_notification_id}] =
410 json_response_and_validate_schema(conn_res, 200)
412 conn_res = get(conn, "/api/v1/notifications?include_types[]=favourite")
414 assert [%{"id" => ^favorite_notification_id}] =
415 json_response_and_validate_schema(conn_res, 200)
417 conn_res = get(conn, "/api/v1/notifications?include_types[]=reblog")
419 assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
421 result = conn |> get("/api/v1/notifications") |> json_response_and_validate_schema(200)
423 assert length(result) == 4
425 query = params_to_query(%{include_types: ["follow", "mention", "favourite", "reblog"]})
429 |> get("/api/v1/notifications?" <> query)
430 |> json_response_and_validate_schema(200)
432 assert length(result) == 4
435 test "destroy multiple" do
436 %{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
437 other_user = insert(:user)
439 {:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
440 {:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
441 {:ok, activity3} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
442 {:ok, activity4} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
444 notification1_id = get_notification_id_by_activity(activity1)
445 notification2_id = get_notification_id_by_activity(activity2)
446 notification3_id = get_notification_id_by_activity(activity3)
447 notification4_id = get_notification_id_by_activity(activity4)
451 |> get("/api/v1/notifications")
452 |> json_response_and_validate_schema(:ok)
454 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
458 |> assign(:user, other_user)
459 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
463 |> get("/api/v1/notifications")
464 |> json_response_and_validate_schema(:ok)
466 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
468 query = params_to_query(%{ids: [notification1_id, notification2_id]})
469 conn_destroy = delete(conn, "/api/v1/notifications/destroy_multiple?" <> query)
471 assert json_response_and_validate_schema(conn_destroy, 200) == %{}
475 |> get("/api/v1/notifications")
476 |> json_response_and_validate_schema(:ok)
478 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
481 test "doesn't see notifications after muting user with notifications" do
482 %{user: user, conn: conn} = oauth_access(["read:notifications"])
483 user2 = insert(:user)
485 {:ok, _, _, _} = CommonAPI.follow(user, user2)
486 {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
488 ret_conn = get(conn, "/api/v1/notifications")
490 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
492 {:ok, _user_relationships} = User.mute(user, user2)
494 conn = get(conn, "/api/v1/notifications")
496 assert json_response_and_validate_schema(conn, 200) == []
499 test "see notifications after muting user without notifications" do
500 %{user: user, conn: conn} = oauth_access(["read:notifications"])
501 user2 = insert(:user)
503 {:ok, _, _, _} = CommonAPI.follow(user, user2)
504 {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
506 ret_conn = get(conn, "/api/v1/notifications")
508 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
510 {:ok, _user_relationships} = User.mute(user, user2, %{notifications: false})
512 conn = get(conn, "/api/v1/notifications")
514 assert length(json_response_and_validate_schema(conn, 200)) == 1
517 test "see notifications after muting user with notifications and with_muted parameter" do
518 %{user: user, conn: conn} = oauth_access(["read:notifications"])
519 user2 = insert(:user)
521 {:ok, _, _, _} = CommonAPI.follow(user, user2)
522 {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
524 ret_conn = get(conn, "/api/v1/notifications")
526 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
528 {:ok, _user_relationships} = User.mute(user, user2)
530 conn = get(conn, "/api/v1/notifications?with_muted=true")
532 assert length(json_response_and_validate_schema(conn, 200)) == 1
535 test "see move notifications" do
536 old_user = insert(:user)
537 new_user = insert(:user, also_known_as: [old_user.ap_id])
538 %{user: follower, conn: conn} = oauth_access(["read:notifications"])
540 User.follow(follower, old_user)
541 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
542 Pleroma.Tests.ObanHelpers.perform_all()
544 conn = get(conn, "/api/v1/notifications")
546 assert length(json_response_and_validate_schema(conn, 200)) == 1
549 describe "link headers" do
550 test "preserves parameters in link headers" do
551 %{user: user, conn: conn} = oauth_access(["read:notifications"])
552 other_user = insert(:user)
555 CommonAPI.post(other_user, %{
556 status: "hi @#{user.nickname}",
561 CommonAPI.post(other_user, %{
562 status: "hi @#{user.nickname}",
566 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
567 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
571 |> assign(:user, user)
572 |> get("/api/v1/notifications?limit=5")
574 assert [link_header] = get_resp_header(conn, "link")
575 assert link_header =~ ~r/limit=5/
576 assert link_header =~ ~r/min_id=#{notification2.id}/
577 assert link_header =~ ~r/max_id=#{notification1.id}/
581 describe "from specified user" do
583 %{user: user, conn: conn} = oauth_access(["read:notifications"])
585 %{id: account_id} = other_user1 = insert(:user)
586 other_user2 = insert(:user)
588 {:ok, _activity} = CommonAPI.post(other_user1, %{status: "hi @#{user.nickname}"})
589 {:ok, _activity} = CommonAPI.post(other_user2, %{status: "bye @#{user.nickname}"})
591 assert [%{"account" => %{"id" => ^account_id}}] =
593 |> assign(:user, user)
594 |> get("/api/v1/notifications?account_id=#{account_id}")
595 |> json_response_and_validate_schema(200)
597 assert %{"error" => "Account is not found"} =
599 |> assign(:user, user)
600 |> get("/api/v1/notifications?account_id=cofe")
601 |> json_response_and_validate_schema(404)
605 defp get_notification_id_by_activity(%{id: id}) do
607 |> Repo.get_by(activity_id: id)
612 defp params_to_query(%{} = params) do
613 Enum.map_join(params, "&", fn
614 {k, v} when is_list(v) -> Enum.map_join(v, "&", &"#{k}[]=#{&1}")
615 {k, v} -> k <> "=" <> v