2014-01-29 02:18:19 +08:00
|
|
|
module Onebox
|
|
|
|
module Engine
|
|
|
|
class DiscourseLocalOnebox
|
|
|
|
include Engine
|
|
|
|
|
2014-02-26 03:08:48 +08:00
|
|
|
# Use this onebox before others
|
|
|
|
def self.priority
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
2015-04-22 14:06:40 +08:00
|
|
|
def self.===(other)
|
2016-11-04 05:48:32 +08:00
|
|
|
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
|
2015-04-22 14:06:40 +08:00
|
|
|
end
|
|
|
|
|
2014-01-29 02:18:19 +08:00
|
|
|
def to_html
|
2016-11-04 05:48:32 +08:00
|
|
|
path = @url.sub(Discourse.base_url, "")
|
|
|
|
route = Rails.application.routes.recognize_path(path)
|
2014-01-29 02:18:19 +08:00
|
|
|
|
|
|
|
case route[:controller]
|
2016-11-04 05:48:32 +08:00
|
|
|
when "uploads" then upload_html(path)
|
2016-11-08 01:14:28 +08:00
|
|
|
when "topics" then topic_html(route)
|
2016-11-04 05:48:32 +08:00
|
|
|
end
|
|
|
|
end
|
2015-12-25 04:22:14 +08:00
|
|
|
|
2016-11-04 05:48:32 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def upload_html(path)
|
|
|
|
case File.extname(path)
|
2016-12-16 06:21:44 +08:00
|
|
|
when /^\.(mov|mp4|webm|ogv)$/i
|
2016-11-04 05:48:32 +08:00
|
|
|
"<video width='100%' height='100%' controls><source src='#{@url}'><a href='#{@url}'>#{@url}</a></video>"
|
2016-12-16 06:21:44 +08:00
|
|
|
when /^\.(mp3|ogg|wav|m4a)$/i
|
2016-11-04 05:48:32 +08:00
|
|
|
"<audio controls><source src='#{@url}'><a href='#{@url}'>#{@url}</a></audio>"
|
2015-12-25 04:22:14 +08:00
|
|
|
end
|
2016-11-04 05:48:32 +08:00
|
|
|
end
|
|
|
|
|
2016-11-08 01:14:28 +08:00
|
|
|
def topic_html(route)
|
2016-11-04 05:48:32 +08:00
|
|
|
link = "<a href='#{@url}'>#{@url}</a>"
|
|
|
|
source_topic_id = @url[/[&?]source_topic_id=(\d+)/, 1].to_i
|
2016-11-08 01:14:28 +08:00
|
|
|
source_topic = Topic.find_by(id: source_topic_id) if source_topic_id > 0
|
2014-01-29 02:18:19 +08:00
|
|
|
|
|
|
|
if route[:post_number].present? && route[:post_number].to_i > 1
|
2016-11-04 05:48:32 +08:00
|
|
|
post = Post.find_by(topic_id: route[:topic_id], post_number: route[:post_number])
|
2016-11-08 01:14:28 +08:00
|
|
|
return link unless can_see_post?(post, source_topic)
|
2014-01-29 02:18:19 +08:00
|
|
|
|
|
|
|
topic = post.topic
|
|
|
|
slug = Slug.for(topic.title)
|
2016-06-02 10:29:25 +08:00
|
|
|
excerpt = post.excerpt(SiteSetting.post_onebox_maxlength)
|
2016-11-04 05:48:32 +08:00
|
|
|
excerpt.gsub!(/[\r\n]+/, " ")
|
|
|
|
excerpt.gsub!("[/quote]", "[quote]") # don't break my quote
|
|
|
|
|
2017-06-27 22:57:29 +08:00
|
|
|
quote = "[quote=\"#{post.user.username}, topic:#{topic.id}, slug:#{slug}, post:#{post.post_number}\"]\n#{excerpt}\n[/quote]"
|
2014-01-29 02:18:19 +08:00
|
|
|
|
2015-09-25 12:52:43 +08:00
|
|
|
args = {}
|
|
|
|
args[:topic_id] = source_topic_id if source_topic_id > 0
|
2014-01-29 02:18:19 +08:00
|
|
|
|
2016-11-04 05:48:32 +08:00
|
|
|
PrettyText.cook(quote, args)
|
2014-01-29 02:18:19 +08:00
|
|
|
else
|
2016-11-04 05:48:32 +08:00
|
|
|
topic = Topic.find_by(id: route[:topic_id])
|
2016-11-08 01:14:28 +08:00
|
|
|
return link unless can_see_topic?(topic, source_topic)
|
2016-11-04 05:48:32 +08:00
|
|
|
|
|
|
|
first_post = topic.ordered_posts.first
|
|
|
|
|
|
|
|
args = {
|
2017-02-06 21:45:04 +08:00
|
|
|
topic_id: topic.id,
|
2016-11-04 05:48:32 +08:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
template = File.read("#{Rails.root}/lib/onebox/templates/discourse_topic_onebox.hbs")
|
|
|
|
Mustache.render(template, args)
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 01:14:28 +08:00
|
|
|
def can_see_post?(post, source_topic)
|
|
|
|
return false if post.nil? || post.hidden || post.trashed? || post.topic.nil?
|
|
|
|
Guardian.new.can_see_post?(post) || same_category?(post.topic.category, source_topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_topic?(topic, source_topic)
|
|
|
|
return false if topic.nil? || topic.trashed? || topic.private_message?
|
|
|
|
Guardian.new.can_see_topic?(topic) || same_category?(topic.category, source_topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_category?(category, source_topic)
|
|
|
|
source_topic.try(:category_id) == category.try(:id)
|
|
|
|
end
|
|
|
|
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|