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