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]
18 %{scopes: ["read:notifications"]} when action in @oauth_read_actions
21 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action not in @oauth_read_actions)
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} ->
29 |> Map.delete("account_id")
30 |> Map.put("account_ap_id", account_ap_id)
36 |> put_status(:not_found)
37 |> json(%{"error" => "Account is not found"})
41 def index(%{assigns: %{user: user}} = conn, params) do
42 notifications = MastodonAPI.get_notifications(user, params)
45 |> add_link_headers(notifications)
46 |> render("index.json",
47 notifications: notifications,
49 skip_relationships: skip_relationships?(params)
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)
60 |> put_status(:forbidden)
61 |> json(%{"error" => reason})
65 # POST /api/v1/notifications/clear
66 def clear(%{assigns: %{user: user}} = conn, _params) do
67 Notification.clear(user)
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
79 |> put_status(:forbidden)
80 |> json(%{"error" => reason})
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)