2019-06-10 21:13:39 -04:00
|
|
|
const _cache = {};
|
2018-11-27 03:00:31 -05:00
|
|
|
|
2020-02-12 05:11:28 -05:00
|
|
|
export function applyInlineOneboxes(inline, ajax, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
2021-05-06 12:08:04 -04:00
|
|
|
const urls = Object.keys(inline).filter((url) => !_cache[url]);
|
|
|
|
|
|
|
|
urls.forEach((url) => {
|
2018-01-29 16:39:41 -05:00
|
|
|
// cache a blank locally, so we never trigger a lookup
|
|
|
|
_cache[url] = {};
|
|
|
|
});
|
|
|
|
|
2021-05-06 12:08:04 -04:00
|
|
|
if (urls.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ajax("/inline-onebox", {
|
2020-02-12 05:11:28 -05:00
|
|
|
data: {
|
2021-05-06 12:08:04 -04:00
|
|
|
urls,
|
2020-02-12 05:11:28 -05:00
|
|
|
category_id: opts.categoryId,
|
|
|
|
topic_id: opts.topicId,
|
|
|
|
},
|
2017-07-19 15:08:54 -04:00
|
|
|
}).then((result) => {
|
|
|
|
result["inline-oneboxes"].forEach((onebox) => {
|
2018-11-12 15:14:20 -05:00
|
|
|
if (onebox.title) {
|
|
|
|
_cache[onebox.url] = onebox;
|
2021-10-22 07:15:46 -04:00
|
|
|
|
2018-11-12 15:14:20 -05:00
|
|
|
let links = inline[onebox.url] || [];
|
|
|
|
links.forEach((link) => {
|
2021-10-22 07:15:46 -04:00
|
|
|
link.innerText = onebox.title;
|
|
|
|
link.classList.add("inline-onebox");
|
|
|
|
link.classList.remove("inline-onebox-loading");
|
2018-11-12 15:14:20 -05:00
|
|
|
});
|
|
|
|
}
|
2017-07-19 15:08:54 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cachedInlineOnebox(url) {
|
|
|
|
return _cache[url];
|
|
|
|
}
|
2019-06-10 21:13:39 -04:00
|
|
|
|
|
|
|
export function applyCachedInlineOnebox(url, onebox) {
|
|
|
|
return (_cache[url] = onebox);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteCachedInlineOnebox(url) {
|
|
|
|
return delete _cache[url];
|
|
|
|
}
|