mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 10:57:04 +08:00
FIX: inline oneboxer min title length of 2
also: cache mini onebox misses as well to cut down traffic
This commit is contained in:
parent
d37477b1ef
commit
f946db4afe
|
@ -96,15 +96,15 @@ function applyOnebox(state, silent) {
|
|||
|
||||
if (!isTopLevel(href)) {
|
||||
let onebox = cachedInlineOnebox(href);
|
||||
|
||||
let options = state.md.options.discourse;
|
||||
|
||||
if (options.lookupInlineOnebox) {
|
||||
onebox = options.lookupInlineOnebox(href);
|
||||
}
|
||||
|
||||
if (onebox) {
|
||||
if (onebox && onebox.title) {
|
||||
text.content = onebox.title;
|
||||
} else if (state.md.options.discourse.previewing) {
|
||||
} else if (state.md.options.discourse.previewing && !onebox) {
|
||||
attrs.push(["class", "inline-onebox-loading"]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
let _cache = {};
|
||||
|
||||
export function applyInlineOneboxes(inline, ajax) {
|
||||
|
||||
Object.keys(inline).forEach(url => {
|
||||
// cache a blank locally, so we never trigger a lookup
|
||||
_cache[url] = {};
|
||||
});
|
||||
|
||||
return ajax("/inline-onebox", {
|
||||
data: { urls: Object.keys(inline) },
|
||||
}).then(result => {
|
||||
|
|
|
@ -2,6 +2,8 @@ require_dependency 'retrieve_title'
|
|||
|
||||
class InlineOneboxer
|
||||
|
||||
MIN_TITLE_LENGTH = 2
|
||||
|
||||
def initialize(urls, opts = nil)
|
||||
@urls = urls
|
||||
@opts = opts || {}
|
||||
|
@ -46,6 +48,7 @@ class InlineOneboxer
|
|||
uri.hostname.present? &&
|
||||
(always_allow || domains.include?(uri.hostname)) &&
|
||||
title = RetrieveTitle.crawl(url)
|
||||
title = nil if title && title.length < MIN_TITLE_LENGTH
|
||||
return onebox_for(url, title, opts)
|
||||
end
|
||||
end
|
||||
|
@ -58,7 +61,7 @@ class InlineOneboxer
|
|||
def self.onebox_for(url, title, opts)
|
||||
onebox = {
|
||||
url: url,
|
||||
title: Emoji.gsub_emoji_to_unicode(title)
|
||||
title: title && Emoji.gsub_emoji_to_unicode(title)
|
||||
}
|
||||
unless opts[:skip_cache]
|
||||
Rails.cache.write(cache_key(url), onebox, expires_in: 1.day)
|
||||
|
|
|
@ -67,6 +67,6 @@ module RetrieveTitle
|
|||
title = extract_title(current)
|
||||
throw :done if title || max_size < current.length
|
||||
end
|
||||
return title || ""
|
||||
return title
|
||||
end
|
||||
end
|
||||
|
|
|
@ -87,6 +87,25 @@ describe InlineOneboxer do
|
|||
expect(onebox[:title]).to eq("a blog")
|
||||
end
|
||||
|
||||
it "will not return a onebox if it does not meet minimal length" do
|
||||
SiteSetting.enable_inline_onebox_on_all_domains = true
|
||||
|
||||
# Final destination does a HEAD and a GET
|
||||
stub_request(:head, "https://eviltrout.com/some-path").to_return(status: 200)
|
||||
|
||||
stub_request(:get, "https://eviltrout.com/some-path").
|
||||
to_return(status: 200, body: "<html><head><title>a</title></head></html>", headers: {})
|
||||
|
||||
onebox = InlineOneboxer.lookup(
|
||||
"https://eviltrout.com/some-path",
|
||||
skip_cache: true
|
||||
)
|
||||
|
||||
expect(onebox).to be_present
|
||||
expect(onebox[:url]).to eq("https://eviltrout.com/some-path")
|
||||
expect(onebox[:title]).to eq(nil)
|
||||
end
|
||||
|
||||
it "will lookup whitelisted domains" do
|
||||
SiteSetting.inline_onebox_domains_whitelist = "eviltrout.com"
|
||||
RetrieveTitle.stubs(:crawl).returns("Evil Trout's Blog")
|
||||
|
|
Loading…
Reference in New Issue
Block a user