ensure we allow self oneboxing of login required sites

This commit is contained in:
Régis Hanol
2016-11-03 22:48:32 +01:00
parent a9d7569dda
commit a655e4b092
5 changed files with 76 additions and 146 deletions

View File

@ -3,118 +3,84 @@ module Onebox
class DiscourseLocalOnebox
include Engine
# we need to allow for multisite here
def self.is_on_site?(url)
Regexp.new("^#{Discourse.base_url.gsub(".","\\.")}.*$", true) === url.to_s
end
# Use this onebox before others
def self.priority
1
end
def self.===(other)
if other.kind_of?(URI)
uri = other
begin
route = Rails.application.routes.recognize_path(uri.path.sub(Discourse.base_uri, ""))
case route[:controller]
when 'uploads'
is_on_site?(other)
when 'topics'
# super will use matches_regexp to match the domain name
is_on_site?(other)
else
false
end
rescue ActionController::RoutingError
false
end
else
is_on_site?(other)
end
url = other.to_s
return false unless url[Discourse.base_url]
path = url.sub(Discourse.base_url, "")
route = Rails.application.routes.recognize_path(path)
!!(route[:controller] =~ /topics|uploads/)
rescue ActionController::RoutingError
false
end
def to_html
uri = URI::parse(@url)
route = Rails.application.routes.recognize_path(uri.path.sub(Discourse.base_uri, ""))
url = @url.sub(/[&?]source_topic_id=(\d+)/, "")
source_topic_id = $1.to_i
path = @url.sub(Discourse.base_url, "")
route = Rails.application.routes.recognize_path(path)
# Figure out what kind of onebox to show based on the URL
case route[:controller]
when 'uploads'
when "uploads" then upload_html(path)
when "topics" then topic_html(path, route)
end
end
url.gsub!("http:", "https:") if SiteSetting.force_https
if File.extname(uri.path) =~ /^.(mov|mp4|webm|ogv)$/
return "<video width='100%' height='100%' controls><source src='#{url}'><a href='#{url}'>#{url}</a></video>"
elsif File.extname(uri.path) =~ /^.(mp3|ogg|wav)$/
return "<audio controls><source src='#{url}'><a href='#{url}'>#{url}</a></audio>"
else
return false
private
def upload_html(path)
case File.extname(path)
when /^\.(mov|mp4|webm|ogv)$/
"<video width='100%' height='100%' controls><source src='#{@url}'><a href='#{@url}'>#{@url}</a></video>"
when /^\.(mp3|ogg|wav)$/
"<audio controls><source src='#{@url}'><a href='#{@url}'>#{@url}</a></audio>"
end
when 'topics'
end
def topic_html(path, route)
link = "<a href='#{@url}'>#{@url}</a>"
source_topic_id = @url[/[&?]source_topic_id=(\d+)/, 1].to_i
linked = "<a href='#{url}'>#{url}</a>"
if route[:post_number].present? && route[:post_number].to_i > 1
# Post Link
post = Post.find_by(topic_id: route[:topic_id], post_number: route[:post_number].to_i)
return linked unless post
return linked if post.hidden
return linked unless Guardian.new.can_see?(post)
post = Post.find_by(topic_id: route[:topic_id], post_number: route[:post_number])
return link if post.nil? || post.hidden || !Guardian.new.can_see?(post)
topic = post.topic
slug = Slug.for(topic.title)
excerpt = post.excerpt(SiteSetting.post_onebox_maxlength)
excerpt.gsub!("\n"," ")
# hack to make it render for now
excerpt.gsub!("[/quote]", "[quote]")
excerpt.gsub!(/[\r\n]+/, " ")
excerpt.gsub!("[/quote]", "[quote]") # don't break my quote
quote = "[quote=\"#{post.user.username}, topic:#{topic.id}, slug:#{slug}, post:#{post.post_number}\"]#{excerpt}[/quote]"
args = {}
args[:topic_id] = source_topic_id if source_topic_id > 0
cooked = PrettyText.cook(quote, args)
return cooked
PrettyText.cook(quote, args)
else
# Topic Link
topic = Topic.where(id: route[:topic_id].to_i).includes(:user).first
return linked unless topic
return linked unless Guardian.new.can_see?(topic)
topic = Topic.find_by(id: route[:topic_id])
return link if topic.nil? || !Guardian.new.can_see?(topic)
post = topic.posts.first
first_post = topic.ordered_posts.first
posters = topic.posters_summary.map do |p|
{
username: p[:user].username,
avatar: PrettyText.avatar_img(p[:user].avatar_template, 'tiny'),
description: p[:description],
extras: p[:extras]
}
end
args = {
topic: topic.id,
avatar: PrettyText.avatar_img(topic.user.avatar_template, "tiny"),
original_url: @url,
title: PrettyText.unescape_emoji(CGI::escapeHTML(topic.title)),
category_html: CategoryBadge.html_for(topic.category),
quote: first_post.excerpt(SiteSetting.post_onebox_maxlength),
}
quote = post.excerpt(SiteSetting.post_onebox_maxlength)
args = { original_url: url,
title: PrettyText.unescape_emoji(CGI::escapeHTML(topic.title)),
avatar: PrettyText.avatar_img(topic.user.avatar_template, 'tiny'),
posts_count: topic.posts_count,
last_post: FreedomPatches::Rails4.time_ago_in_words(topic.last_posted_at, false, scope: :'datetime.distance_in_words_verbose'),
age: FreedomPatches::Rails4.time_ago_in_words(topic.created_at, false, scope: :'datetime.distance_in_words_verbose'),
views: topic.views,
posters: posters,
quote: quote,
category_html: CategoryBadge.html_for(topic.category),
topic: topic.id }
return Mustache.render(File.read("#{Rails.root}/lib/onebox/templates/discourse_topic_onebox.hbs"), args)
template = File.read("#{Rails.root}/lib/onebox/templates/discourse_topic_onebox.hbs")
Mustache.render(template, args)
end
end
rescue ActionController::RoutingError
nil
end
end
end
end

View File

@ -1,11 +1,9 @@
<aside class='quote' data-post="1" data-topic="{{topic}}">
<div class='title'>
<div class='quote-controls'></div>
{{{avatar}}}
<a href="{{original_url}}">{{{title}}}</a> {{{category_html}}}
</div>
<blockquote>{{{quote}}}
<div class='topic-info'>
</div>
<blockquote>
{{{quote}}}
</blockquote>
</aside>