1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
10 alias Pleroma.Notification
11 alias Pleroma.Web.MastodonAPI.MastodonAPI
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
14 @oauth_read_actions [:show, :index]
16 plug(Pleroma.Web.ApiSpec.CastAndValidate)
20 %{scopes: ["read:notifications"]} when action in @oauth_read_actions
23 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action not in @oauth_read_actions)
25 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.NotificationOperation
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} ->
33 |> Map.delete(:account_id)
34 |> Map.put(:account_ap_id, account_ap_id)
40 |> put_status(:not_found)
41 |> json(%{"error" => "Account is not found"})
45 @default_notification_types ~w{
52 pleroma:emoji_reaction
56 def index(%{assigns: %{user: user}} = conn, params) do
58 Map.new(params, fn {k, v} -> {to_string(k), v} end)
59 |> Map.put_new("include_types", @default_notification_types)
61 notifications = MastodonAPI.get_notifications(user, params)
64 |> add_link_headers(notifications)
65 |> render("index.json",
66 notifications: notifications,
71 # GET /api/v1/notifications/:id
72 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
73 with {:ok, notification} <- Notification.get(user, id) do
74 render(conn, "show.json", notification: notification, for: user)
78 |> put_status(:forbidden)
79 |> json(%{"error" => reason})
83 # POST /api/v1/notifications/clear
84 def clear(%{assigns: %{user: user}} = conn, _params) do
85 Notification.clear(user)
89 # POST /api/v1/notifications/:id/dismiss
91 def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
92 with {:ok, _notif} <- Notification.dismiss(user, id) do
97 |> put_status(:forbidden)
98 |> json(%{"error" => reason})
102 # DELETE /api/v1/notifications/destroy_multiple
103 def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
104 Notification.destroy_multiple(user, ids)