openvibers/openvibe.live Commits

Serve inline Community threads through Live comment proxy (#829)

OP OpenVibers committed 2 days ago · f57d634abd · 3 files, +23 −1

Changes

ddiff --git a/server/media-proxy/comments.js b/server/media-proxy/comments.js
iindex 27e971d8..1e8e4de4 100644
---- a/server/media-proxy/comments.js
++++ b/server/media-proxy/comments.js
@@@ -180,6 +180,23 @@ router.get('/:type/:id', optionalAuth, async (req, res) => {
const thread = await comments.threadFor(contentType, contentId, target.row.title, { ip: req.ip });
const ctx = { type: contentType, id: contentId, user: req.user, subject, target };
++ // Item pages need the Community page as it is: two replies per comment and an opaque
++ // cursor for Top or Newest. The older Live SPA still uses offset and expanded replies.
++ if (req.query.inline === '1') {
++ const sort = req.query.sort === 'new' ? 'new' : 'top';
++ const after = typeof req.query.after === 'string' && req.query.after.length <= 60 ? req.query.after : null;
++ const page = await comments.readThread(thread.id, { subject, sort, after, limit: 30, ip: req.ip });
++ if (page.thread && page.thread.visibility === 'hidden' && !permissions.isStaff(req.user)) return res.status(404).json(HIDDEN);
++ const out = {
++ comments: await Promise.all(page.comments.map((c) => shape(c, ctx))),
++ total: page.thread ? page.thread.comment_count || 0 : 0,
++ next_cursor: page.next_cursor || null,
++ viewer: page.viewer,
++ };
++ if (!access.isPrivate(target.row)) out.thread = { id: thread.access_id, url: comments.threadUrl(thread.access_id) };
++ return res.json(out);
++ }
++
// Community pages by cursor, so an offset is walked in the requested order.
const sort = req.query.sort === 'old' ? 'old' : 'new';
const picked = [];
ddiff --git a/test/comments-community.test.js b/test/comments-community.test.js
iindex e5afe407..dde7eca2 100644
---- a/test/comments-community.test.js
++++ b/test/comments-community.test.js
@@@ -180,6 +180,11 @@ async function check(name, fn) {
assert.deepStrictEqual(page.comments.map((c) => c.message), ['great stream']);
const oldest = (await call('GET', '/api/comments/vod/100?sort=old')).json;
assert.deepStrictEqual(oldest.comments.map((c) => c.message), ['great stream', 'said on Community']);
++ const inline = await call('GET', '/api/comments/vod/100?inline=1&sort=top', 3);
++ assert.strictEqual(inline.status, 200, inline.text);
++ assert.match(last('GET', /^\/threads\/cth_[^/]+$/).query, /sort=top/);
++ assert.strictEqual(inline.json.comments.find((c) => c.id === carolTop.id).replies[0].message, 'thanks!');
++ assert.strictEqual(inline.json.comments[0].can_delete, true, 'owner moderation survives the inline proxy');
assert.ok(!r.text.includes('legacy local row'), 'Live\'s own table is not read');
});
ddiff --git a/test/community-stub.js b/test/community-stub.js
iindex bac8b692..e7e07a32 100644
---- a/test/community-stub.js
++++ b/test/community-stub.js
@@@ -38,7 +38,7 @@ function startCommunityStub({ people = {} } = {}) {
reply_count: replyCount(c), attachments: deleted ? [] : (c.attachments || []), created_at: c.created_at, updated_at: c.edited_at || c.created_at, edited_at: deleted ? null : c.edited_at,
can_edit: !deleted && !!subject && c.author === subject, can_delete: !deleted && !!subject && c.author === subject,
};
-- if (withReplies) out.replies = comments.filter((r) => r.parent_id === c.id && !r.deleted_at).sort((a, b) => a.id - b.id).slice(0, 20).map((r) => shapeComment(r, subject, false));
++ if (withReplies) out.replies = comments.filter((r) => r.parent_id === c.id && !r.deleted_at).sort((a, b) => a.id - b.id).slice(0, 2).map((r) => shapeComment(r, subject, false));
return out;
}