76 lines
No EOL
2.1 KiB
JavaScript
76 lines
No EOL
2.1 KiB
JavaScript
import Route from "../route.js";
|
|
import initDb from "../db.js";
|
|
import { randomUUID } from 'node:crypto';
|
|
import auth from "../form/auth.js";
|
|
|
|
let db = await initDb();
|
|
|
|
let main = new Route([auth], async function (req, res, input) {
|
|
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' };
|
|
|
|
if (content.length > 8192)
|
|
return { 'success': false, 'message': 'Comment is too long' };
|
|
|
|
await db.run('INSERT INTO comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)', [
|
|
username,
|
|
targetType,
|
|
targetId,
|
|
+new Date(),
|
|
content,
|
|
id
|
|
]);
|
|
|
|
let u = `/client/${targetType}?id=${targetId}`;
|
|
|
|
let a = content.split(' ');
|
|
|
|
for (let b of a) {
|
|
if (b.startsWith('@')) {
|
|
let f = b.slice(1);
|
|
|
|
await db.run('INSERT INTO message (username, targetType, targetId, date, content, read) VALUES (?,?,?,?,?,?)', [
|
|
f,
|
|
'mention',
|
|
u,
|
|
+new Date(),
|
|
content,
|
|
'false'
|
|
]);
|
|
}
|
|
}
|
|
|
|
if (targetType == 'user') {
|
|
await db.run('INSERT INTO message (username, targetType, targetId, date, content, read) VALUES (?,?,?,?,?,?)', [
|
|
targetId,
|
|
'profile comment',
|
|
u,
|
|
+new Date(),
|
|
content,
|
|
'false'
|
|
]);
|
|
} else if (targetType == 'video') {
|
|
let v = await db.all('SELECT * FROM video WHERE id = ?', [
|
|
targetId
|
|
]);
|
|
|
|
if (v.length == 0) return;
|
|
|
|
await db.run('INSERT INTO message (username, targetType, targetId, date, content, read) VALUES (?,?,?,?,?,?)', [
|
|
v[0].username,
|
|
'video comment',
|
|
u,
|
|
+new Date(),
|
|
content,
|
|
'false'
|
|
]);
|
|
}
|
|
|
|
res.send({ 'message': 'Comment sent', redirect: u });
|
|
});
|
|
|
|
export default main; |