forked from mtusi2026/mtusi2026lab6
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.
Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.
Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
(OTEL_*) из skeleton.
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { encodeCursor, decodeCursor } from '../common/http/cursor.js';
|
|
import type { Page } from '../common/http/page.js';
|
|
import type { User } from '../users/user.js';
|
|
import type { CreateChatDto } from './dto/create-chat.dto.js';
|
|
import type { Chat } from './chat.js';
|
|
import { ChatsRepository } from './chats.repository.js';
|
|
|
|
@Injectable()
|
|
export class ChatsService {
|
|
constructor(private readonly chatsRepository: ChatsRepository) {}
|
|
|
|
create(input: CreateChatDto, currentUser: User): Promise<Chat> {
|
|
if (!input.member_ids.includes(currentUser.id))
|
|
throw new BadRequestException('Current user must be a chat member');
|
|
if (new Set(input.member_ids).size !== input.member_ids.length)
|
|
throw new BadRequestException('Duplicate chat member');
|
|
return this.chatsRepository.create(input.title, input.member_ids);
|
|
}
|
|
|
|
async getForUser(chatId: string, currentUser: User): Promise<Chat> {
|
|
const chat = await this.chatsRepository.findForUser(
|
|
chatId,
|
|
currentUser,
|
|
);
|
|
if (!chat) throw new NotFoundException();
|
|
return chat;
|
|
}
|
|
|
|
async listForUser(
|
|
currentUser: User,
|
|
limit: number,
|
|
cursorValue?: string,
|
|
): Promise<Page<Chat>> {
|
|
const scope = `chats:${currentUser.id}`;
|
|
const cursor = cursorValue
|
|
? decodeCursor(cursorValue, scope)
|
|
: undefined;
|
|
const page = await this.chatsRepository.findPageForUser(
|
|
currentUser,
|
|
limit,
|
|
cursor,
|
|
);
|
|
const last = page.items.at(-1);
|
|
return {
|
|
items: page.items,
|
|
next_cursor:
|
|
page.hasMore && last
|
|
? encodeCursor({
|
|
scope,
|
|
created_at: last.created_at,
|
|
id: last.id,
|
|
})
|
|
: null,
|
|
};
|
|
}
|
|
}
|