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]
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 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
25 # GET /api/v1/notifications
26 def index(conn, %{"account_id" => account_id} = params) do
27 case Pleroma.User.get_cached_by_id(account_id) do
28 %{ap_id: account_ap_id} ->
31 |> Map.delete("account_id")
32 |> Map.put("account_ap_id", account_ap_id)
38 |> put_status(:not_found)
39 |> json(%{"error" => "Account is not found"})
43 def index(%{assigns: %{user: user}} = conn, params) do
44 notifications = MastodonAPI.get_notifications(user, params)
47 |> add_link_headers(notifications)
48 |> render("index.json", notifications: notifications, for: user)
51 # GET /api/v1/notifications/:id
52 def show(%{assigns: %{user: user}} = conn, %{"id" => id}) do
53 with {:ok, notification} <- Notification.get(user, id) do
54 render(conn, "show.json", notification: notification, for: user)
58 |> put_status(:forbidden)
59 |> json(%{"error" => reason})
63 # POST /api/v1/notifications/clear
64 def clear(%{assigns: %{user: user}} = conn, _params) do
65 Notification.clear(user)
69 # POST /api/v1/notifications/dismiss
70 def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
71 with {:ok, _notif} <- Notification.dismiss(user, id) do
76 |> put_status(:forbidden)
77 |> json(%{"error" => reason})
81 # DELETE /api/v1/notifications/destroy_multiple
82 def destroy_multiple(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do
83 Notification.destroy_multiple(user, ids)