mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
perf: get rid of exists call, load all attachments in one call
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
if (attachments.length) {
|
||||
thumbs[idx].push(...attachments.map(attachment => attachment.url));
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
let response = thumbs.map((thumbSet, idx) => thumbSet.map(thumb => ({
|
||||
id: tids[idx],
|
||||
|
||||
Reference in New Issue
Block a user