2019-09-24 10:16:44 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 06:08:45 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-24 10:16:44 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.NotificationController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2020-04-01 18:49:09 +02:00
|
|
|
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2, skip_relationships?: 1]
|
2019-09-24 10:16:44 +02:00
|
|
|
|
|
|
|
alias Pleroma.Notification
|
2019-10-02 19:42:40 +02:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-09-24 10:16:44 +02:00
|
|
|
alias Pleroma.Web.MastodonAPI.MastodonAPI
|
|
|
|
|
2019-10-02 19:42:40 +02:00
|
|
|
@oauth_read_actions [:show, :index]
|
|
|
|
|
2020-04-28 19:27:54 +02:00
|
|
|
plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
|
|
|
|
|
2019-10-02 19:42:40 +02:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:notifications"]} when action in @oauth_read_actions
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action not in @oauth_read_actions)
|
|
|
|
|
2020-04-28 19:27:54 +02:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.NotificationOperation
|
|
|
|
|
2019-09-24 10:16:44 +02:00
|
|
|
# GET /api/v1/notifications
|
2020-04-28 19:27:54 +02:00
|
|
|
def index(conn, %{account_id: account_id} = params) do
|
2019-12-19 14:45:44 +01:00
|
|
|
case Pleroma.User.get_cached_by_id(account_id) do
|
|
|
|
%{ap_id: account_ap_id} ->
|
|
|
|
params =
|
|
|
|
params
|
2020-04-28 19:27:54 +02:00
|
|
|
|> Map.delete(:account_id)
|
|
|
|
|> Map.put(:account_ap_id, account_ap_id)
|
2019-12-19 14:45:44 +01:00
|
|
|
|
|
|
|
index(conn, params)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{"error" => "Account is not found"})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-24 10:16:44 +02:00
|
|
|
def index(%{assigns: %{user: user}} = conn, params) do
|
2020-04-28 19:27:54 +02:00
|
|
|
params = Map.new(params, fn {k, v} -> {to_string(k), v} end)
|
2019-09-24 10:16:44 +02:00
|
|
|
notifications = MastodonAPI.get_notifications(user, params)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(notifications)
|
2020-04-01 18:49:09 +02:00
|
|
|
|> render("index.json",
|
|
|
|
notifications: notifications,
|
|
|
|
for: user,
|
|
|
|
skip_relationships: skip_relationships?(params)
|
|
|
|
)
|
2019-09-24 10:16:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/notifications/:id
|
2020-04-28 19:27:54 +02:00
|
|
|
def show(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-24 10:16:44 +02:00
|
|
|
with {:ok, notification} <- Notification.get(user, id) do
|
|
|
|
render(conn, "show.json", notification: notification, for: user)
|
|
|
|
else
|
|
|
|
{:error, reason} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:forbidden)
|
|
|
|
|> json(%{"error" => reason})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /api/v1/notifications/clear
|
|
|
|
def clear(%{assigns: %{user: user}} = conn, _params) do
|
|
|
|
Notification.clear(user)
|
|
|
|
json(conn, %{})
|
|
|
|
end
|
|
|
|
|
2020-04-09 15:08:43 +02:00
|
|
|
# POST /api/v1/notifications/:id/dismiss
|
2020-04-28 19:27:54 +02:00
|
|
|
|
|
|
|
def dismiss(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
|
2019-09-24 10:16:44 +02:00
|
|
|
with {:ok, _notif} <- Notification.dismiss(user, id) do
|
|
|
|
json(conn, %{})
|
|
|
|
else
|
|
|
|
{:error, reason} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:forbidden)
|
|
|
|
|> json(%{"error" => reason})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-28 19:27:54 +02:00
|
|
|
# POST /api/v1/notifications/dismiss (deprecated)
|
|
|
|
def dismiss_via_body(%{body_params: params} = conn, _) do
|
|
|
|
dismiss(conn, params)
|
|
|
|
end
|
|
|
|
|
2019-09-24 10:16:44 +02:00
|
|
|
# DELETE /api/v1/notifications/destroy_multiple
|
2020-04-28 19:27:54 +02:00
|
|
|
def destroy_multiple(%{assigns: %{user: user}} = conn, %{ids: ids} = _params) do
|
2019-09-24 10:16:44 +02:00
|
|
|
Notification.destroy_multiple(user, ids)
|
|
|
|
json(conn, %{})
|
|
|
|
end
|
|
|
|
end
|