2020-08-31 18:56:05 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# 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
|
2020-08-31 18:56:05 -06:00
|
|
|
alias Pleroma.ModerationLog
|
2020-09-01 18:39:34 -06:00
|
|
|
alias Pleroma.Pagination
|
2020-08-31 18:56:05 -06:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
|
|
|
alias Pleroma.Web.CommonAPI
|
2020-09-01 18:39:34 -06:00
|
|
|
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
|
2020-09-01 18:49:46 -06:00
|
|
|
alias Pleroma.Web.PleromaAPI.ChatView
|
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,
|
2020-09-01 18:49:46 -06:00
|
|
|
%{scopes: ["read:chats"], admin: true} when action in [:show, :messages]
|
2020-09-01 18:39:34 -06:00
|
|
|
)
|
|
|
|
|
2020-08-31 18:56:05 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:chats"], admin: true} when action in [:delete_message]
|
|
|
|
)
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
|
|
|
|
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ChatOperation
|
|
|
|
|
|
|
|
def delete_message(%{assigns: %{user: user}} = conn, %{message_id: id}) do
|
|
|
|
with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
|
|
|
|
ModerationLog.insert_log(%{
|
|
|
|
action: "chat_message_delete",
|
|
|
|
actor: user,
|
|
|
|
subject_id: id
|
|
|
|
})
|
|
|
|
|
|
|
|
json(conn, %{})
|
|
|
|
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
|
|
|
|
|> put_view(ChatView)
|
|
|
|
|> render("show.json", chat: chat)
|
|
|
|
end
|
|
|
|
end
|
2020-08-31 18:56:05 -06:00
|
|
|
end
|