refactor: add thumbsOnly option to thumbs retrieval method so that embeds/attachments don't show up in composer thumbnail modal

This commit is contained in:
Julian Lam
2024-12-17 11:14:29 -05:00
parent f4835695de
commit 51fd65abc8
5 changed files with 48 additions and 27 deletions

View File

@@ -11,6 +11,13 @@ get:
required: true
description: a valid topic id
example: 1
- in: query
name: thumbsOnly
schema:
type: boolean
required: false
description: "(default: false) exclude post attachments, uploaded media, and those added by plugins"
example: 0
responses:
'200':
description: Thumbnails successfully retrieved

View File

@@ -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));

View File

@@ -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 necessary
const [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.addThumb

View File

@@ -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) => {

View File

@@ -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 usage
let 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,6 +69,7 @@ Thumbs.get = async function (tids) {
let mainPids = await topics.getTopicsFields(tids, ['mainPid']);
mainPids = mainPids.map(o => o.mainPid);
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) => {
@@ -87,6 +94,7 @@ Thumbs.get = async function (tids) {
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;
};