mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
47
lib/inline_oneboxer.rb
Normal file
47
lib/inline_oneboxer.rb
Normal 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
|
||||
|
Reference in New Issue
Block a user