0c9218454454768fee8f9335e1cd10c5e9f5d57a
[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(
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(conn, %{"account_id" => account_id} = params) do
27 case Pleroma.User.get_cached_by_id(account_id) do
28 %{ap_id: account_ap_id} ->
29 params =
30 params
31 |> Map.delete("account_id")
32 |> Map.put("account_ap_id", account_ap_id)
33
34 index(conn, params)
35
36 _ ->
37 conn
38 |> put_status(:not_found)
39 |> json(%{"error" => "Account is not found"})
40 end
41 end
42
43 def index(%{assigns: %{user: user}} = conn, params) do
44 notifications = MastodonAPI.get_notifications(user, params)
45
46 conn
47 |> add_link_headers(notifications)
48 |> render("index.json", notifications: notifications, for: user)
49 end
50
51 # GET /api/v1/notifications/:id
52 def show(%{assigns: %{user: user}} = conn, %{"id" => id}) do
53 with {:ok, notification} <- Notification.get(user, id) do
54 render(conn, "show.json", notification: notification, for: user)
55 else
56 {:error, reason} ->
57 conn
58 |> put_status(:forbidden)
59 |> json(%{"error" => reason})
60 end
61 end
62
63 # POST /api/v1/notifications/clear
64 def clear(%{assigns: %{user: user}} = conn, _params) do
65 Notification.clear(user)
66 json(conn, %{})
67 end
68
69 # POST /api/v1/notifications/dismiss
70 def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
71 with {:ok, _notif} <- Notification.dismiss(user, id) do
72 json(conn, %{})
73 else
74 {:error, reason} ->
75 conn
76 |> put_status(:forbidden)
77 |> json(%{"error" => reason})
78 end
79 end
80
81 # DELETE /api/v1/notifications/destroy_multiple
82 def destroy_multiple(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do
83 Notification.destroy_multiple(user, ids)
84 json(conn, %{})
85 end
86 end