diff --git a/public/s/js/comments.js b/public/s/js/comments.js index e920882..bb64a47 100644 --- a/public/s/js/comments.js +++ b/public/s/js/comments.js @@ -47,6 +47,14 @@ class CommentSystem { async loadComments(scrollToId = null) { if (!this.container) return; + + // If guest, hide completely and don't fetch + if (!this.user) { + this.container.innerHTML = ''; + this.container.style.display = 'none'; + return; + } + if (!scrollToId) this.container.innerHTML = '
Loading comments...
'; try { @@ -54,6 +62,12 @@ class CommentSystem { const data = await res.json(); if (data.success) { + if (data.require_login) { + this.container.innerHTML = ''; + this.container.style.display = 'none'; // Ensure it takes no space + return; + } + this.isAdmin = data.is_admin || false; this.isLocked = data.is_locked || false; this.render(data.comments, data.user_id, data.is_subscribed); diff --git a/src/inc/routes/comments.mjs b/src/inc/routes/comments.mjs index f9cd5cc..9671397 100644 --- a/src/inc/routes/comments.mjs +++ b/src/inc/routes/comments.mjs @@ -5,14 +5,32 @@ export default (router, tpl) => { - // Fetch comments for an item + // Get comments for an item router.get(/\/api\/comments\/(?\d+)/, async (req, res) => { const itemId = req.params.itemid; const sort = req.url.qs?.sort || 'new'; // 'new' or 'old' + // Require login + if (!req.session) { + return res.reply({ + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + success: true, + comments: [], + require_login: true, + user_id: null, + is_admin: false + }) + }); + } + try { + // Check locked status + const item = await db`SELECT is_comments_locked FROM items WHERE id = ${itemId}`; + const is_locked = item.length > 0 ? item[0].is_comments_locked : false; + const comments = await db` - SELECT + SELECT c.id, c.parent_id, c.content, c.created_at, c.vote_score, c.is_deleted, COALESCE(c.is_pinned, false) as is_pinned, u.user as username, u.id as user_id, uo.avatar, @@ -30,10 +48,6 @@ export default (router, tpl) => { if (sub.length > 0) is_subscribed = true; } - // Check if thread is locked - const itemInfo = await db`SELECT COALESCE(is_comments_locked, false) as is_locked FROM items WHERE id = ${itemId}`; - const is_locked = itemInfo.length > 0 ? itemInfo[0].is_locked : false; - // Transform for frontend if needed, or send as is return res.reply({ headers: { 'Content-Type': 'application/json' },