bigly-chat/client/comment.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-11-25 14:12:43 -05:00
import Route from "../route.js";
import auth from "../form/auth.js";
import initDb from "../db.js";
let db = await initDb();
let main = new Route([auth], async function (req, res, input) {
2024-11-25 14:12:44 -05:00
let { route } = req.params;
let { id } = req.query;
2024-11-25 14:12:43 -05:00
2024-11-25 14:12:44 -05:00
let comments = await db.all('SELECT * FROM comment WHERE targetType = ? AND targetId = ? ORDER BY date DESC', [
2024-11-25 14:12:43 -05:00
route,
id
]);
2024-11-25 14:12:44 -05:00
for (let comment of comments) {
let { content } = comment;
content = content.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
content = content.split(' ');
for (let elem in content) {
let e = content[elem];
if (e.startsWith('@')) {
let f = e.slice(1);
content[elem] = `<a class='link' href='/client/user?id=${f}'>${e}</a>`
}
}
content = content.join(' ');
comment.content = content;
}
2024-11-25 14:12:43 -05:00
return {
...input,
route,
id,
comments
};
});
export default main;