Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.NotificationController do
  5. use Pleroma.Web, :controller
  6. import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
  7. alias Pleroma.Notification
  8. alias Pleroma.Web.MastodonAPI.MastodonAPI
  9. alias Pleroma.Web.Plugs.OAuthScopesPlug
  10. @oauth_read_actions [:show, :index]
  11. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  12. plug(
  13. OAuthScopesPlug,
  14. %{scopes: ["read:notifications"]} when action in @oauth_read_actions
  15. )
  16. plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action not in @oauth_read_actions)
  17. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.NotificationOperation
  18. # GET /api/v1/notifications
  19. def index(conn, %{account_id: account_id} = params) do
  20. case Pleroma.User.get_cached_by_id(account_id) do
  21. %{ap_id: account_ap_id} ->
  22. params =
  23. params
  24. |> Map.delete(:account_id)
  25. |> Map.put(:account_ap_id, account_ap_id)
  26. index(conn, params)
  27. _ ->
  28. conn
  29. |> put_status(:not_found)
  30. |> json(%{"error" => "Account is not found"})
  31. end
  32. end
  33. @default_notification_types ~w{
  34. mention
  35. follow
  36. follow_request
  37. reblog
  38. favourite
  39. move
  40. pleroma:emoji_reaction
  41. }
  42. def index(%{assigns: %{user: user}} = conn, params) do
  43. params =
  44. Map.new(params, fn {k, v} -> {to_string(k), v} end)
  45. |> Map.put_new("include_types", @default_notification_types)
  46. notifications = MastodonAPI.get_notifications(user, params)
  47. conn
  48. |> add_link_headers(notifications)
  49. |> render("index.json",
  50. notifications: notifications,
  51. for: user
  52. )
  53. end
  54. # GET /api/v1/notifications/:id
  55. def show(%{assigns: %{user: user}} = conn, %{id: id}) do
  56. with {:ok, notification} <- Notification.get(user, id) do
  57. render(conn, "show.json", notification: notification, for: user)
  58. else
  59. {:error, reason} ->
  60. conn
  61. |> put_status(:forbidden)
  62. |> json(%{"error" => reason})
  63. end
  64. end
  65. # POST /api/v1/notifications/clear
  66. def clear(%{assigns: %{user: user}} = conn, _params) do
  67. Notification.clear(user)
  68. json(conn, %{})
  69. end
  70. # POST /api/v1/notifications/:id/dismiss
  71. def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
  72. with {:ok, _notif} <- Notification.dismiss(user, id) do
  73. json(conn, %{})
  74. else
  75. {:error, reason} ->
  76. conn
  77. |> put_status(:forbidden)
  78. |> json(%{"error" => reason})
  79. end
  80. end
  81. # POST /api/v1/notifications/dismiss (deprecated)
  82. def dismiss_via_body(%{body_params: params} = conn, _) do
  83. dismiss(conn, params)
  84. end
  85. # DELETE /api/v1/notifications/destroy_multiple
  86. def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
  87. Notification.destroy_multiple(user, ids)
  88. json(conn, %{})
  89. end
  90. end