Skip to content

Node.js based forum software built for the modern web

perf: get rid of getPidByIndex, use postAtIndex

Barış Soner Uşaklıad22b54e5e93
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index ea17578f33..d1c33040cc 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -118,12 +118,15 @@ topicsController.get = async function getTopic(req, res, next) {
}
topicData.postIndex = postIndex;
+ const postAtIndex = topicData.posts.find(
+ p => parseInt(p.index, 10) === parseInt(Math.max(0, postIndex - 1), 10)
+ );
const [author] = await Promise.all([
user.getUserFields(topicData.uid, ['username', 'userslug']),
buildBreadcrumbs(topicData),
addOldCategory(topicData, userPrivileges),
- addTags(topicData, req, res, currentPage),
+ addTags(topicData, req, res, currentPage, postAtIndex),
topics.increaseViewCount(req, tid),
markAsRead(req, tid),
analytics.increment([`pageviews:byCid:${topicData.category.cid}`]),
@@ -136,9 +139,9 @@ topicsController.get = async function getTopic(req, res, next) {
res.locals.linkTags.push(rel);
});
- if (meta.config.activitypubEnabled) {
+ if (meta.config.activitypubEnabled && postAtIndex) {
// Include link header for richer parsing
- const pid = await topics.getPidByIndex(tid, postIndex);
+ const { pid } = postAtIndex;
const href = utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid;
res.set('Link', `<${href}>; rel="alternate"; type="application/activity+json"`);
}
@@ -204,9 +207,7 @@ async function addOldCategory(topicData, userPrivileges) {
}
}
-async function addTags(topicData, req, res, currentPage) {
- const postIndex = parseInt(req.params.post_index, 10) || 0;
- const postAtIndex = topicData.posts.find(p => parseInt(p.index, 10) === parseInt(Math.max(0, postIndex - 1), 10));
+async function addTags(topicData, req, res, currentPage, postAtIndex) {
let description = '';
if (postAtIndex && postAtIndex.content) {
description = utils.stripHTMLTags(utils.decodeHTMLEntities(postAtIndex.content)).trim();
@@ -295,9 +296,8 @@ async function addTags(topicData, req, res, currentPage) {
});
}
- if (meta.config.activitypubEnabled) {
- const pid = await topics.getPidByIndex(topicData.tid, topicData.postIndex);
-
+ if (meta.config.activitypubEnabled && postAtIndex) {
+ const { pid } = postAtIndex;
res.locals.linkTags.push({
rel: 'alternate',
type: 'application/activity+json',
diff --git a/src/topics/index.js b/src/topics/index.js
index 5d015c6c90..46483d441a 100644
--- a/src/topics/index.js
+++ b/src/topics/index.js
@@ -314,11 +314,4 @@ 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, index)).pop() :
- await Topics.getTopicField(tid, 'mainPid');
-};
-
require('../promisify')(Topics);