mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 09:42:02 +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))
|
||||
c[:onebox]
|
||||
end
|
||||
rescue => e
|
||||
invalidate(url)
|
||||
Rails.logger.warn("invalid cached onebox for #{url} #{e}")
|
||||
""
|
||||
end
|
||||
|
||||
def self.cached_preview(url)
|
||||
if c = Rails.cache.read(onebox_cache_key(url))
|
||||
c[:preview]
|
||||
end
|
||||
rescue => e
|
||||
invalidate(url)
|
||||
Rails.logger.warn("invalid cached preview for #{url} #{e}")
|
||||
""
|
||||
end
|
||||
|
||||
def self.oneboxer_exists_for_url?(url)
|
||||
|
@ -110,21 +118,20 @@ module Oneboxer
|
|||
end
|
||||
|
||||
def self.onebox_raw(url)
|
||||
Rails.cache.fetch(onebox_cache_key(url)){
|
||||
begin
|
||||
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day){
|
||||
# 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?
|
||||
Oneboxer.add_discourse_whitelists
|
||||
|
||||
r = Onebox.preview(url, cache: {})
|
||||
{
|
||||
onebox: r.to_s,
|
||||
preview: r.try(:placeholder_html).to_s
|
||||
}
|
||||
rescue => e
|
||||
Discourse.handle_exception(e, url: url)
|
||||
end
|
||||
r = Onebox.preview(url, cache: {})
|
||||
{
|
||||
onebox: r.to_s,
|
||||
preview: r.try(:placeholder_html).to_s
|
||||
}
|
||||
}
|
||||
rescue => e
|
||||
Discourse.handle_exception(e, url: url)
|
||||
# return a blank hash, so rest of the code works
|
||||
{preview: "", onebox: ""}
|
||||
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