branch
[akkoma] / lib / pleroma / web / mastodon_api / controllers / notification_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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(
17 OAuthScopesPlug,
18 %{scopes: ["read:notifications"]} when action in @oauth_read_actions
19 )
20
21 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action not in @oauth_read_actions)
22
23 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
24
25 # GET /api/v1/notifications
26 def index(%{assigns: %{user: user}} = conn, params) do
27 notifications = MastodonAPI.get_notifications(user, params)
28
29 conn
30 |> add_link_headers(notifications)
31 |> render("index.json", notifications: notifications, for: user)
32 end
33
34 # GET /api/v1/notifications/:id
35 def show(%{assigns: %{user: user}} = conn, %{"id" => id}) do
36 with {:ok, notification} <- Notification.get(user, id) do
37 render(conn, "show.json", notification: notification, for: user)
38 else
39 {:error, reason} ->
40 conn
41 |> put_status(:forbidden)
42 |> json(%{"error" => reason})
43 end
44 end
45
46 # POST /api/v1/notifications/clear
47 def clear(%{assigns: %{user: user}} = conn, _params) do
48 Notification.clear(user)
49 json(conn, %{})
50 end
51
52 # POST /api/v1/notifications/dismiss
53 def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
54 with {:ok, _notif} <- Notification.dismiss(user, id) do
55 json(conn, %{})
56 else
57 {:error, reason} ->
58 conn
59 |> put_status(:forbidden)
60 |> json(%{"error" => reason})
61 end
62 end
63
64 # DELETE /api/v1/notifications/destroy_multiple
65 def destroy_multiple(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do
66 Notification.destroy_multiple(user, ids)
67 json(conn, %{})
68 end
69 end