Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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, skip_relationships?: 1]
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",
49 notifications: notifications,
50 for: user,
51 skip_relationships: skip_relationships?(params)
52 )
53 end
54
55 # GET /api/v1/notifications/:id
56 def show(%{assigns: %{user: user}} = conn, %{"id" => id}) do
57 with {:ok, notification} <- Notification.get(user, id) do
58 render(conn, "show.json", notification: notification, for: user)
59 else
60 {:error, reason} ->
61 conn
62 |> put_status(:forbidden)
63 |> json(%{"error" => reason})
64 end
65 end
66
67 # POST /api/v1/notifications/clear
68 def clear(%{assigns: %{user: user}} = conn, _params) do
69 Notification.clear(user)
70 json(conn, %{})
71 end
72
73 # POST /api/v1/notifications/:id/dismiss
74 # POST /api/v1/notifications/dismiss (deprecated)
75 def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
76 with {:ok, _notif} <- Notification.dismiss(user, id) do
77 json(conn, %{})
78 else
79 {:error, reason} ->
80 conn
81 |> put_status(:forbidden)
82 |> json(%{"error" => reason})
83 end
84 end
85
86 # DELETE /api/v1/notifications/destroy_multiple
87 def destroy_multiple(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do
88 Notification.destroy_multiple(user, ids)
89 json(conn, %{})
90 end
91 end