2014-01-01 03:37:43 +08:00
|
|
|
class EmbedController < ApplicationController
|
2017-08-31 12:06:56 +08:00
|
|
|
skip_before_action :check_xhr, :preload_json, :verify_authenticity_token
|
2015-05-06 09:00:31 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
before_action :ensure_embeddable, except: [ :info ]
|
|
|
|
before_action :get_embeddable_css_class, except: [ :info ]
|
|
|
|
before_action :ensure_api_request, only: [ :info ]
|
2014-01-01 03:37:43 +08:00
|
|
|
|
|
|
|
layout 'embed'
|
|
|
|
|
2016-12-14 03:37:37 +08:00
|
|
|
rescue_from Discourse::InvalidAccess do
|
|
|
|
response.headers['X-Frame-Options'] = "ALLOWALL"
|
|
|
|
if current_user.try(:admin?)
|
|
|
|
@setup_url = "#{Discourse.base_url}/admin/customize/embedding"
|
|
|
|
@show_reason = true
|
|
|
|
@hosts = EmbeddableHost.all
|
|
|
|
end
|
|
|
|
render 'embed_error'
|
|
|
|
end
|
|
|
|
|
2014-01-04 01:52:24 +08:00
|
|
|
def comments
|
2015-06-10 04:24:04 +08:00
|
|
|
embed_url = params[:embed_url]
|
2016-02-25 19:16:27 +08:00
|
|
|
embed_username = params[:discourse_username]
|
2015-06-10 04:24:04 +08:00
|
|
|
|
|
|
|
topic_id = nil
|
|
|
|
if embed_url.present?
|
|
|
|
topic_id = TopicEmbed.topic_id_for_embed(embed_url)
|
|
|
|
else
|
|
|
|
topic_id = params[:topic_id].to_i
|
|
|
|
end
|
2014-01-01 03:37:43 +08:00
|
|
|
|
|
|
|
if topic_id
|
2014-06-19 05:39:12 +08:00
|
|
|
@topic_view = TopicView.new(topic_id,
|
|
|
|
current_user,
|
|
|
|
limit: SiteSetting.embed_post_limit,
|
|
|
|
exclude_first: true,
|
2016-05-04 03:01:20 +08:00
|
|
|
exclude_deleted_users: true,
|
|
|
|
exclude_hidden: true)
|
2014-06-19 05:39:12 +08:00
|
|
|
|
2014-01-03 01:15:48 +08:00
|
|
|
@second_post_url = "#{@topic_view.topic.url}/2" if @topic_view
|
2014-01-04 03:55:37 +08:00
|
|
|
@posts_left = 0
|
|
|
|
if @topic_view && @topic_view.posts.size == SiteSetting.embed_post_limit
|
2015-05-11 16:26:21 +08:00
|
|
|
@posts_left = @topic_view.topic.posts_count - SiteSetting.embed_post_limit - 1
|
2014-01-04 03:55:37 +08:00
|
|
|
end
|
2015-06-10 04:24:04 +08:00
|
|
|
|
2015-11-21 03:27:30 +08:00
|
|
|
if @topic_view
|
|
|
|
@reply_count = @topic_view.topic.posts_count - 1
|
|
|
|
@reply_count = 0 if @reply_count < 0
|
|
|
|
end
|
2015-06-10 04:24:04 +08:00
|
|
|
elsif embed_url.present?
|
2016-04-25 08:47:38 +08:00
|
|
|
Jobs.enqueue(:retrieve_topic,
|
|
|
|
user_id: current_user.try(:id),
|
|
|
|
embed_url: embed_url,
|
|
|
|
author_username: embed_username,
|
|
|
|
referer: request.env['HTTP_REFERER']
|
|
|
|
)
|
2014-01-01 03:37:43 +08:00
|
|
|
render 'loading'
|
|
|
|
end
|
|
|
|
|
|
|
|
discourse_expires_in 1.minute
|
|
|
|
end
|
|
|
|
|
2015-05-06 09:00:31 +08:00
|
|
|
def info
|
|
|
|
embed_url = params.require(:embed_url)
|
|
|
|
@topic_embed = TopicEmbed.where(embed_url: embed_url).first
|
|
|
|
|
|
|
|
raise Discourse::NotFound if @topic_embed.nil?
|
|
|
|
|
|
|
|
render_serialized(@topic_embed, TopicEmbedSerializer, root: false)
|
|
|
|
end
|
|
|
|
|
2014-01-14 01:47:24 +08:00
|
|
|
def count
|
2014-05-21 03:20:02 +08:00
|
|
|
embed_urls = params[:embed_url]
|
2014-01-14 01:47:24 +08:00
|
|
|
by_url = {}
|
2014-05-21 03:20:02 +08:00
|
|
|
|
|
|
|
if embed_urls.present?
|
2017-07-28 09:20:09 +08:00
|
|
|
urls = embed_urls.map { |u| u.sub(/#discourse-comments$/, '').sub(/\/$/, '') }
|
2014-05-21 03:20:02 +08:00
|
|
|
topic_embeds = TopicEmbed.where(embed_url: urls).includes(:topic).references(:topic)
|
|
|
|
|
|
|
|
topic_embeds.each do |te|
|
2015-05-01 21:04:45 +08:00
|
|
|
url = te.embed_url
|
2014-05-21 03:20:02 +08:00
|
|
|
url = "#{url}#discourse-comments" unless params[:embed_url].include?(url)
|
2016-08-25 09:27:00 +08:00
|
|
|
if te.topic.present?
|
|
|
|
by_url[url] = I18n.t('embed.replies', count: te.topic.posts_count - 1)
|
|
|
|
else
|
|
|
|
by_url[url] = I18n.t('embed.replies', count: 0)
|
|
|
|
end
|
2014-05-21 03:20:02 +08:00
|
|
|
end
|
2014-01-14 01:47:24 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
render json: { counts: by_url }, callback: params[:callback]
|
2014-01-14 01:47:24 +08:00
|
|
|
end
|
|
|
|
|
2014-01-01 03:37:43 +08:00
|
|
|
private
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def get_embeddable_css_class
|
|
|
|
@embeddable_css_class = ""
|
|
|
|
embeddable_host = EmbeddableHost.record_for_url(request.referer)
|
|
|
|
@embeddable_css_class = " class=\"#{embeddable_host.class_name}\"" if embeddable_host.present? && embeddable_host.class_name.present?
|
|
|
|
end
|
2017-05-09 00:58:36 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def ensure_api_request
|
|
|
|
raise Discourse::InvalidAccess.new('api key not set') if !is_api?
|
|
|
|
end
|
2015-05-06 09:00:31 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def ensure_embeddable
|
|
|
|
if !(Rails.env.development? && current_user&.admin?)
|
|
|
|
referer = request.referer
|
2014-01-03 00:32:50 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
unless referer && EmbeddableHost.url_allowed?(referer)
|
|
|
|
raise Discourse::InvalidAccess.new('invalid referer host')
|
2014-01-03 00:32:50 +08:00
|
|
|
end
|
2014-01-01 03:37:43 +08:00
|
|
|
end
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
response.headers['X-Frame-Options'] = "ALLOWALL"
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
raise Discourse::InvalidAccess.new('invalid referer host')
|
|
|
|
end
|
|
|
|
|
2014-01-01 03:37:43 +08:00
|
|
|
end
|