1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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=\"#{
49 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
51 assert [%{"status" => %{"content" => response}} | _rest] =
52 json_response_and_validate_schema(conn, 200)
54 assert response == expected_response
57 test "by default, does not contain pleroma:chat_mention" do
58 %{user: user, conn: conn} = oauth_access(["read:notifications"])
59 other_user = insert(:user)
61 {:ok, _activity} = CommonAPI.post_chat_message(other_user, user, "hey")
65 |> get("/api/v1/notifications")
66 |> json_response_and_validate_schema(200)
72 |> get("/api/v1/notifications?include_types[]=pleroma:chat_mention")
73 |> json_response_and_validate_schema(200)
78 test "by default, does not contain pleroma:report" do
79 %{user: user, conn: conn} = oauth_access(["read:notifications"])
80 other_user = insert(:user)
81 third_user = insert(:user)
84 |> User.admin_api_update(%{is_moderator: true})
86 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
89 CommonAPI.report(third_user, %{account_id: other_user.id, status_ids: [activity.id]})
93 |> get("/api/v1/notifications")
94 |> json_response_and_validate_schema(200)
100 |> get("/api/v1/notifications?include_types[]=pleroma:report")
101 |> json_response_and_validate_schema(200)
106 test "getting a single notification" do
107 %{user: user, conn: conn} = oauth_access(["read:notifications"])
108 other_user = insert(:user)
110 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
112 {:ok, [notification]} = Notification.create_notifications(activity)
114 conn = get(conn, "/api/v1/notifications/#{notification.id}")
117 "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{
119 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
121 assert %{"status" => %{"content" => response}} = json_response_and_validate_schema(conn, 200)
122 assert response == expected_response
125 test "dismissing a single notification (deprecated endpoint)" do
126 %{user: user, conn: conn} = oauth_access(["write:notifications"])
127 other_user = insert(:user)
129 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
131 {:ok, [notification]} = Notification.create_notifications(activity)
135 |> assign(:user, user)
136 |> put_req_header("content-type", "application/json")
137 |> post("/api/v1/notifications/dismiss", %{"id" => to_string(notification.id)})
139 assert %{} = json_response_and_validate_schema(conn, 200)
142 test "dismissing a single notification" do
143 %{user: user, conn: conn} = oauth_access(["write:notifications"])
144 other_user = insert(:user)
146 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
148 {:ok, [notification]} = Notification.create_notifications(activity)
152 |> assign(:user, user)
153 |> post("/api/v1/notifications/#{notification.id}/dismiss")
155 assert %{} = json_response_and_validate_schema(conn, 200)
158 test "clearing all notifications" do
159 %{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
160 other_user = insert(:user)
162 {:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
164 {:ok, [_notification]} = Notification.create_notifications(activity)
166 ret_conn = post(conn, "/api/v1/notifications/clear")
168 assert %{} = json_response_and_validate_schema(ret_conn, 200)
170 ret_conn = get(conn, "/api/v1/notifications")
172 assert all = json_response_and_validate_schema(ret_conn, 200)
176 test "paginates notifications using min_id, since_id, max_id, and limit" do
177 %{user: user, conn: conn} = oauth_access(["read:notifications"])
178 other_user = insert(:user)
180 {:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
181 {:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
182 {:ok, activity3} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
183 {:ok, activity4} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
185 notification1_id = get_notification_id_by_activity(activity1)
186 notification2_id = get_notification_id_by_activity(activity2)
187 notification3_id = get_notification_id_by_activity(activity3)
188 notification4_id = get_notification_id_by_activity(activity4)
190 conn = assign(conn, :user, user)
195 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
196 |> json_response_and_validate_schema(:ok)
198 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
203 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
204 |> json_response_and_validate_schema(:ok)
206 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
211 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
212 |> json_response_and_validate_schema(:ok)
214 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
217 describe "exclude_visibilities" do
218 test "filters notifications for mentions" do
219 %{user: user, conn: conn} = oauth_access(["read:notifications"])
220 other_user = insert(:user)
222 {:ok, public_activity} =
223 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "public"})
225 {:ok, direct_activity} =
226 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
228 {:ok, unlisted_activity} =
229 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "unlisted"})
231 {:ok, private_activity} =
232 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "private"})
234 query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "private"]})
235 conn_res = get(conn, "/api/v1/notifications?" <> query)
237 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
238 assert id == direct_activity.id
240 query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "direct"]})
241 conn_res = get(conn, "/api/v1/notifications?" <> query)
243 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
244 assert id == private_activity.id
246 query = params_to_query(%{exclude_visibilities: ["public", "private", "direct"]})
247 conn_res = get(conn, "/api/v1/notifications?" <> query)
249 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
250 assert id == unlisted_activity.id
252 query = params_to_query(%{exclude_visibilities: ["unlisted", "private", "direct"]})
253 conn_res = get(conn, "/api/v1/notifications?" <> query)
255 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
256 assert id == public_activity.id
259 test "filters notifications for Like activities" do
261 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
263 {:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
265 {:ok, direct_activity} =
266 CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
268 {:ok, unlisted_activity} =
269 CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
271 {:ok, private_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "private"})
273 {:ok, _} = CommonAPI.favorite(user, public_activity.id)
274 {:ok, _} = CommonAPI.favorite(user, direct_activity.id)
275 {:ok, _} = CommonAPI.favorite(user, unlisted_activity.id)
276 {:ok, _} = CommonAPI.favorite(user, private_activity.id)
280 |> get("/api/v1/notifications?exclude_visibilities[]=direct")
281 |> json_response_and_validate_schema(200)
282 |> Enum.map(& &1["status"]["id"])
284 assert public_activity.id in activity_ids
285 assert unlisted_activity.id in activity_ids
286 assert private_activity.id in activity_ids
287 refute direct_activity.id in activity_ids
291 |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
292 |> json_response_and_validate_schema(200)
293 |> Enum.map(& &1["status"]["id"])
295 assert public_activity.id in activity_ids
296 refute unlisted_activity.id in activity_ids
297 assert private_activity.id in activity_ids
298 assert direct_activity.id in activity_ids
302 |> get("/api/v1/notifications?exclude_visibilities[]=private")
303 |> json_response_and_validate_schema(200)
304 |> Enum.map(& &1["status"]["id"])
306 assert public_activity.id in activity_ids
307 assert unlisted_activity.id in activity_ids
308 refute private_activity.id in activity_ids
309 assert direct_activity.id in activity_ids
313 |> get("/api/v1/notifications?exclude_visibilities[]=public")
314 |> json_response_and_validate_schema(200)
315 |> Enum.map(& &1["status"]["id"])
317 refute public_activity.id in activity_ids
318 assert unlisted_activity.id in activity_ids
319 assert private_activity.id in activity_ids
320 assert direct_activity.id in activity_ids
323 test "filters notifications for Announce activities" do
325 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
327 {:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
329 {:ok, unlisted_activity} =
330 CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
332 {:ok, _} = CommonAPI.repeat(public_activity.id, user)
333 {:ok, _} = CommonAPI.repeat(unlisted_activity.id, user)
337 |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
338 |> json_response_and_validate_schema(200)
339 |> Enum.map(& &1["status"]["id"])
341 assert public_activity.id in activity_ids
342 refute unlisted_activity.id in activity_ids
345 test "doesn't return less than the requested amount of records when the user's reply is liked" do
347 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
350 CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "public"})
352 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
355 CommonAPI.post(other_user, %{
357 visibility: "public",
358 in_reply_to_status_id: activity.id
361 {:ok, _favorite} = CommonAPI.favorite(user, reply.id)
365 |> get("/api/v1/notifications?exclude_visibilities[]=direct&limit=2")
366 |> json_response_and_validate_schema(200)
367 |> Enum.map(& &1["status"]["id"])
369 assert [reply.id, mention.id] == activity_ids
373 test "filters notifications using exclude_types" do
374 %{user: user, conn: conn} = oauth_access(["read:notifications"])
375 other_user = insert(:user)
377 {:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
378 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
379 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
380 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
381 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
383 mention_notification_id = get_notification_id_by_activity(mention_activity)
384 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
385 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
386 follow_notification_id = get_notification_id_by_activity(follow_activity)
388 query = params_to_query(%{exclude_types: ["mention", "favourite", "reblog"]})
389 conn_res = get(conn, "/api/v1/notifications?" <> query)
391 assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
393 query = params_to_query(%{exclude_types: ["favourite", "reblog", "follow"]})
394 conn_res = get(conn, "/api/v1/notifications?" <> query)
396 assert [%{"id" => ^mention_notification_id}] =
397 json_response_and_validate_schema(conn_res, 200)
399 query = params_to_query(%{exclude_types: ["reblog", "follow", "mention"]})
400 conn_res = get(conn, "/api/v1/notifications?" <> query)
402 assert [%{"id" => ^favorite_notification_id}] =
403 json_response_and_validate_schema(conn_res, 200)
405 query = params_to_query(%{exclude_types: ["follow", "mention", "favourite"]})
406 conn_res = get(conn, "/api/v1/notifications?" <> query)
408 assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
411 test "filters notifications using include_types" do
412 %{user: user, conn: conn} = oauth_access(["read:notifications"])
413 other_user = insert(:user)
415 {:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
416 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
417 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
418 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
419 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
421 mention_notification_id = get_notification_id_by_activity(mention_activity)
422 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
423 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
424 follow_notification_id = get_notification_id_by_activity(follow_activity)
426 conn_res = get(conn, "/api/v1/notifications?include_types[]=follow")
428 assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
430 conn_res = get(conn, "/api/v1/notifications?include_types[]=mention")
432 assert [%{"id" => ^mention_notification_id}] =
433 json_response_and_validate_schema(conn_res, 200)
435 conn_res = get(conn, "/api/v1/notifications?include_types[]=favourite")
437 assert [%{"id" => ^favorite_notification_id}] =
438 json_response_and_validate_schema(conn_res, 200)
440 conn_res = get(conn, "/api/v1/notifications?include_types[]=reblog")
442 assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
444 result = conn |> get("/api/v1/notifications") |> json_response_and_validate_schema(200)
446 assert length(result) == 4
448 query = params_to_query(%{include_types: ["follow", "mention", "favourite", "reblog"]})
452 |> get("/api/v1/notifications?" <> query)
453 |> json_response_and_validate_schema(200)
455 assert length(result) == 4
458 test "destroy multiple" do
459 %{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
460 other_user = insert(:user)
462 {:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
463 {:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
464 {:ok, activity3} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
465 {:ok, activity4} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
467 notification1_id = get_notification_id_by_activity(activity1)
468 notification2_id = get_notification_id_by_activity(activity2)
469 notification3_id = get_notification_id_by_activity(activity3)
470 notification4_id = get_notification_id_by_activity(activity4)
474 |> get("/api/v1/notifications")
475 |> json_response_and_validate_schema(:ok)
477 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
481 |> assign(:user, other_user)
482 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
486 |> get("/api/v1/notifications")
487 |> json_response_and_validate_schema(:ok)
489 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
491 query = params_to_query(%{ids: [notification1_id, notification2_id]})
492 conn_destroy = delete(conn, "/api/v1/notifications/destroy_multiple?" <> query)
494 assert json_response_and_validate_schema(conn_destroy, 200) == %{}
498 |> get("/api/v1/notifications")
499 |> json_response_and_validate_schema(:ok)
501 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
504 test "doesn't see notifications after muting user with notifications" do
505 %{user: user, conn: conn} = oauth_access(["read:notifications"])
506 user2 = insert(:user)
508 {:ok, _, _, _} = CommonAPI.follow(user, user2)
509 {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
511 ret_conn = get(conn, "/api/v1/notifications")
513 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
515 {:ok, _user_relationships} = User.mute(user, user2)
517 conn = get(conn, "/api/v1/notifications")
519 assert json_response_and_validate_schema(conn, 200) == []
522 test "see notifications after muting user without notifications" do
523 %{user: user, conn: conn} = oauth_access(["read:notifications"])
524 user2 = insert(:user)
526 {:ok, _, _, _} = CommonAPI.follow(user, user2)
527 {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
529 ret_conn = get(conn, "/api/v1/notifications")
531 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
533 {:ok, _user_relationships} = User.mute(user, user2, %{notifications: false})
535 conn = get(conn, "/api/v1/notifications")
537 assert length(json_response_and_validate_schema(conn, 200)) == 1
540 test "see notifications after muting user with notifications and with_muted parameter" do
541 %{user: user, conn: conn} = oauth_access(["read:notifications"])
542 user2 = insert(:user)
544 {:ok, _, _, _} = CommonAPI.follow(user, user2)
545 {:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
547 ret_conn = get(conn, "/api/v1/notifications")
549 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
551 {:ok, _user_relationships} = User.mute(user, user2)
553 conn = get(conn, "/api/v1/notifications?with_muted=true")
555 assert length(json_response_and_validate_schema(conn, 200)) == 1
558 @tag capture_log: true
559 test "see move notifications" do
560 old_user = insert(:user)
561 new_user = insert(:user, also_known_as: [old_user.ap_id])
562 %{user: follower, conn: conn} = oauth_access(["read:notifications"])
564 old_user_url = old_user.ap_id
567 File.read!("test/fixtures/users_mock/localhost.json")
568 |> String.replace("{{nickname}}", old_user.nickname)
572 %{method: :get, url: ^old_user_url} ->
573 %Tesla.Env{status: 200, body: body}
576 User.follow(follower, old_user)
577 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
578 Pleroma.Tests.ObanHelpers.perform_all()
580 conn = get(conn, "/api/v1/notifications")
582 assert length(json_response_and_validate_schema(conn, 200)) == 1
585 describe "link headers" do
586 test "preserves parameters in link headers" do
587 %{user: user, conn: conn} = oauth_access(["read:notifications"])
588 other_user = insert(:user)
591 CommonAPI.post(other_user, %{
592 status: "hi @#{user.nickname}",
597 CommonAPI.post(other_user, %{
598 status: "hi @#{user.nickname}",
602 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
603 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
607 |> assign(:user, user)
608 |> get("/api/v1/notifications?limit=5")
610 assert [link_header] = get_resp_header(conn, "link")
611 assert link_header =~ ~r/limit=5/
612 assert link_header =~ ~r/min_id=#{notification2.id}/
613 assert link_header =~ ~r/max_id=#{notification1.id}/
617 describe "from specified user" do
619 %{user: user, conn: conn} = oauth_access(["read:notifications"])
621 %{id: account_id} = other_user1 = insert(:user)
622 other_user2 = insert(:user)
624 {:ok, _activity} = CommonAPI.post(other_user1, %{status: "hi @#{user.nickname}"})
625 {:ok, _activity} = CommonAPI.post(other_user2, %{status: "bye @#{user.nickname}"})
627 assert [%{"account" => %{"id" => ^account_id}}] =
629 |> assign(:user, user)
630 |> get("/api/v1/notifications?account_id=#{account_id}")
631 |> json_response_and_validate_schema(200)
633 assert %{"error" => "Account is not found"} =
635 |> assign(:user, user)
636 |> get("/api/v1/notifications?account_id=cofe")
637 |> json_response_and_validate_schema(404)
641 defp get_notification_id_by_activity(%{id: id}) do
643 |> Repo.get_by(activity_id: id)
648 defp params_to_query(%{} = params) do
649 Enum.map_join(params, "&", fn
650 {k, v} when is_list(v) -> Enum.map_join(v, "&", &"#{k}[]=#{&1}")
651 {k, v} -> k <> "=" <> v