From 86107535db689edc4944a982e4f20d55409b237a Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 8 Apr 2024 16:08:41 -0400 Subject: [PATCH] fix: default to showing alternate as link to the post object --- src/controllers/topics.js | 8 ++++---- src/topics/index.js | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 1cd83520bd..2ba24d8155 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -137,7 +137,9 @@ topicsController.get = async function getTopic(req, res, next) { if (meta.config.activitypubEnabled) { // Include link header for richer parsing - res.set('Link', `<${nconf.get('url')}/topic/${tid}>; rel="alternate"; type="application/activity+json"`); + const pid = await topics.getPidByIndex(tid, postIndex); + const href = utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid; + res.set('Link', `<${href}>; rel="alternate"; type="application/activity+json"`); } res.render('topic', topicData); @@ -293,9 +295,7 @@ async function addTags(topicData, req, res, currentPage) { } if (meta.config.activitypubEnabled) { - const pid = topicData.postIndex !== 1 ? - (await db.getSortedSetRange(`tid:${topicData.tid}:posts`, topicData.postIndex - 2, topicData.postIndex - 2)).pop() : - topicData.mainPid; + const pid = await topics.getPidByIndex(topicData.tid, topicData.postIndex); res.locals.linkTags.push({ rel: 'alternate', diff --git a/src/topics/index.js b/src/topics/index.js index ae3b345768..77573a2154 100644 --- a/src/topics/index.js +++ b/src/topics/index.js @@ -313,4 +313,11 @@ Topics.search = async function (tid, term) { return Array.isArray(result) ? result : result.ids; }; +Topics.getPidByIndex = async function (tid, index) { + index -= 2; // zset only stores replies, index is not zero-indexed, so offset by 2. + return index > 0 ? + (await db.getSortedSetRange(`tid:${tid}:posts`, index - 2, index - 2)).pop() : + await Topics.getTopicField(tid, 'mainPid'); +}; + require('../promisify')(Topics);