discourse/lib/inline_oneboxer.rb
Sam Saffron 0fb497eb23 DEV: use Discourse.cache over Rails.cache
Discourse.cache is a more consistent method to use and offers clean fallback
if you are skipping redis

This is part of a larger change that both optimizes Discoruse.cache and omits
use of setex on $redis in favor of consistently using discourse cache

Bench does reveal that use of Rails.cache and Discourse.cache is 1.25x slower
than redis.setex / get so a re-implementation will follow prior to porting
2019-11-27 12:36:19 +11:00

84 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class InlineOneboxer
MIN_TITLE_LENGTH = 2
def initialize(urls, opts = nil)
@urls = urls
@opts = opts || {}
end
def process
@urls.map { |url| InlineOneboxer.lookup(url, @opts) }.compact
end
def self.purge(url)
Discourse.cache.delete(cache_key(url))
end
def self.cache_lookup(url)
Discourse.cache.read(cache_key(url))
end
def self.lookup(url, opts = nil)
opts ||= {}
opts = opts.with_indifferent_access
unless opts[:skip_cache] || opts[:invalidate]
cached = cache_lookup(url)
return cached if cached.present?
end
return unless url
if route = Discourse.route_for(url)
if route[:controller] == "topics" &&
route[:action] == "show" &&
topic = Topic.where(id: route[:topic_id].to_i).first
return onebox_for(url, topic.title, opts) if Guardian.new.can_see?(topic)
end
end
always_allow = SiteSetting.enable_inline_onebox_on_all_domains
domains = SiteSetting.inline_onebox_domains_whitelist&.split('|') unless always_allow
if always_allow || domains
uri = begin
URI(url)
rescue URI::Error
end
if uri.present? &&
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
nil
end
private
def self.onebox_for(url, title, opts)
onebox = {
url: url,
title: title && Emoji.gsub_emoji_to_unicode(title)
}
unless opts[:skip_cache]
Discourse.cache.write(cache_key(url), onebox, expires_in: 1.day)
end
onebox
end
def self.cache_key(url)
"inline_onebox:#{url}"
end
end