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