remove all endpoints marked as deprecated (#91)
[akkoma] / lib / pleroma / web / mastodon_api / controllers / notification_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.Web.MastodonAPI.MastodonAPI
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
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 @default_notification_types ~w{
46 mention
47 follow
48 follow_request
49 reblog
50 favourite
51 move
52 pleroma:emoji_reaction
53 poll
54 }
55 def index(%{assigns: %{user: user}} = conn, params) do
56 params =
57 Map.new(params, fn {k, v} -> {to_string(k), v} end)
58 |> Map.put_new("include_types", @default_notification_types)
59
60 notifications = MastodonAPI.get_notifications(user, params)
61
62 conn
63 |> add_link_headers(notifications)
64 |> render("index.json",
65 notifications: notifications,
66 for: user
67 )
68 end
69
70 # GET /api/v1/notifications/:id
71 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
72 with {:ok, notification} <- Notification.get(user, id) do
73 render(conn, "show.json", notification: notification, for: user)
74 else
75 {:error, reason} ->
76 conn
77 |> put_status(:forbidden)
78 |> json(%{"error" => reason})
79 end
80 end
81
82 # POST /api/v1/notifications/clear
83 def clear(%{assigns: %{user: user}} = conn, _params) do
84 Notification.clear(user)
85 json(conn, %{})
86 end
87
88 # POST /api/v1/notifications/:id/dismiss
89
90 def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
91 with {:ok, _notif} <- Notification.dismiss(user, id) do
92 json(conn, %{})
93 else
94 {:error, reason} ->
95 conn
96 |> put_status(:forbidden)
97 |> json(%{"error" => reason})
98 end
99 end
100
101 # DELETE /api/v1/notifications/destroy_multiple
102 def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
103 Notification.destroy_multiple(user, ids)
104 json(conn, %{})
105 end
106 end