Skip to content

Node.js based forum software built for the modern web

perf: select uploads on the routes that render posts (#14654)

palmoni5a2d19d34b878
diff --git a/src/posts/summary.js b/src/posts/summary.js
index f949cc251a..67b33dec51 100644
--- a/src/posts/summary.js
+++ b/src/posts/summary.js
@@ -22,7 +22,9 @@ module.exports = function (Posts) {
options.escape = options.hasOwnProperty('escape') ? options.escape : false;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
- const fields = ['pid', 'tid', 'toPid', 'url', 'content', 'sourceContent', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);
+ // `uploads` is selected so parsing can size images without a second lookup,
+ // and is dropped again below to keep the summary payload unchanged
+ const fields = ['pid', 'tid', 'toPid', 'url', 'content', 'sourceContent', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle', 'uploads'].concat(options.extraFields);
let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
@@ -68,6 +70,9 @@ module.exports = function (Posts) {
posts = posts.filter(post => tidToTopic[post.tid]);
posts = await parsePosts(posts, options);
+ if (!options.extraFields.includes('uploads')) {
+ posts.forEach((post) => { post.uploads = undefined; });
+ }
const result = await plugins.hooks.fire('filter:post.getPostSummaryByPids', { posts: posts, uid: uid });
return result.posts;
};
diff --git a/src/topics/posts.js b/src/topics/posts.js
index bde36cf1cc..bbfedfa80c 100644
--- a/src/topics/posts.js
+++ b/src/topics/posts.js
@@ -190,7 +190,7 @@ module.exports = function (Topics) {
const pidToPrivs = _.zipObject(parentPids, postPrivileges);
parentPids = parentPids.filter(p => pidToPrivs[p]['topics:read']);
- const parentPosts = await posts.getPostsFields(parentPids, ['uid', 'pid', 'timestamp', 'content', 'sourceContent', 'deleted']);
+ const parentPosts = await posts.getPostsFields(parentPids, ['uid', 'pid', 'timestamp', 'content', 'sourceContent', 'deleted', 'uploads']);
const parentUids = _.uniq(parentPosts.map(postObj => postObj && postObj.uid));
const userData = await user.getUsersFields(parentUids, ['username', 'userslug', 'picture']);