mirror of
https://github.com/discourse/discourse.git
synced 2025-02-08 12:24:55 +00: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,9 +118,7 @@ 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?
|
# This might be able to move to whenever the SiteSetting changes?
|
||||||
Oneboxer.add_discourse_whitelists
|
Oneboxer.add_discourse_whitelists
|
||||||
|
|
||||||
@ -121,10 +127,11 @@ module Oneboxer
|
|||||||
onebox: r.to_s,
|
onebox: r.to_s,
|
||||||
preview: r.try(:placeholder_html).to_s
|
preview: r.try(:placeholder_html).to_s
|
||||||
}
|
}
|
||||||
|
}
|
||||||
rescue => e
|
rescue => e
|
||||||
Discourse.handle_exception(e, url: url)
|
Discourse.handle_exception(e, url: url)
|
||||||
end
|
# 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…
x
Reference in New Issue
Block a user