// Сообщения чата: отправка и история с keyset-пагинацией. Отправитель берётся из сессии, не из тела. import { requireUser } from '../auth/sessions.js'; import { notFound } from '../http/errors.js'; import type { Handler } from '../http/router.js'; import { afterCursor, jsonBody, page, pageLimit, parseUuid, textField } from '../http/validation.js'; import { isMember } from '../repositories/chats.js'; import { createMessage, listMessages } from '../repositories/messages.js'; export const send: Handler = async (ctx) => { const userId = await requireUser(ctx); const chatId = parseUuid(ctx.params[0]); const text = textField(jsonBody(ctx, ['text']).text, 'text', 1, 4000); const message = await createMessage(chatId, userId, text); if (!message) throw notFound(); // чата нет или пользователь в нём не состоит return [201, message]; }; export const history: Handler = async (ctx) => { const userId = await requireUser(ctx); const chatId = parseUuid(ctx.params[0]); const limit = pageLimit(ctx); const scope = 'c:' + chatId; const after = afterCursor(ctx, scope); if (!(await isMember(chatId, userId))) throw notFound(); return [200, page(await listMessages(chatId, after, limit + 1), limit, scope)]; };