perf: get rid of getPidByIndex, use postAtIndex

This commit is contained in:
Barış Soner Uşaklı
2024-06-15 13:54:37 -04:00
parent 820d576481
commit ad22b54e5e
2 changed files with 9 additions and 16 deletions

View File

@@ -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',

View File

@@ -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);