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