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
55 def index(%{assigns: %{user: user}} = conn, params) do
57 Map.new(params, fn {k, v} -> {to_string(k), v} end)
58 |> Map.put_new("include_types", @default_notification_types)
60 notifications = MastodonAPI.get_notifications(user, params)
63 |> add_link_headers(notifications)
64 |> render("index.json",
65 notifications: notifications,
70 # GET /api/v1/notifications/:id
71 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
72 with {:ok, notification} <- Notification.get(user, id) do
73 render(conn, "show.json", notification: notification, for: user)
77 |> put_status(:forbidden)
78 |> json(%{"error" => reason})
82 # POST /api/v1/notifications/clear
83 def clear(%{assigns: %{user: user}} = conn, _params) do
84 Notification.clear(user)
88 # POST /api/v1/notifications/:id/dismiss
90 def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
91 with {:ok, _notif} <- Notification.dismiss(user, id) do
96 |> put_status(:forbidden)
97 |> json(%{"error" => reason})
101 # DELETE /api/v1/notifications/destroy_multiple
102 def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
103 Notification.destroy_multiple(user, ids)