AdminAPI: show chat

This commit is contained in:
Alex Gleason 2020-09-01 19:49:46 -05:00
parent f13b52a703
commit 9dd0b23da4
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 59 additions and 2 deletions

View file

@ -13,6 +13,7 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
alias Pleroma.Web.PleromaAPI.ChatView
require Logger
@ -20,7 +21,7 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
plug(
OAuthScopesPlug,
%{scopes: ["read:chats"], admin: true} when action in [:messages]
%{scopes: ["read:chats"], admin: true} when action in [:show, :messages]
)
plug(
@ -61,4 +62,12 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
|> json(%{error: "not found"})
end
end
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
end

View file

@ -5,6 +5,7 @@
defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
alias OpenApiSpex.Operation
alias Pleroma.Web.ApiSpec.Schemas.ApiError
alias Pleroma.Web.ApiSpec.Schemas.Chat
alias Pleroma.Web.ApiSpec.Schemas.FlakeID
import Pleroma.Web.ApiSpec.Helpers
@ -52,6 +53,37 @@ defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
}
end
def show_operation do
%Operation{
tags: ["chat"],
summary: "Create a chat",
operationId: "AdminAPI.ChatController.show",
parameters: [
Operation.parameter(
:id,
:path,
:string,
"The id of the chat",
required: true,
example: "1234"
)
],
responses: %{
200 =>
Operation.response(
"The existing chat",
"application/json",
Chat
)
},
security: [
%{
"oAuth" => ["read"]
}
]
}
end
def id_param do
Operation.parameter(:id, :path, FlakeID, "Chat ID",
example: "9umDrYheeY451cQnEe",

View file

@ -216,7 +216,7 @@ defmodule Pleroma.Web.Router do
post("/media_proxy_caches/delete", MediaProxyCacheController, :delete)
post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
# get("/chats/:id", ChatController, :show)
get("/chats/:id", ChatController, :show)
get("/chats/:id/messages", ChatController, :messages)
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
end

View file

@ -104,4 +104,20 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
assert length(result) == 3
end
end
describe "GET /api/pleroma/admin/chats/:id" do
test "it returns a chat", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
{:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
result =
conn
|> get("/api/pleroma/admin/chats/#{chat.id}")
|> json_response_and_validate_schema(200)
assert result["id"] == to_string(chat.id)
end
end
end