Add `account_activation_required` to /api/v1/instance
[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]
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(Pleroma.Web.ApiSpec.CastAndValidate)
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 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.NotificationOperation
26
27 # GET /api/v1/notifications
28 def index(conn, %{account_id: account_id} = params) do
29 case Pleroma.User.get_cached_by_id(account_id) do
30 %{ap_id: account_ap_id} ->
31 params =
32 params
33 |> Map.delete(:account_id)
34 |> Map.put(:account_ap_id, account_ap_id)
35
36 index(conn, params)
37
38 _ ->
39 conn
40 |> put_status(:not_found)
41 |> json(%{"error" => "Account is not found"})
42 end
43 end
44
45 def index(%{assigns: %{user: user}} = conn, params) do
46 params = Map.new(params, fn {k, v} -> {to_string(k), v} end)
47 notifications = MastodonAPI.get_notifications(user, params)
48
49 conn
50 |> add_link_headers(notifications)
51 |> render("index.json",
52 notifications: notifications,
53 for: user
54 )
55 end
56
57 # GET /api/v1/notifications/:id
58 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
59 with {:ok, notification} <- Notification.get(user, id) do
60 render(conn, "show.json", notification: notification, for: user)
61 else
62 {:error, reason} ->
63 conn
64 |> put_status(:forbidden)
65 |> json(%{"error" => reason})
66 end
67 end
68
69 # POST /api/v1/notifications/clear
70 def clear(%{assigns: %{user: user}} = conn, _params) do
71 Notification.clear(user)
72 json(conn, %{})
73 end
74
75 # POST /api/v1/notifications/:id/dismiss
76
77 def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
78 with {:ok, _notif} <- Notification.dismiss(user, id) do
79 json(conn, %{})
80 else
81 {:error, reason} ->
82 conn
83 |> put_status(:forbidden)
84 |> json(%{"error" => reason})
85 end
86 end
87
88 # POST /api/v1/notifications/dismiss (deprecated)
89 def dismiss_via_body(%{body_params: params} = conn, _) do
90 dismiss(conn, params)
91 end
92
93 # DELETE /api/v1/notifications/destroy_multiple
94 def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
95 Notification.destroy_multiple(user, ids)
96 json(conn, %{})
97 end
98 end