Merge branch 'ecto-rollback-in-test-env' into 'develop'
[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 }
54 def index(%{assigns: %{user: user}} = conn, params) do
55 params =
56 Map.new(params, fn {k, v} -> {to_string(k), v} end)
57 |> Map.put_new("include_types", @default_notification_types)
58
59 notifications = MastodonAPI.get_notifications(user, params)
60
61 conn
62 |> add_link_headers(notifications)
63 |> render("index.json",
64 notifications: notifications,
65 for: user
66 )
67 end
68
69 # GET /api/v1/notifications/:id
70 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
71 with {:ok, notification} <- Notification.get(user, id) do
72 render(conn, "show.json", notification: notification, for: user)
73 else
74 {:error, reason} ->
75 conn
76 |> put_status(:forbidden)
77 |> json(%{"error" => reason})
78 end
79 end
80
81 # POST /api/v1/notifications/clear
82 def clear(%{assigns: %{user: user}} = conn, _params) do
83 Notification.clear(user)
84 json(conn, %{})
85 end
86
87 # POST /api/v1/notifications/:id/dismiss
88
89 def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
90 with {:ok, _notif} <- Notification.dismiss(user, id) do
91 json(conn, %{})
92 else
93 {:error, reason} ->
94 conn
95 |> put_status(:forbidden)
96 |> json(%{"error" => reason})
97 end
98 end
99
100 # POST /api/v1/notifications/dismiss (deprecated)
101 def dismiss_via_body(%{body_params: params} = conn, _) do
102 dismiss(conn, params)
103 end
104
105 # DELETE /api/v1/notifications/destroy_multiple
106 def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
107 Notification.destroy_multiple(user, ids)
108 json(conn, %{})
109 end
110 end