2013-12-31 14:37:43 -05:00
|
|
|
class EmbedController < ApplicationController
|
|
|
|
skip_before_filter :check_xhr
|
|
|
|
skip_before_filter :preload_json
|
2014-05-09 16:25:52 -04:00
|
|
|
skip_before_filter :verify_authenticity_token
|
2014-01-13 12:58:53 -05:00
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
before_filter :ensure_embeddable
|
|
|
|
|
|
|
|
layout 'embed'
|
|
|
|
|
2014-01-03 12:52:24 -05:00
|
|
|
def comments
|
2013-12-31 14:37:43 -05:00
|
|
|
embed_url = params.require(:embed_url)
|
|
|
|
topic_id = TopicEmbed.topic_id_for_embed(embed_url)
|
|
|
|
|
|
|
|
if topic_id
|
2014-06-18 17:39:12 -04:00
|
|
|
@topic_view = TopicView.new(topic_id,
|
|
|
|
current_user,
|
|
|
|
limit: SiteSetting.embed_post_limit,
|
|
|
|
exclude_first: true,
|
|
|
|
exclude_deleted_users: true)
|
|
|
|
|
2014-01-02 12:15:48 -05:00
|
|
|
@second_post_url = "#{@topic_view.topic.url}/2" if @topic_view
|
2014-01-03 14:55:37 -05:00
|
|
|
@posts_left = 0
|
|
|
|
if @topic_view && @topic_view.posts.size == SiteSetting.embed_post_limit
|
|
|
|
@posts_left = @topic_view.topic.posts_count - SiteSetting.embed_post_limit
|
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
else
|
|
|
|
Jobs.enqueue(:retrieve_topic, user_id: current_user.try(:id), embed_url: embed_url)
|
|
|
|
render 'loading'
|
|
|
|
end
|
|
|
|
|
|
|
|
discourse_expires_in 1.minute
|
|
|
|
end
|
|
|
|
|
2014-01-13 12:47:24 -05:00
|
|
|
def count
|
|
|
|
|
2014-05-20 15:20:02 -04:00
|
|
|
embed_urls = params[:embed_url]
|
2014-01-13 12:47:24 -05:00
|
|
|
by_url = {}
|
2014-05-20 15:20:02 -04:00
|
|
|
|
|
|
|
if embed_urls.present?
|
|
|
|
urls = embed_urls.map {|u| u.sub(/#discourse-comments$/, '').sub(/\/$/, '') }
|
|
|
|
topic_embeds = TopicEmbed.where(embed_url: urls).includes(:topic).references(:topic)
|
|
|
|
|
|
|
|
topic_embeds.each do |te|
|
|
|
|
url = te.embed_url
|
|
|
|
url = "#{url}#discourse-comments" unless params[:embed_url].include?(url)
|
|
|
|
by_url[url] = I18n.t('embed.replies', count: te.topic.posts_count - 1)
|
|
|
|
end
|
2014-01-13 12:47:24 -05:00
|
|
|
end
|
|
|
|
|
2014-04-14 12:15:41 -04:00
|
|
|
render json: {counts: by_url}, callback: params[:callback]
|
2014-01-13 12:47:24 -05:00
|
|
|
end
|
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_embeddable
|
2014-01-02 11:32:50 -05:00
|
|
|
|
|
|
|
if !(Rails.env.development? && current_user.try(:admin?))
|
2014-02-12 15:55:44 -05:00
|
|
|
raise Discourse::InvalidAccess.new('embeddable host not set') if SiteSetting.normalized_embeddable_host.blank?
|
|
|
|
raise Discourse::InvalidAccess.new('invalid referer host') if URI(request.referer || '').host != SiteSetting.normalized_embeddable_host
|
2014-01-02 11:32:50 -05:00
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
response.headers['X-Frame-Options'] = "ALLOWALL"
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
raise Discourse::InvalidAccess.new('invalid referer host')
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|