FIX: inline oneboxer min title length of 2

also: cache mini onebox misses as well to cut down traffic
This commit is contained in:
Sam 2018-01-30 08:39:41 +11:00
parent d37477b1ef
commit f946db4afe
5 changed files with 33 additions and 5 deletions

View File

@ -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"]);
}
}

View File

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

View File

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

View File

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

View File

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