2025-02-26 01:40:27 -05:00
|
|
|
import { Router } from "express";
|
|
|
|
import { apiStat } from "../lib.js";
|
|
|
|
import { initDb } from "../db.js";
|
|
|
|
import { randomBytes } from 'node:crypto';
|
|
|
|
|
|
|
|
let db = await initDb();
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
const minChar = 1;
|
2025-02-26 22:22:19 -05:00
|
|
|
const maxChar = 2048;
|
2025-02-26 01:40:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
router.post('/', async (req, res, next) => {
|
|
|
|
let { username } = res.auth;
|
2025-03-04 20:26:00 -05:00
|
|
|
let { post, type, id, edit } = req.body;
|
2025-02-26 01:40:27 -05:00
|
|
|
|
2025-02-26 22:12:46 -05:00
|
|
|
if (!username) {
|
|
|
|
apiStat(res, next, 'Log in to chat with the community.')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!post || !id || !type) {
|
2025-02-26 01:40:27 -05:00
|
|
|
apiStat(res, next, 'Fields are missing.')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (post.length < minChar || post.length > maxChar) {
|
|
|
|
apiStat(res, next, `Post length isn't ${minChar} to ${maxChar} characters.`)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-26 20:17:33 -05:00
|
|
|
let token = randomBytes(8).toString('hex');
|
|
|
|
|
|
|
|
if (req.file && req.file.path) {
|
|
|
|
post += `\nfile::${req.file.path}`;
|
|
|
|
}
|
2025-02-26 01:40:27 -05:00
|
|
|
|
2025-03-04 20:26:00 -05:00
|
|
|
if (edit == 'edit') {
|
|
|
|
await db.run('UPDATE comment SET content = ? WHERE username = ? AND id = ?', [
|
|
|
|
post,
|
|
|
|
username,
|
|
|
|
id
|
|
|
|
]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2025-02-26 01:40:27 -05:00
|
|
|
await db.run('INSERT INTO comment (username, date, content, id) VALUES (?,?,?,?)', [
|
|
|
|
username,
|
|
|
|
(+new Date),
|
|
|
|
post,
|
|
|
|
token
|
|
|
|
]);
|
|
|
|
|
|
|
|
await db.run('INSERT INTO feeder (parentType, parentId , childType , childId , sortId ) VALUES(?,?,?,?,?)', [
|
|
|
|
type,
|
|
|
|
id,
|
|
|
|
'comment',
|
|
|
|
token,
|
|
|
|
(+new Date)
|
|
|
|
]);
|
|
|
|
|
2025-03-02 04:09:17 -05:00
|
|
|
if (type == 'users') {
|
|
|
|
await db.run('INSERT INTO messages (username, date, content, link, status) VALUES(?,?,?,?,?)', [
|
|
|
|
id,
|
|
|
|
(+new Date),
|
|
|
|
`A user responded to your wall.`,
|
|
|
|
`/walls/get/users/${id}/0`,
|
|
|
|
'unread'
|
|
|
|
]);
|
|
|
|
} else if (type == 'comment') {
|
|
|
|
let commenter = await db.all('SELECT * FROM comment WHERE id = ?', [
|
|
|
|
id
|
|
|
|
]);
|
|
|
|
if (commenter[0]) {
|
|
|
|
await db.run('INSERT INTO messages (username, date, content, link, status) VALUES(?,?,?,?,?)', [
|
|
|
|
commenter[0].username,
|
|
|
|
(+new Date),
|
|
|
|
`A user responded to your post.`,
|
|
|
|
`/walls/get/comment/${id}/0`,
|
|
|
|
'unread'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-26 01:40:27 -05:00
|
|
|
apiStat(res, next, `Comment submitted.`, '/comment/' + token)
|
|
|
|
})
|
|
|
|
|
|
|
|
export default router;
|