mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 15:25:35 +08:00
BUGFIX: more robust onebox implementation
This commit is contained in:
parent
d9f51961c4
commit
0bc3525b10
|
@ -35,12 +35,20 @@ module Oneboxer
|
||||||
if c = Rails.cache.read(onebox_cache_key(url))
|
if c = Rails.cache.read(onebox_cache_key(url))
|
||||||
c[:onebox]
|
c[:onebox]
|
||||||
end
|
end
|
||||||
|
rescue => e
|
||||||
|
invalidate(url)
|
||||||
|
Rails.logger.warn("invalid cached onebox for #{url} #{e}")
|
||||||
|
""
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.cached_preview(url)
|
def self.cached_preview(url)
|
||||||
if c = Rails.cache.read(onebox_cache_key(url))
|
if c = Rails.cache.read(onebox_cache_key(url))
|
||||||
c[:preview]
|
c[:preview]
|
||||||
end
|
end
|
||||||
|
rescue => e
|
||||||
|
invalidate(url)
|
||||||
|
Rails.logger.warn("invalid cached preview for #{url} #{e}")
|
||||||
|
""
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.oneboxer_exists_for_url?(url)
|
def self.oneboxer_exists_for_url?(url)
|
||||||
|
@ -110,21 +118,20 @@ module Oneboxer
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.onebox_raw(url)
|
def self.onebox_raw(url)
|
||||||
Rails.cache.fetch(onebox_cache_key(url)){
|
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day){
|
||||||
begin
|
# This might be able to move to whenever the SiteSetting changes?
|
||||||
|
Oneboxer.add_discourse_whitelists
|
||||||
|
|
||||||
# This might be able to move to whenever the SiteSetting changes?
|
r = Onebox.preview(url, cache: {})
|
||||||
Oneboxer.add_discourse_whitelists
|
{
|
||||||
|
onebox: r.to_s,
|
||||||
r = Onebox.preview(url, cache: {})
|
preview: r.try(:placeholder_html).to_s
|
||||||
{
|
}
|
||||||
onebox: r.to_s,
|
|
||||||
preview: r.try(:placeholder_html).to_s
|
|
||||||
}
|
|
||||||
rescue => e
|
|
||||||
Discourse.handle_exception(e, url: url)
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
|
rescue => e
|
||||||
|
Discourse.handle_exception(e, url: url)
|
||||||
|
# return a blank hash, so rest of the code works
|
||||||
|
{preview: "", onebox: ""}
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
10
spec/components/oneboxer_spec.rb
Normal file
10
spec/components/oneboxer_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
require_dependency 'oneboxer'
|
||||||
|
|
||||||
|
describe Oneboxer do
|
||||||
|
it "returns blank string for an invalid onebox" do
|
||||||
|
Oneboxer.preview("http://boom.com").should == ""
|
||||||
|
Oneboxer.onebox("http://boom.com").should == ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user