1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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, skip_relationships?: 1]
10 alias Pleroma.Notification
11 alias Pleroma.Plugs.OAuthScopesPlug
12 alias Pleroma.Web.MastodonAPI.MastodonAPI
14 @oauth_read_actions [:show, :index]
16 plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
20 %{scopes: ["read:notifications"]} when action in @oauth_read_actions
23 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action not in @oauth_read_actions)
25 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
27 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.NotificationOperation
29 # GET /api/v1/notifications
30 def index(conn, %{account_id: account_id} = params) do
31 case Pleroma.User.get_cached_by_id(account_id) do
32 %{ap_id: account_ap_id} ->
35 |> Map.delete(:account_id)
36 |> Map.put(:account_ap_id, account_ap_id)
42 |> put_status(:not_found)
43 |> json(%{"error" => "Account is not found"})
47 def index(%{assigns: %{user: user}} = conn, params) do
48 params = Map.new(params, fn {k, v} -> {to_string(k), v} end)
49 notifications = MastodonAPI.get_notifications(user, params)
52 |> add_link_headers(notifications)
53 |> render("index.json",
54 notifications: notifications,
56 skip_relationships: skip_relationships?(params)
60 # GET /api/v1/notifications/:id
61 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
62 with {:ok, notification} <- Notification.get(user, id) do
63 render(conn, "show.json", notification: notification, for: user)
67 |> put_status(:forbidden)
68 |> json(%{"error" => reason})
72 # POST /api/v1/notifications/clear
73 def clear(%{assigns: %{user: user}} = conn, _params) do
74 Notification.clear(user)
78 # POST /api/v1/notifications/:id/dismiss
80 def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
81 with {:ok, _notif} <- Notification.dismiss(user, id) do
86 |> put_status(:forbidden)
87 |> json(%{"error" => reason})
91 # POST /api/v1/notifications/dismiss (deprecated)
92 def dismiss_via_body(%{body_params: params} = conn, _) do
96 # DELETE /api/v1/notifications/destroy_multiple
97 def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
98 Notification.destroy_multiple(user, ids)