logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

notification_controller.ex (3571B)


  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. 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, replace_params: false)
  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. @default_notification_types ~w{
  19. mention
  20. follow
  21. follow_request
  22. reblog
  23. favourite
  24. move
  25. pleroma:emoji_reaction
  26. poll
  27. update
  28. }
  29. # GET /api/v1/notifications
  30. def index(%{private: %{open_api_spex: %{params: %{account_id: account_id} = params}}} = conn, _) do
  31. case Pleroma.User.get_cached_by_id(account_id) do
  32. %{ap_id: account_ap_id} ->
  33. params =
  34. params
  35. |> Map.delete(:account_id)
  36. |> Map.put(:account_ap_id, account_ap_id)
  37. do_get_notifications(conn, params)
  38. _ ->
  39. conn
  40. |> put_status(:not_found)
  41. |> json(%{"error" => "Account is not found"})
  42. end
  43. end
  44. def index(%{private: %{open_api_spex: %{params: params}}} = conn, _) do
  45. do_get_notifications(conn, params)
  46. end
  47. defp do_get_notifications(%{assigns: %{user: user}} = conn, params) do
  48. params =
  49. Map.new(params, fn {k, v} -> {to_string(k), v} end)
  50. |> Map.put_new("types", Map.get(params, :include_types, @default_notification_types))
  51. notifications = MastodonAPI.get_notifications(user, params)
  52. conn
  53. |> add_link_headers(notifications)
  54. |> render("index.json",
  55. notifications: notifications,
  56. for: user
  57. )
  58. end
  59. # GET /api/v1/notifications/:id
  60. def show(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
  61. with {:ok, notification} <- Notification.get(user, id) do
  62. render(conn, "show.json", notification: notification, for: user)
  63. else
  64. {:error, reason} ->
  65. conn
  66. |> put_status(:forbidden)
  67. |> json(%{"error" => reason})
  68. end
  69. end
  70. # POST /api/v1/notifications/clear
  71. def clear(%{assigns: %{user: user}} = conn, _params) do
  72. Notification.clear(user)
  73. json(conn, %{})
  74. end
  75. # POST /api/v1/notifications/:id/dismiss
  76. def dismiss(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
  77. do_dismiss(conn, id)
  78. end
  79. # POST /api/v1/notifications/dismiss (deprecated)
  80. def dismiss_via_body(
  81. %{private: %{open_api_spex: %{body_params: %{id: id}}}} = conn,
  82. _
  83. ) do
  84. do_dismiss(conn, id)
  85. end
  86. defp do_dismiss(%{assigns: %{user: user}} = conn, notification_id) do
  87. with {:ok, _notif} <- Notification.dismiss(user, notification_id) do
  88. json(conn, %{})
  89. else
  90. {:error, reason} ->
  91. conn
  92. |> put_status(:forbidden)
  93. |> json(%{"error" => reason})
  94. end
  95. end
  96. # DELETE /api/v1/notifications/destroy_multiple
  97. def destroy_multiple(
  98. %{assigns: %{user: user}, private: %{open_api_spex: %{params: %{ids: ids}}}} = conn,
  99. _
  100. ) do
  101. Notification.destroy_multiple(user, ids)
  102. json(conn, %{})
  103. end
  104. end