2020-08-31 18:56:05 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-08-31 18:56:05 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.AdminAPI.ChatController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
2020-09-01 18:39:34 -06:00
|
|
|
alias Pleroma.Chat
|
|
|
|
alias Pleroma.Chat.MessageReference
|
|
|
|
alias Pleroma.Pagination
|
2020-09-10 00:44:32 -06:00
|
|
|
alias Pleroma.Web.AdminAPI
|
2020-08-31 18:56:05 -06:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2020-09-01 18:39:34 -06:00
|
|
|
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
|
2020-09-19 12:25:01 -06:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2020-08-31 18:56:05 -06:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2020-09-01 18:39:34 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2021-02-17 11:37:23 -07:00
|
|
|
%{scopes: ["admin:read:chats"]} when action in [:show, :messages]
|
2020-09-01 18:39:34 -06:00
|
|
|
)
|
|
|
|
|
2020-08-31 18:56:05 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2021-02-17 11:37:23 -07:00
|
|
|
%{scopes: ["admin:write:chats"]} when action in [:delete_message]
|
2020-08-31 18:56:05 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
|
|
|
|
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ChatOperation
|
|
|
|
|
2020-09-01 19:40:36 -06:00
|
|
|
def delete_message(%{assigns: %{user: user}} = conn, %{
|
|
|
|
message_id: message_id,
|
|
|
|
id: chat_id
|
|
|
|
}) do
|
|
|
|
with %MessageReference{object: %{data: %{"id" => object_ap_id}}} = cm_ref <-
|
|
|
|
MessageReference.get_by_id(message_id),
|
|
|
|
^chat_id <- to_string(cm_ref.chat_id),
|
|
|
|
%Activity{id: activity_id} <- Activity.get_create_by_object_ap_id(object_ap_id),
|
|
|
|
{:ok, _} <- CommonAPI.delete(activity_id, user) do
|
|
|
|
conn
|
|
|
|
|> put_view(MessageReferenceView)
|
|
|
|
|> render("show.json", chat_message_reference: cm_ref)
|
|
|
|
else
|
|
|
|
_e ->
|
|
|
|
{:error, :could_not_delete}
|
2020-08-31 18:56:05 -06:00
|
|
|
end
|
|
|
|
end
|
2020-09-01 18:39:34 -06:00
|
|
|
|
|
|
|
def messages(conn, %{id: id} = params) do
|
|
|
|
with %Chat{} = chat <- Chat.get_by_id(id) do
|
|
|
|
cm_refs =
|
|
|
|
chat
|
|
|
|
|> MessageReference.for_chat_query()
|
|
|
|
|> Pagination.fetch_paginated(params)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(MessageReferenceView)
|
|
|
|
|> render("index.json", chat_message_references: cm_refs)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{error: "not found"})
|
|
|
|
end
|
|
|
|
end
|
2020-09-01 18:49:46 -06:00
|
|
|
|
|
|
|
def show(conn, %{id: id}) do
|
|
|
|
with %Chat{} = chat <- Chat.get_by_id(id) do
|
|
|
|
conn
|
2020-09-10 00:44:32 -06:00
|
|
|
|> put_view(AdminAPI.ChatView)
|
2020-09-01 18:49:46 -06:00
|
|
|
|> render("show.json", chat: chat)
|
|
|
|
end
|
|
|
|
end
|
2020-08-31 18:56:05 -06:00
|
|
|
end
|