FIX: Load short upload URLs only once (#15918)

Loading did not work when it was used for multiple posts. Only the
short URLs from the first post were loaded.
This commit is contained in:
Bianca Nenciu 2022-02-16 08:57:20 +02:00 committed by GitHub
parent 2d30dd439f
commit add4b74e08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 15 deletions

View File

@ -1,5 +1,6 @@
import I18n from "I18n";
import discourseDebounce from "discourse-common/lib/debounce";
import I18n from "I18n";
import { Promise } from "rsvp";
let _cache = {};
@ -160,6 +161,15 @@ function _loadCachedShortUrls(uploadElements, siteSettings, opts) {
});
}
let queueUrls;
let queuePromise;
let queueResolve;
function queuePop(ajax) {
lookupUncachedUploadUrls(queueUrls, ajax).then(queueResolve);
queueUrls = queueResolve = null;
}
function _loadShortUrls(uploads, ajax, siteSettings, opts) {
let urls = [...uploads].map((upload) => {
return (
@ -168,9 +178,20 @@ function _loadShortUrls(uploads, ajax, siteSettings, opts) {
);
});
return lookupUncachedUploadUrls(urls, ajax).then(() =>
_loadCachedShortUrls(uploads, siteSettings, opts)
);
if (!queueUrls) {
queueUrls = [...urls];
queuePromise = new Promise((resolve) => {
queueResolve = resolve;
});
discourseDebounce(null, queuePop, ajax, 450);
} else {
queueUrls.push(...urls);
}
return queuePromise.then(() => {
_loadCachedShortUrls(uploads, siteSettings, opts);
});
}
const SHORT_URL_ATTRIBUTES =
@ -192,17 +213,7 @@ export function resolveAllShortUrls(ajax, siteSettings, scope, opts) {
shortUploadElements = scope.querySelectorAll(SHORT_URL_ATTRIBUTES);
if (shortUploadElements.length > 0) {
// this is carefully batched so we can do a leading debounce (trigger right away)
return discourseDebounce(
null,
_loadShortUrls,
shortUploadElements,
ajax,
siteSettings,
opts,
450,
true
);
return _loadShortUrls(shortUploadElements, ajax, siteSettings, opts);
}
}
}