2017-07-19 15:08:54 -04:00
|
|
|
class InlineOneboxer
|
|
|
|
|
|
|
|
def initialize(urls)
|
|
|
|
@urls = urls
|
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
|
|
|
@urls.map {|url| InlineOneboxer.lookup(url) }.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cache_lookup(url)
|
|
|
|
Rails.cache.read(cache_key(url))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup(url)
|
|
|
|
cached = cache_lookup(url)
|
|
|
|
return cached if cached.present?
|
|
|
|
|
|
|
|
if route = Discourse.route_for(url)
|
|
|
|
if route[:controller] == "topics" &&
|
|
|
|
route[:action] == "show" &&
|
2017-07-20 16:01:16 -04:00
|
|
|
topic = (Topic.where(id: route[:topic_id].to_i).first rescue nil)
|
2017-07-19 15:08:54 -04:00
|
|
|
|
|
|
|
# Only public topics
|
|
|
|
if Guardian.new.can_see?(topic)
|
2017-07-21 14:24:28 -04:00
|
|
|
onebox = {
|
|
|
|
url: url,
|
|
|
|
title: Emoji.gsub_emoji_to_unicode(topic.title)
|
|
|
|
}
|
2017-07-19 15:08:54 -04:00
|
|
|
Rails.cache.write(cache_key(url), onebox, expires_in: 1.day)
|
|
|
|
return onebox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.cache_key(url)
|
|
|
|
"inline_onebox:#{url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|