}
end
+ def show_operation do
+ %Operation{
+ tags: ["chat"],
+ summary: "Create a chat",
+ operationId: "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 create_operation do
%Operation{
tags: ["chat"],
id: %Schema{type: :string},
account: %Schema{type: :object},
unread: %Schema{type: :integer},
- last_message: %Schema{type: ChatMessage, nullable: true}
+ last_message: ChatMessage
},
example: %{
"account" => %{
OpenApiSpex.schema(%{
title: "ChatMessage",
description: "Response schema for a ChatMessage",
+ nullable: true,
type: :object,
properties: %{
id: %Schema{type: :string},
plug(
OAuthScopesPlug,
- %{scopes: ["read:statuses"]} when action in [:messages, :index]
+ %{scopes: ["read:statuses"]} when action in [:messages, :index, :show]
)
plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
|> render("show.json", chat: chat)
end
end
+
+ def show(%{assigns: %{user: user}} = conn, params) do
+ with %Chat{} = chat <- Repo.get_by(Chat, user_id: user.id, id: params[:id]) do
+ conn
+ |> put_view(ChatView)
+ |> render("show.json", chat: chat)
+ end
+ end
end
post("/chats/by-account-id/:id", ChatController, :create)
get("/chats", ChatController, :index)
+ get("/chats/:id", ChatController, :show)
get("/chats/:id/messages", ChatController, :messages)
post("/chats/:id/messages", ChatController, :post_chat_message)
post("/chats/:id/read", ChatController, :mark_as_read)
end
end
+ describe "GET /api/v1/pleroma/chats/:id" do
+ setup do: oauth_access(["read:statuses"])
+
+ test "it returns a chat", %{conn: conn, user: user} do
+ other_user = insert(:user)
+
+ {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+
+ result =
+ conn
+ |> get("/api/v1/pleroma/chats/#{chat.id}")
+ |> json_response_and_validate_schema(200)
+
+ assert result["id"] == to_string(chat.id)
+ end
+ end
+
describe "GET /api/v1/pleroma/chats" do
setup do: oauth_access(["read:statuses"])