[#1234] Merge remote-tracking branch 'remotes/upstream/develop' into 1234-mastodon...
[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 # GET /api/v1/notifications
24 def index(%{assigns: %{user: user}} = conn, params) do
25 notifications = MastodonAPI.get_notifications(user, params)
26
27 conn
28 |> add_link_headers(notifications)
29 |> render("index.json", notifications: notifications, for: user)
30 end
31
32 # GET /api/v1/notifications/:id
33 def show(%{assigns: %{user: user}} = conn, %{"id" => id}) do
34 with {:ok, notification} <- Notification.get(user, id) do
35 render(conn, "show.json", notification: notification, for: user)
36 else
37 {:error, reason} ->
38 conn
39 |> put_status(:forbidden)
40 |> json(%{"error" => reason})
41 end
42 end
43
44 # POST /api/v1/notifications/clear
45 def clear(%{assigns: %{user: user}} = conn, _params) do
46 Notification.clear(user)
47 json(conn, %{})
48 end
49
50 # POST /api/v1/notifications/dismiss
51 def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
52 with {:ok, _notif} <- Notification.dismiss(user, id) do
53 json(conn, %{})
54 else
55 {:error, reason} ->
56 conn
57 |> put_status(:forbidden)
58 |> json(%{"error" => reason})
59 end
60 end
61
62 # DELETE /api/v1/notifications/destroy_multiple
63 def destroy_multiple(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do
64 Notification.destroy_multiple(user, ids)
65 json(conn, %{})
66 end
67 end