1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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, replace_params: false)
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 @default_notification_types ~w{
34 pleroma:emoji_reaction
39 # GET /api/v1/notifications
40 def index(%{private: %{open_api_spex: %{params: %{account_id: account_id} = params}}} = conn, _) do
41 case Pleroma.User.get_cached_by_id(account_id) do
42 %{ap_id: account_ap_id} ->
45 |> Map.delete(:account_id)
46 |> Map.put(:account_ap_id, account_ap_id)
48 do_get_notifications(conn, params)
52 |> put_status(:not_found)
53 |> json(%{"error" => "Account is not found"})
57 def index(%{private: %{open_api_spex: %{params: params}}} = conn, _) do
58 do_get_notifications(conn, params)
61 defp do_get_notifications(%{assigns: %{user: user}} = conn, params) do
63 Map.new(params, fn {k, v} -> {to_string(k), v} end)
64 |> Map.put_new("types", Map.get(params, :include_types, @default_notification_types))
66 notifications = MastodonAPI.get_notifications(user, params)
69 |> add_link_headers(notifications)
70 |> render("index.json",
71 notifications: notifications,
76 # GET /api/v1/notifications/:id
77 def show(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
78 with {:ok, notification} <- Notification.get(user, id) do
79 render(conn, "show.json", notification: notification, for: user)
83 |> put_status(:forbidden)
84 |> json(%{"error" => reason})
88 # POST /api/v1/notifications/clear
89 def clear(%{assigns: %{user: user}} = conn, _params) do
90 Notification.clear(user)
94 # POST /api/v1/notifications/:id/dismiss
96 def dismiss(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
100 # POST /api/v1/notifications/dismiss (deprecated)
101 def dismiss_via_body(
102 %{private: %{open_api_spex: %{body_params: %{id: id}}}} = conn,
108 defp do_dismiss(%{assigns: %{user: user}} = conn, notification_id) do
109 with {:ok, _notif} <- Notification.dismiss(user, notification_id) do
114 |> put_status(:forbidden)
115 |> json(%{"error" => reason})
119 # DELETE /api/v1/notifications/destroy_multiple
120 def destroy_multiple(
121 %{assigns: %{user: user}, private: %{open_api_spex: %{params: %{ids: ids}}}} = conn,
124 Notification.destroy_multiple(user, ids)