2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'oneboxer/oembed_onebox'
|
|
|
|
require_dependency 'freedom_patches/rails4'
|
|
|
|
|
|
|
|
module Oneboxer
|
2013-03-08 15:58:37 -05:00
|
|
|
class DiscourseLocalOnebox < BaseOnebox
|
2013-02-05 14:16:51 -05:00
|
|
|
include ActionView::Helpers::DateHelper
|
|
|
|
|
2013-02-06 00:22:11 -05:00
|
|
|
matcher do
|
|
|
|
Regexp.new "^#{Discourse.base_url.gsub(".","\\.")}.*$", true
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def onebox
|
|
|
|
uri = URI::parse(@url)
|
|
|
|
route = Rails.application.routes.recognize_path(uri.path)
|
|
|
|
|
|
|
|
args = {original_url: @url}
|
|
|
|
|
|
|
|
# Figure out what kind of onebox to show based on the URL
|
|
|
|
case route[:controller]
|
2013-09-13 17:01:28 -04:00
|
|
|
# when 'users'
|
|
|
|
# user = User.where(username_lower: route[:username].downcase).first
|
|
|
|
# return nil unless user
|
2013-04-17 20:04:42 -04:00
|
|
|
|
2013-09-13 17:01:28 -04:00
|
|
|
# return @url unless Guardian.new.can_see?(user)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-13 17:01:28 -04:00
|
|
|
# args.merge! avatar: PrettyText.avatar_img(user.avatar_template, 'tiny'), username: user.username
|
|
|
|
# args[:bio] = user.bio_cooked if user.bio_cooked.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-13 17:01:28 -04:00
|
|
|
# @template = 'user'
|
2013-02-05 14:16:51 -05:00
|
|
|
when 'topics'
|
2013-08-24 15:08:36 -04:00
|
|
|
|
|
|
|
linked = "<a href='#{@url}'>#{@url}</a>"
|
2013-03-04 19:42:44 -05:00
|
|
|
if route[:post_number].present? && route[:post_number].to_i > 1
|
2013-02-05 14:16:51 -05:00
|
|
|
# Post Link
|
|
|
|
post = Post.where(topic_id: route[:topic_id], post_number: route[:post_number].to_i).first
|
2013-08-24 15:08:36 -04:00
|
|
|
return linked unless post
|
|
|
|
return linked unless Guardian.new.can_see?(post)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
topic = post.topic
|
|
|
|
slug = Slug.for(topic.title)
|
|
|
|
|
|
|
|
excerpt = post.excerpt(SiteSetting.post_onebox_maxlength)
|
|
|
|
excerpt.gsub!("\n"," ")
|
2013-02-22 15:41:12 -05:00
|
|
|
# hack to make it render for now
|
2013-02-05 14:16:51 -05:00
|
|
|
excerpt.gsub!("[/quote]", "[quote]")
|
|
|
|
quote = "[quote=\"#{post.user.username}, topic:#{topic.id}, slug:#{slug}, post:#{post.post_number}\"]#{excerpt}[/quote]"
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
cooked = PrettyText.cook(quote)
|
2013-02-22 15:41:12 -05:00
|
|
|
return cooked
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
else
|
|
|
|
# Topic Link
|
2013-02-22 15:41:12 -05:00
|
|
|
topic = Topic.where(id: route[:topic_id].to_i).includes(:user).first
|
2013-08-24 15:08:36 -04:00
|
|
|
return linked unless topic
|
|
|
|
return linked unless Guardian.new.can_see?(topic)
|
2013-07-15 15:10:05 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
post = topic.posts.first
|
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
posters = topic.posters_summary.map do |p|
|
2013-08-15 18:24:21 -04:00
|
|
|
{
|
|
|
|
username: p[:user].username,
|
|
|
|
avatar: PrettyText.avatar_img(p[:user].avatar_template, 'tiny'),
|
|
|
|
description: p[:description],
|
|
|
|
extras: p[:extras]
|
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
category = topic.category
|
|
|
|
if category
|
2013-06-07 15:29:12 -04:00
|
|
|
category = "<a href=\"/category/#{category.slug}\" class=\"badge badge-category\" style=\"background-color: ##{category.color}\">#{category.name}</a>"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
quote = post.excerpt(SiteSetting.post_onebox_maxlength)
|
|
|
|
args.merge! title: topic.title,
|
2013-08-15 18:24:21 -04:00
|
|
|
avatar: PrettyText.avatar_img(topic.user.avatar_template, 'tiny'),
|
2013-02-05 14:16:51 -05:00
|
|
|
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: category,
|
|
|
|
topic: topic.id
|
|
|
|
|
|
|
|
@template = 'topic'
|
|
|
|
end
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
return nil unless @template
|
2013-12-23 18:50:36 -05:00
|
|
|
Mustache.render(File.read("#{Rails.root}/lib/oneboxer/templates/discourse_#{@template}_onebox.handlebars"), args)
|
2013-02-05 14:16:51 -05:00
|
|
|
rescue ActionController::RoutingError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|