bigly-chat/form/comment.js

34 lines
1,003 B
JavaScript
Raw Normal View History

2024-11-25 14:12:43 -05:00
import Route from "../route.js";
import initDb from "../db.js";
import { randomUUID } from 'node:crypto';
import auth from "../form/auth.js";
2024-11-25 14:12:44 -05:00
import captcha from "./captcha.js";
2024-11-25 14:12:43 -05:00
let db = await initDb();
// TODO: rewrite
2024-11-25 14:12:44 -05:00
let main = new Route([auth,captcha], async function (req, res, input) {
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
2024-11-25 14:12:43 -05:00
let { username } = input;
let id = randomUUID();
let { targetType, targetId, content } = req.body;
if (!targetType || !targetId || !content || username == '!nobody')
return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings
await db.run('INSERT INTO comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)', [
username,
targetType,
targetId,
+new Date(),
content,
id
]);
res.send({ redir: `?` });
});
export default main;