forked from mtusi2026/mtusi2026lab2
feat: lab2 done
This commit is contained in:
@@ -1,75 +1,75 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { decodeCursor, encodeCursor } from '../common/http/cursor.js';
|
||||
import type { User } from '../users/user.js';
|
||||
import { ChatsService } from '../chats/chats.service.js';
|
||||
import type { CreateMessageDto } from './dto/create-message.dto.js';
|
||||
import type { Message } from './message.js';
|
||||
import { MessagesRepository } from './messages.repository.js';
|
||||
import { MessagesCacheService } from './cache/messages-cache.service.js';
|
||||
|
||||
@Injectable()
|
||||
export class MessagesService {
|
||||
constructor(
|
||||
private readonly messagesRepository: MessagesRepository,
|
||||
private readonly chatsService: ChatsService,
|
||||
) {}
|
||||
constructor(
|
||||
private readonly messagesRepository: MessagesRepository,
|
||||
private readonly chatsService: ChatsService,
|
||||
private readonly cache: MessagesCacheService,
|
||||
) {}
|
||||
|
||||
create(
|
||||
chatId: string,
|
||||
input: CreateMessageDto,
|
||||
currentUser: User,
|
||||
): Message {
|
||||
const chat = this.chatsService.getForUser(chatId, currentUser);
|
||||
async create(
|
||||
chatId: string,
|
||||
input: CreateMessageDto,
|
||||
currentUser: User,
|
||||
): Promise<Message> {
|
||||
await this.chatsService.getForUser(chatId, currentUser);
|
||||
if ([...input.text].every((character) => /\s/u.test(character)))
|
||||
throw new BadRequestException('Message text cannot be blank');
|
||||
return this.messagesRepository.create(
|
||||
chatId,
|
||||
currentUser.id,
|
||||
input.text,
|
||||
);
|
||||
}
|
||||
|
||||
if ([...input.text].every((character) => /\s/u.test(character))) {
|
||||
throw new BadRequestException('Message text cannot be blank');
|
||||
}
|
||||
|
||||
const message: Message = {
|
||||
id: randomUUID(),
|
||||
chat_id: chat.id,
|
||||
sender_id: currentUser.id,
|
||||
text: input.text,
|
||||
created_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
this.messagesRepository.save(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
list(
|
||||
chatId: string,
|
||||
currentUser: User,
|
||||
limit: number,
|
||||
cursorValue?: string,
|
||||
): { items: Message[]; next_cursor: string | null } {
|
||||
this.chatsService.getForUser(chatId, currentUser);
|
||||
|
||||
const scope = `messages:${chatId}`;
|
||||
const cursor = cursorValue
|
||||
? decodeCursor(cursorValue, scope)
|
||||
: undefined;
|
||||
const messages = this.messagesRepository.findByChatId(chatId);
|
||||
const start = cursor
|
||||
? this.messagesRepository.findFirstAfter(chatId, cursor)
|
||||
: 0;
|
||||
const items = messages.slice(start, start + limit);
|
||||
const hasMore = messages.length > start + limit;
|
||||
const last = items.at(-1);
|
||||
|
||||
return {
|
||||
items,
|
||||
next_cursor:
|
||||
hasMore && last
|
||||
? encodeCursor({
|
||||
scope,
|
||||
created_at: last.created_at,
|
||||
id: last.id,
|
||||
})
|
||||
: null,
|
||||
};
|
||||
}
|
||||
async list(
|
||||
chatId: string,
|
||||
currentUser: User,
|
||||
limit: number,
|
||||
cursorValue?: string,
|
||||
): Promise<{ items: Message[]; next_cursor: string | null }> {
|
||||
await this.chatsService.getForUser(chatId, currentUser);
|
||||
const scope = `messages:${chatId}`;
|
||||
const cursor = cursorValue
|
||||
? decodeCursor(cursorValue, scope)
|
||||
: undefined;
|
||||
const version = await this.messagesRepository.getVersion(
|
||||
chatId,
|
||||
currentUser.id,
|
||||
);
|
||||
const page = await this.cache.getOrLoad(
|
||||
chatId,
|
||||
version,
|
||||
cursorValue,
|
||||
limit,
|
||||
async () => {
|
||||
const loaded = await this.messagesRepository.findPage(
|
||||
chatId,
|
||||
limit,
|
||||
cursor,
|
||||
);
|
||||
const lastLoaded = loaded.items.at(-1);
|
||||
return {
|
||||
items: loaded.items,
|
||||
next_cursor:
|
||||
loaded.hasMore && lastLoaded
|
||||
? encodeCursor({
|
||||
scope,
|
||||
created_at: lastLoaded.created_at,
|
||||
id: lastLoaded.id,
|
||||
})
|
||||
: null,
|
||||
};
|
||||
},
|
||||
);
|
||||
return page as { items: Message[]; next_cursor: string | null };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user