perf: get rid of exists call, load all attachments in one call

This commit is contained in:
Barış Soner Uşaklı
2024-07-11 22:24:32 -04:00
parent 2aeabce566
commit 9b8c834ef0
2 changed files with 23 additions and 13 deletions

View File

@@ -8,9 +8,20 @@ const db = require('../database');
const Attachments = module.exports;
const posts = require('./index');
Attachments.get = async (pid) => {
const hashes = await posts.getPostField(pid, `attachments`);
return Attachments.getAttachments(hashes);
Attachments.get = async (pids) => {
const isArray = Array.isArray(pids);
if (!isArray) {
pids = [pids];
}
const postData = await posts.getPostsFields(pids, [`attachments`]);
const allHashes = _.flatten(postData.map(p => p && p.attachments));
const allAttachments = await Attachments.getAttachments(allHashes);
const hashToAttachment = _.zipObject(allHashes, allAttachments);
const data = postData.map((post, idx) => {
const pidHashes = post ? post.attachments : [];
return pidHashes.map(hash => hashToAttachment[hash]);
});
return isArray ? data : data[0];
};
Attachments.getAttachments = async (hashes) => {

View File

@@ -50,22 +50,21 @@ Thumbs.get = async function (tids) {
const hasTimestampPrefix = /^\d+-/;
const upload_url = nconf.get('relative_path') + nconf.get('upload_url');
const exists = await topics.exists(tids);
const sets = tids.map(tid => `${validator.isUUID(String(tid)) ? 'draft' : 'topic'}:${tid}:thumbs`);
const thumbs = await Promise.all(sets.map(getThumbs));
// Add attachments to thumb sets
await Promise.all(tids.map(async (tid, idx) => {
if (exists[idx]) {
const mainPid = await topics.getTopicField(tid, 'mainPid');
let attachments = await posts.attachments.get(mainPid);
attachments = attachments.filter(attachment => attachment.mediaType.startsWith('image/'));
const mainPids = await topics.getTopicsFields(tids, ['mainPid']);
const mainPidAttachments = await posts.attachments.get(mainPids);
mainPidAttachments.forEach((attachments, idx) => {
attachments = attachments.filter(
attachment => attachment.mediaType.startsWith('image/')
);
if (attachments.length) {
thumbs[idx].push(...attachments.map(attachment => attachment.url));
}
}
}));
});
let response = thumbs.map((thumbSet, idx) => thumbSet.map(thumb => ({
id: tids[idx],