adding comment overview page on userprofiles

This commit is contained in:
2026-01-26 22:54:59 +01:00
parent 002dfeded3
commit dfc8ebba89
4 changed files with 219 additions and 9 deletions

View File

@@ -58,6 +58,87 @@ export default (router, tpl) => {
}
});
// Browse User Comments
router.get(/\/user\/(?<user>[^\/]+)\/comments/, async (req, res) => {
const user = decodeURIComponent(req.params.user);
try {
// Check if user exists and get ID + avatar
const u = await db`
SELECT "user".id, "user".user, user_options.avatar
FROM "user"
LEFT JOIN user_options ON "user".id = user_options.user_id
WHERE "user".user ILIKE ${user}
`;
if (!u.length) {
return res.reply({ code: 404, body: "User not found" });
}
const userId = u[0].id;
const sort = req.url.qs?.sort || 'new';
const page = +(req.url.qs?.page || 1);
const limit = 20;
const offset = (page - 1) * limit;
const isJson = req.url.qs?.json === 'true';
const comments = await db`
SELECT c.*, i.mime, i.id as item_id
FROM comments c
LEFT JOIN items i ON c.item_id = i.id
WHERE c.user_id = ${userId} AND c.is_deleted = false
ORDER BY c.created_at DESC
LIMIT ${limit} OFFSET ${offset}
`;
const emojis = await db`SELECT name, url FROM custom_emojis`;
const emojiMap = new Map();
emojis.forEach(e => emojiMap.set(e.name, e.url));
const escapeHtml = (unsafe) => {
return (unsafe || '')
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
};
const processedComments = comments.map(c => {
let safeContent = escapeHtml(c.content);
// Replace :emoji: with img
safeContent = safeContent.replace(/:([a-z0-9_]+):/g, (match, name) => {
if (emojiMap.has(name)) {
return `<img src="${emojiMap.get(name)}" style="height:20px;vertical-align:middle;" alt="${name}">`;
}
return match;
});
return {
...c,
content: safeContent
};
});
if (isJson) {
return res.reply({
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ success: true, comments: processedComments })
});
}
const data = {
user: u[0],
comments: processedComments,
hidePagination: true,
tmp: null // for header/footer
};
return res.reply({ body: tpl.render('comments_user', data, req) });
} catch (e) {
console.error(e);
return res.reply({ code: 500, body: "Error" });
}
});
// Post a comment
router.post('/api/comments', async (req, res) => {
if (!req.session) return res.reply({ code: 401, body: JSON.stringify({ success: false, message: "Unauthorized" }) });

View File

@@ -69,6 +69,13 @@ export default (router, tpl) => {
count.favs = 0;
}
try {
const comms = await db`select count(*) from comments where user_id = ${query[0].user_id}`;
count.comments = +comms[0].count;
} catch (e) {
count.comments = 0;
}
const data = {
user: query[0],
f0cks,