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