FEATURE: Inline (Mini) Oneboxing

see:
https://meta.discourse.org/t/mini-inline-onebox-support-rfc/66400?source_topic_id=66066
This commit is contained in:
Robin Ward
2017-07-19 15:08:54 -04:00
parent 44fb2a2833
commit 3882722195
18 changed files with 306 additions and 8 deletions

47
lib/inline_oneboxer.rb Normal file
View File

@@ -0,0 +1,47 @@
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" &&
topic = Topic.where(id: route[:topic_id].to_i).first
# Only public topics
if Guardian.new.can_see?(topic)
onebox = { url: url, title: topic.title }
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