2017-05-22 16:42:19 -04:00
|
|
|
require_dependency "onebox/discourse_onebox_sanitize_config"
|
|
|
|
require_dependency 'final_destination'
|
|
|
|
|
2016-10-24 19:25:44 -04:00
|
|
|
Dir["#{Rails.root}/lib/onebox/engine/*_onebox.rb"].sort.each { |f| require f }
|
2014-02-25 13:35:08 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
module Oneboxer
|
2013-04-29 22:43:21 -04:00
|
|
|
# keep reloaders happy
|
|
|
|
unless defined? Oneboxer::Result
|
|
|
|
Result = Struct.new(:doc, :changed) do
|
|
|
|
def to_html
|
|
|
|
doc.to_html
|
|
|
|
end
|
2013-04-10 03:52:38 -04:00
|
|
|
|
2013-04-29 22:43:21 -04:00
|
|
|
def changed?
|
|
|
|
changed
|
|
|
|
end
|
2013-04-10 03:52:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-26 15:38:23 -04:00
|
|
|
def self.ignore_redirects
|
2017-07-27 21:20:09 -04:00
|
|
|
@ignore_redirects ||= ['http://www.dropbox.com', 'http://store.steampowered.com', Discourse.base_url]
|
2017-06-26 15:38:23 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.preview(url, options = nil)
|
2014-01-27 15:09:09 -05:00
|
|
|
options ||= {}
|
2016-12-19 18:31:10 -05:00
|
|
|
invalidate(url) if options[:invalidate_oneboxes]
|
2014-04-01 00:29:14 -04:00
|
|
|
onebox_raw(url)[:preview]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.onebox(url, options = nil)
|
2014-01-27 15:09:09 -05:00
|
|
|
options ||= {}
|
2016-12-19 18:31:10 -05:00
|
|
|
invalidate(url) if options[:invalidate_oneboxes]
|
2014-04-01 00:29:14 -04:00
|
|
|
onebox_raw(url)[:onebox]
|
2014-03-17 22:12:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.cached_onebox(url)
|
2014-04-01 00:29:14 -04:00
|
|
|
if c = Rails.cache.read(onebox_cache_key(url))
|
|
|
|
c[:onebox]
|
|
|
|
end
|
2014-05-28 03:15:10 -04:00
|
|
|
rescue => e
|
|
|
|
invalidate(url)
|
|
|
|
Rails.logger.warn("invalid cached onebox for #{url} #{e}")
|
|
|
|
""
|
2014-03-17 22:12:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.cached_preview(url)
|
2014-04-01 00:29:14 -04:00
|
|
|
if c = Rails.cache.read(onebox_cache_key(url))
|
|
|
|
c[:preview]
|
|
|
|
end
|
2014-05-28 03:15:10 -04:00
|
|
|
rescue => e
|
|
|
|
invalidate(url)
|
|
|
|
Rails.logger.warn("invalid cached preview for #{url} #{e}")
|
|
|
|
""
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
|
2014-01-28 13:18:19 -05:00
|
|
|
def self.invalidate(url)
|
2014-03-17 22:12:58 -04:00
|
|
|
Rails.cache.delete(onebox_cache_key(url))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-01-28 13:18:19 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Parse URLs out of HTML, returning the document when finished.
|
|
|
|
def self.each_onebox_link(string_or_doc)
|
|
|
|
doc = string_or_doc
|
2013-04-10 03:52:38 -04:00
|
|
|
doc = Nokogiri::HTML::fragment(doc) if doc.is_a?(String)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
onebox_links = doc.search("a.onebox")
|
|
|
|
if onebox_links.present?
|
|
|
|
onebox_links.each do |link|
|
2016-11-03 17:48:32 -04:00
|
|
|
yield(link['href'], link) if link['href'].present?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2015-09-25 06:14:38 -04:00
|
|
|
def self.append_source_topic_id(url, topic_id)
|
|
|
|
# hack urls to create proper expansions
|
2017-07-27 21:20:09 -04:00
|
|
|
if url =~ Regexp.new("^#{Discourse.base_url.gsub(".", "\\.")}.*$", true)
|
2015-09-25 06:14:38 -04:00
|
|
|
uri = URI.parse(url) rescue nil
|
|
|
|
if uri && uri.path
|
|
|
|
route = Rails.application.routes.recognize_path(uri.path) rescue nil
|
|
|
|
if route && route[:controller] == 'topics'
|
|
|
|
url += (url =~ /\?/ ? "&" : "?") + "source_topic_id=#{topic_id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
url
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.apply(string_or_doc, args = nil)
|
2013-04-10 03:52:38 -04:00
|
|
|
doc = string_or_doc
|
|
|
|
doc = Nokogiri::HTML::fragment(doc) if doc.is_a?(String)
|
|
|
|
changed = false
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
each_onebox_link(doc) do |url, element|
|
2015-09-25 06:14:38 -04:00
|
|
|
if args && args[:topic_id]
|
|
|
|
url = append_source_topic_id(url, args[:topic_id])
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
onebox, _preview = yield(url, element)
|
2013-04-10 03:52:38 -04:00
|
|
|
if onebox
|
|
|
|
parsed_onebox = Nokogiri::HTML::fragment(onebox)
|
2013-05-01 02:37:27 -04:00
|
|
|
next unless parsed_onebox.children.count > 0
|
2013-04-10 03:52:38 -04:00
|
|
|
|
|
|
|
# special logic to strip empty p elements
|
2013-05-01 02:37:27 -04:00
|
|
|
if element.parent &&
|
2015-09-21 22:42:13 -04:00
|
|
|
element.parent.node_name &&
|
2013-05-01 02:37:27 -04:00
|
|
|
element.parent.node_name.downcase == "p" &&
|
2014-07-04 16:09:51 -04:00
|
|
|
element.parent.children.count == 1
|
2013-05-01 02:37:27 -04:00
|
|
|
element = element.parent
|
2013-04-10 03:52:38 -04:00
|
|
|
end
|
|
|
|
changed = true
|
|
|
|
element.swap parsed_onebox.to_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Result.new(doc, changed)
|
|
|
|
end
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
def self.is_previewing?(user_id)
|
|
|
|
$redis.get(preview_key(user_id)) == "1"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preview_onebox!(user_id)
|
|
|
|
$redis.setex(preview_key(user_id), 1.minute, "1")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.onebox_previewed!(user_id)
|
|
|
|
$redis.del(preview_key(user_id))
|
|
|
|
end
|
|
|
|
|
2017-01-05 21:01:14 -05:00
|
|
|
def self.engine(url)
|
|
|
|
Onebox::Matcher.new(url).oneboxed
|
|
|
|
end
|
|
|
|
|
2014-03-17 22:12:58 -04:00
|
|
|
private
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
def self.preview_key(user_id)
|
2016-12-20 05:18:47 -05:00
|
|
|
"onebox:preview:#{user_id}"
|
2016-12-19 18:31:10 -05:00
|
|
|
end
|
|
|
|
|
2016-10-24 06:46:22 -04:00
|
|
|
def self.blank_onebox
|
|
|
|
{ preview: "", onebox: "" }
|
2014-04-09 16:57:45 -04:00
|
|
|
end
|
|
|
|
|
2016-10-24 06:46:22 -04:00
|
|
|
def self.onebox_cache_key(url)
|
|
|
|
"onebox__#{url}"
|
|
|
|
end
|
2015-08-23 20:43:07 -04:00
|
|
|
|
2016-10-24 06:46:22 -04:00
|
|
|
def self.onebox_raw(url)
|
|
|
|
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
|
2017-06-26 15:38:23 -04:00
|
|
|
fd = FinalDestination.new(url, ignore_redirects: ignore_redirects)
|
2017-06-06 15:02:11 -04:00
|
|
|
uri = fd.resolve
|
2016-10-24 06:46:22 -04:00
|
|
|
return blank_onebox if uri.blank? || SiteSetting.onebox_domains_blacklist.include?(uri.hostname)
|
2017-06-06 15:02:11 -04:00
|
|
|
options = {
|
|
|
|
cache: {},
|
|
|
|
max_width: 695,
|
|
|
|
sanitize_config: Sanitize::Config::DISCOURSE_ONEBOX
|
|
|
|
}
|
|
|
|
|
|
|
|
options[:cookie] = fd.cookie if fd.cookie
|
|
|
|
|
2017-06-06 16:39:15 -04:00
|
|
|
r = Onebox.preview(uri.to_s, options)
|
2016-10-24 06:46:22 -04:00
|
|
|
{ onebox: r.to_s, preview: r.try(:placeholder_html).to_s }
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
# no point warning here, just cause we have an issue oneboxing a url
|
|
|
|
# we can later hunt for failed oneboxes by searching logs if needed
|
|
|
|
Rails.logger.info("Failed to onebox #{url} #{e} #{e.backtrace}")
|
|
|
|
# return a blank hash, so rest of the code works
|
|
|
|
blank_onebox
|
|
|
|
end
|
2014-03-17 22:12:58 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|