115 lines
2.8 KiB
TypeScript
115 lines
2.8 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Param,
|
|
Post,
|
|
Query,
|
|
Req,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import {
|
|
ApiBody,
|
|
ApiBearerAuth,
|
|
ApiCreatedResponse,
|
|
ApiOkResponse,
|
|
ApiOperation,
|
|
ApiQuery,
|
|
ApiTags,
|
|
} from '@nestjs/swagger';
|
|
import { UserGuard, type AuthenticatedRequest } from '../User/user.guard';
|
|
import {
|
|
CreateChatDto,
|
|
CreateMessageDto,
|
|
Chat,
|
|
Message,
|
|
ChatPage,
|
|
MessagePage,
|
|
} from './chat.schema';
|
|
import { ChatService } from './chat.service';
|
|
|
|
@ApiTags('chats')
|
|
@ApiBearerAuth('bearerAuth')
|
|
@UseGuards(UserGuard)
|
|
@Controller('api/v1/chats')
|
|
export class ChatController {
|
|
constructor(private readonly chats: ChatService) {}
|
|
|
|
@Post()
|
|
@HttpCode(HttpStatus.CREATED)
|
|
@ApiBody({ type: CreateChatDto })
|
|
@ApiOperation({ operationId: 'createChat' })
|
|
@ApiCreatedResponse({ type: Chat })
|
|
create(@Req() request: AuthenticatedRequest, @Body() body: unknown) {
|
|
return this.chats.create(body, request.currentUser.id);
|
|
}
|
|
|
|
@Get()
|
|
@ApiOperation({ operationId: 'listChats' })
|
|
@ApiOkResponse({ type: ChatPage })
|
|
@ApiQuery({
|
|
name: 'limit',
|
|
required: false,
|
|
schema: { type: 'integer', minimum: 1, maximum: 100, default: 50 },
|
|
})
|
|
@ApiQuery({ name: 'cursor', required: false, type: String })
|
|
list(
|
|
@Req() request: AuthenticatedRequest,
|
|
@Query('limit') limit: unknown,
|
|
@Query('cursor') cursor: unknown,
|
|
) {
|
|
return this.chats.list(
|
|
request.currentUser.id,
|
|
limit,
|
|
cursor,
|
|
request.currentChatsEpoch,
|
|
);
|
|
}
|
|
|
|
@Get(':chat_id')
|
|
@ApiOperation({ operationId: 'getChat' })
|
|
@ApiOkResponse({ type: Chat })
|
|
get(@Req() request: AuthenticatedRequest, @Param('chat_id') chatId: string) {
|
|
return this.chats.get(chatId, request.currentUser.id);
|
|
}
|
|
|
|
@Post(':chat_id/messages')
|
|
@HttpCode(HttpStatus.CREATED)
|
|
@ApiBody({ type: CreateMessageDto })
|
|
@ApiOperation({ operationId: 'createMessage' })
|
|
@ApiCreatedResponse({ type: Message })
|
|
createMessage(
|
|
@Req() request: AuthenticatedRequest,
|
|
@Param('chat_id') chatId: string,
|
|
@Body() body: unknown,
|
|
) {
|
|
return this.chats.createMessage(chatId, body, request.currentUser.id);
|
|
}
|
|
|
|
@Get(':chat_id/messages')
|
|
@ApiOperation({ operationId: 'listMessages' })
|
|
@ApiOkResponse({ type: MessagePage })
|
|
@ApiQuery({
|
|
name: 'limit',
|
|
required: false,
|
|
schema: { type: 'integer', minimum: 1, maximum: 100, default: 50 },
|
|
})
|
|
@ApiQuery({ name: 'cursor', required: false, type: String })
|
|
listMessages(
|
|
@Req() request: AuthenticatedRequest,
|
|
@Param('chat_id') chatId: string,
|
|
@Query('limit') limit: unknown,
|
|
@Query('cursor') cursor: unknown,
|
|
) {
|
|
return this.chats.listMessages(
|
|
chatId,
|
|
request.currentUser.id,
|
|
limit,
|
|
cursor,
|
|
request.currentChatAccess,
|
|
);
|
|
}
|
|
}
|