Node.js based forum software built for the modern web
refactor: add `thumbsOnly` option to thumbs retrieval method so that embeds/attachments don't show up in composer thumbnail modal
Julian Lam51fd65abc841
diff --git a/public/openapi/write/topics/tid/thumbs.yaml b/public/openapi/write/topics/tid/thumbs.yamlindex 133da0c20e..3817d2a5a3 100644--- a/public/openapi/write/topics/tid/thumbs.yaml+++ b/public/openapi/write/topics/tid/thumbs.yaml@@ -11,6 +11,13 @@ get:required: truedescription: a valid topic idexample: 1+ - in: query+ name: thumbsOnly+ schema:+ type: boolean+ required: false+ description: "(default: false) exclude post attachments, uploaded media, and those added by plugins"+ example: 0responses:'200':description: Thumbnails successfully retrieveddiff --git a/public/src/modules/topicThumbs.js b/public/src/modules/topicThumbs.jsindex 5495bad541..70c13218d3 100644--- a/public/src/modules/topicThumbs.js+++ b/public/src/modules/topicThumbs.js@@ -5,7 +5,7 @@ define('topicThumbs', [], function (api, bootbox, alerts, uploader, Benchpress, translator) {const Thumbs = {};- Thumbs.get = id => api.get(`/topics/${id}/thumbs`, {});+ Thumbs.get = id => api.get(`/topics/${id}/thumbs`, { thumbsOnly: 1 });Thumbs.getByPid = pid => api.get(`/posts/${encodeURIComponent(pid)}`, {}).then(post => Thumbs.get(post.tid));diff --git a/src/api/topics.js b/src/api/topics.jsindex 72926ee525..10d1c4c10e 100644--- a/src/api/topics.js+++ b/src/api/topics.js@@ -215,7 +215,7 @@ topicsAPI.deleteTags = async (caller, { tid }) => {await topics.deleteTopicTags(tid);};-topicsAPI.getThumbs = async (caller, { tid }) => {+topicsAPI.getThumbs = async (caller, { tid, thumbsOnly }) => {if (isFinite(tid)) { // post_uuids can be passed in occasionally, in that case no checks are necessaryconst [exists, canRead] = await Promise.all([topics.exists(tid),@@ -229,7 +229,7 @@ topicsAPI.getThumbs = async (caller, { tid }) => {}}- return await topics.thumbs.get(tid);+ return await topics.thumbs.get(tid, { thumbsOnly });};// topicsAPI.addThumbdiff --git a/src/controllers/write/topics.js b/src/controllers/write/topics.jsindex 700b08e21a..9c090105f9 100644--- a/src/controllers/write/topics.js+++ b/src/controllers/write/topics.js@@ -127,7 +127,9 @@ Topics.deleteTags = async (req, res) => {};Topics.getThumbs = async (req, res) => {- helpers.formatApiResponse(200, res, await api.topics.getThumbs(req, { ...req.params }));+ let { thumbsOnly } = req.query;+ thumbsOnly = thumbsOnly ? !!parseInt(thumbsOnly, 10) : false;+ helpers.formatApiResponse(200, res, await api.topics.getThumbs(req, { ...req.params, thumbsOnly }));};Topics.addThumb = async (req, res) => {diff --git a/src/topics/thumbs.js b/src/topics/thumbs.jsindex 0e857c1578..bea0b1292b 100644--- a/src/topics/thumbs.js+++ b/src/topics/thumbs.js@@ -41,7 +41,7 @@ Thumbs.load = async function (topicData) {return topicData.map(t => (t && t.tid ? (tidToThumbs[t.tid] || []) : []));};-Thumbs.get = async function (tids) {+Thumbs.get = async function (tids, options) {// Allow singular or plural usagelet singular = false;if (!Array.isArray(tids)) {@@ -49,6 +49,12 @@ Thumbs.get = async function (tids) {singular = true;}+ if (!options) {+ options = {+ thumbsOnly: false,+ };+ }+const isDraft = !await topics.exists(tids);if (!meta.config.allowTopicsThumbnail || !tids.length) {@@ -63,30 +69,32 @@ Thumbs.get = async function (tids) {let mainPids = await topics.getTopicsFields(tids, ['mainPid']);mainPids = mainPids.map(o => o.mainPid);- // Add uploaded media to thumb sets- const mainPidUploads = await Promise.all(mainPids.map(async pid => await posts.uploads.list(pid)));- mainPidUploads.forEach((uploads, idx) => {- uploads = uploads.map(path => `/${path}`);- uploads = uploads.filter(- upload => !thumbs[idx].includes(upload) && mime.getType(upload).startsWith('image/')- );-- if (uploads.length) {- thumbs[idx].push(...uploads);- }- });-- // Add attachments to thumb sets- const mainPidAttachments = await posts.attachments.get(mainPids);- mainPidAttachments.forEach((attachments, idx) => {- attachments = attachments.filter(- attachment => !thumbs[idx].includes(attachment.url) && (attachment.mediaType && attachment.mediaType.startsWith('image/'))- );-- if (attachments.length) {- thumbs[idx].push(...attachments.map(attachment => attachment.url));- }- });+ if (!options.thumbsOnly) {+ // Add uploaded media to thumb sets+ const mainPidUploads = await Promise.all(mainPids.map(async pid => await posts.uploads.list(pid)));+ mainPidUploads.forEach((uploads, idx) => {+ uploads = uploads.map(path => `/${path}`);+ uploads = uploads.filter(+ upload => !thumbs[idx].includes(upload) && mime.getType(upload).startsWith('image/')+ );++ if (uploads.length) {+ thumbs[idx].push(...uploads);+ }+ });++ // Add attachments to thumb sets+ const mainPidAttachments = await posts.attachments.get(mainPids);+ mainPidAttachments.forEach((attachments, idx) => {+ attachments = attachments.filter(+ attachment => !thumbs[idx].includes(attachment.url) && (attachment.mediaType && 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],@@ -98,7 +106,11 @@ Thumbs.get = async function (tids) {url: thumb.startsWith('http') ? thumb : path.posix.join(upload_url, thumb.replace(/\\/g, '/')),})));- ({ thumbs: response } = await plugins.hooks.fire('filter:topics.getThumbs', { tids, thumbs: response }));+ ({ thumbs: response } = await plugins.hooks.fire('filter:topics.getThumbs', {+ tids,+ thumbsOnly: options.thumbsOnly,+ thumbs: response,+ }));return singular ? response.pop() : response;};