forked from mtusi2026/mtusi2026lab2
ЛР 2 — БПИ2303 — Морару — на 5 (трек 2)
TypeScript/Node API на PostgreSQL с сессиями в Valkey (AOF), миграции, compose-стенд, restart hook, тесты уровня 4 и эксперимент трека 2 (планы запроса на 240k сообщений, стоимость записи, backup/restore).
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// Сообщения чата: отправка и история с 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)];
|
||||
};
|
||||
Reference in New Issue
Block a user