chore: migrate minimum from lab1

This commit is contained in:
2026-10-21 09:41:45 +03:00
parent 65003b6119
commit d8fb486709
44 changed files with 11664 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
import { IsString } from 'class-validator';
import { CodePointLength } from '../../common/dto/code-point-length.validator';
export class CreateMessageDto {
@IsString()
@CodePointLength(0, 4000)
text!: string;
}
+24
View File
@@ -0,0 +1,24 @@
import { Message } from '../../common/store/domain';
export interface MessageResponse {
id: string;
chat_id: string;
sender_id: string;
text: string;
created_at: string;
}
export interface MessagePageResponse {
items: MessageResponse[];
next_cursor: string | null;
}
export function toMessageResponse(message: Message): MessageResponse {
return {
id: message.id,
chat_id: message.chatId,
sender_id: message.senderId,
text: message.text,
created_at: message.createdAt,
};
}
+50
View File
@@ -0,0 +1,50 @@
import {
Body,
Controller,
Get,
HttpCode,
Param,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '../common/auth/auth.guard';
import { CurrentUser } from '../common/auth/current-user.decorator';
import { parseLimit, rawCursorParam } from '../common/dto/pagination-query.dto';
import { User } from '../common/store/domain';
import { CreateMessageDto } from './dto/create-message.dto';
import {
MessagePageResponse,
MessageResponse,
toMessageResponse,
} from './dto/message.response';
import { MessagesService } from './messages.service';
@Controller('api/v1/chats/:chatId/messages')
@UseGuards(AuthGuard)
export class MessagesController {
constructor(private readonly messages: MessagesService) {}
@Post()
@HttpCode(201)
create(
@Param('chatId') chatId: string,
@Body() dto: CreateMessageDto,
@CurrentUser() user: User,
): MessageResponse {
const message = this.messages.create(chatId, user.id, dto.text);
return toMessageResponse(message);
}
@Get()
list(
@Param('chatId') chatId: string,
@CurrentUser() user: User,
@Query('limit') limitRaw: unknown,
@Query('cursor') cursorRaw: unknown,
): MessagePageResponse {
const limit = parseLimit(limitRaw);
const cursor = rawCursorParam(cursorRaw);
return this.messages.list(chatId, user.id, limit, cursor);
}
}
+11
View File
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { CommonModule } from '../common/common.module';
import { MessagesController } from './messages.controller';
import { MessagesService } from './messages.service';
@Module({
imports: [CommonModule],
controllers: [MessagesController],
providers: [MessagesService],
})
export class MessagesModule {}
+73
View File
@@ -0,0 +1,73 @@
import { Injectable } from '@nestjs/common';
import { ApiError } from '../common/filters/api-error';
import { codePointLength } from '../common/dto/code-point-length.validator';
import { Message } from '../common/store/domain';
import {
decodeMessageCursor,
encodeMessageCursor,
StoreService,
} from '../common/store/store.service';
import { validate as isUuid } from '../common/uuid';
import { MessagePageResponse, toMessageResponse } from './dto/message.response';
@Injectable()
export class MessagesService {
constructor(private readonly store: StoreService) {}
create(chatId: string, callerId: string, text: string): Message {
this.assertMember(chatId, callerId);
if (codePointLength(text.trim()) === 0) {
throw new ApiError(
'invalid_request',
'text must not be empty or whitespace-only',
);
}
return this.store.createMessage(chatId, callerId, text);
}
list(
chatId: string,
callerId: string,
limit: number,
rawCursor: string | null,
): MessagePageResponse {
this.assertMember(chatId, callerId);
const cursor =
rawCursor === null ? null : decodeMessageCursor(rawCursor);
if (rawCursor !== null && cursor === null) {
throw new ApiError('invalid_request', 'invalid cursor');
}
if (cursor && cursor.chatId !== chatId) {
throw new ApiError(
'invalid_request',
'cursor belongs to a different chat',
);
}
if (cursor && !this.store.messageCursorExists(chatId, cursor.id)) {
throw new ApiError('invalid_request', 'invalid cursor');
}
const items = this.store.listMessages(chatId, cursor, limit);
const last = items[items.length - 1];
const hasMore = last
? this.store.hasMoreMessagesAfter(chatId, last.id)
: false;
return {
items: items.map(toMessageResponse),
next_cursor: hasMore
? encodeMessageCursor({ chatId, id: last.id })
: null,
};
}
private assertMember(chatId: string, callerId: string): void {
if (!isUuid(chatId)) {
throw new ApiError('invalid_request', 'chatId must be a valid UUID');
}
if (
!this.store.getChatById(chatId) ||
!this.store.isMember(chatId, callerId)
) {
throw new ApiError('not_found', 'chat not found');
}
}
}