Add OpenAPI spec for NotificationController
[akkoma] / lib / pleroma / web / mastodon_api / controllers / notification_controller.ex
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
5 defmodule Pleroma.Web.MastodonAPI.NotificationController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2, skip_relationships?: 1]
9
10 alias Pleroma.Notification
11 alias Pleroma.Plugs.OAuthScopesPlug
12 alias Pleroma.Web.MastodonAPI.MastodonAPI
13
14 @oauth_read_actions [:show, :index]
15
16 plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
17
18 plug(
19 OAuthScopesPlug,
20 %{scopes: ["read:notifications"]} when action in @oauth_read_actions
21 )
22
23 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action not in @oauth_read_actions)
24
25 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
26
27 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.NotificationOperation
28
29 # GET /api/v1/notifications
30 def index(conn, %{account_id: account_id} = params) 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
38 index(conn, params)
39
40 _ ->
41 conn
42 |> put_status(:not_found)
43 |> json(%{"error" => "Account is not found"})
44 end
45 end
46
47 def index(%{assigns: %{user: user}} = conn, params) do
48 params = Map.new(params, fn {k, v} -> {to_string(k), v} end)
49 notifications = MastodonAPI.get_notifications(user, params)
50
51 conn
52 |> add_link_headers(notifications)
53 |> render("index.json",
54 notifications: notifications,
55 for: user,
56 skip_relationships: skip_relationships?(params)
57 )
58 end
59
60 # GET /api/v1/notifications/:id
61 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
62 with {:ok, notification} <- Notification.get(user, id) do
63 render(conn, "show.json", notification: notification, for: user)
64 else
65 {:error, reason} ->
66 conn
67 |> put_status(:forbidden)
68 |> json(%{"error" => reason})
69 end
70 end
71
72 # POST /api/v1/notifications/clear
73 def clear(%{assigns: %{user: user}} = conn, _params) do
74 Notification.clear(user)
75 json(conn, %{})
76 end
77
78 # POST /api/v1/notifications/:id/dismiss
79
80 def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
81 with {:ok, _notif} <- Notification.dismiss(user, id) do
82 json(conn, %{})
83 else
84 {:error, reason} ->
85 conn
86 |> put_status(:forbidden)
87 |> json(%{"error" => reason})
88 end
89 end
90
91 # POST /api/v1/notifications/dismiss (deprecated)
92 def dismiss_via_body(%{body_params: params} = conn, _) do
93 dismiss(conn, params)
94 end
95
96 # DELETE /api/v1/notifications/destroy_multiple
97 def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
98 Notification.destroy_multiple(user, ids)
99 json(conn, %{})
100 end
101 end