mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46: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 Attachments = module.exports;
|
||||||
const posts = require('./index');
|
const posts = require('./index');
|
||||||
|
|
||||||
Attachments.get = async (pid) => {
|
Attachments.get = async (pids) => {
|
||||||
const hashes = await posts.getPostField(pid, `attachments`);
|
const isArray = Array.isArray(pids);
|
||||||
return Attachments.getAttachments(hashes);
|
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) => {
|
Attachments.getAttachments = async (hashes) => {
|
||||||
|
|||||||
@@ -50,22 +50,21 @@ Thumbs.get = async function (tids) {
|
|||||||
|
|
||||||
const hasTimestampPrefix = /^\d+-/;
|
const hasTimestampPrefix = /^\d+-/;
|
||||||
const upload_url = nconf.get('relative_path') + nconf.get('upload_url');
|
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 sets = tids.map(tid => `${validator.isUUID(String(tid)) ? 'draft' : 'topic'}:${tid}:thumbs`);
|
||||||
const thumbs = await Promise.all(sets.map(getThumbs));
|
const thumbs = await Promise.all(sets.map(getThumbs));
|
||||||
|
|
||||||
// Add attachments to thumb sets
|
// Add attachments to thumb sets
|
||||||
await Promise.all(tids.map(async (tid, idx) => {
|
const mainPids = await topics.getTopicsFields(tids, ['mainPid']);
|
||||||
if (exists[idx]) {
|
const mainPidAttachments = await posts.attachments.get(mainPids);
|
||||||
const mainPid = await topics.getTopicField(tid, 'mainPid');
|
mainPidAttachments.forEach((attachments, idx) => {
|
||||||
let attachments = await posts.attachments.get(mainPid);
|
attachments = attachments.filter(
|
||||||
attachments = attachments.filter(attachment => attachment.mediaType.startsWith('image/'));
|
attachment => attachment.mediaType.startsWith('image/')
|
||||||
|
);
|
||||||
|
|
||||||
if (attachments.length) {
|
if (attachments.length) {
|
||||||
thumbs[idx].push(...attachments.map(attachment => attachment.url));
|
thumbs[idx].push(...attachments.map(attachment => attachment.url));
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}));
|
|
||||||
|
|
||||||
let response = thumbs.map((thumbSet, idx) => thumbSet.map(thumb => ({
|
let response = thumbs.map((thumbSet, idx) => thumbSet.map(thumb => ({
|
||||||
id: tids[idx],
|
id: tids[idx],
|
||||||
|
|||||||
Reference in New Issue
Block a user