FEATURE: Allow embedding topics without creating them, by id

This commit is contained in:
Robin Ward 2015-06-09 16:24:04 -04:00
parent 49ca248186
commit ae277e28a6
3 changed files with 61 additions and 25 deletions

View File

@ -1,13 +1,18 @@
class EmbedController < ApplicationController class EmbedController < ApplicationController
skip_before_filter :check_xhr, :preload_json, :verify_authenticity_token skip_before_filter :check_xhr, :preload_json, :verify_authenticity_token
before_filter :ensure_embeddable before_filter :ensure_embeddable
layout 'embed' layout 'embed'
def comments def comments
embed_url = params.require(:embed_url) embed_url = params[:embed_url]
topic_id = TopicEmbed.topic_id_for_embed(embed_url)
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
if topic_id if topic_id
@topic_view = TopicView.new(topic_id, @topic_view = TopicView.new(topic_id,
@ -21,7 +26,8 @@ class EmbedController < ApplicationController
if @topic_view && @topic_view.posts.size == SiteSetting.embed_post_limit if @topic_view && @topic_view.posts.size == SiteSetting.embed_post_limit
@posts_left = @topic_view.topic.posts_count - SiteSetting.embed_post_limit - 1 @posts_left = @topic_view.topic.posts_count - SiteSetting.embed_post_limit - 1
end end
else
elsif embed_url.present?
Jobs.enqueue(:retrieve_topic, user_id: current_user.try(:id), embed_url: embed_url) Jobs.enqueue(:retrieve_topic, user_id: current_user.try(:id), embed_url: embed_url)
render 'loading' render 'loading'
end end
@ -30,7 +36,6 @@ class EmbedController < ApplicationController
end end
def count def count
embed_urls = params[:embed_url] embed_urls = params[:embed_url]
by_url = {} by_url = {}

View File

@ -1,24 +1,41 @@
/* global discourseUrl */
/* global discourseUserName */
/* global discourseEmbedUrl */
(function() { (function() {
var comments = document.getElementById('discourse-comments'),
iframe = document.createElement('iframe'); var DE = window.DiscourseEmbed || {};
if (typeof discourseUserName === 'undefined') { var comments = document.getElementById('discourse-comments');
iframe.src = var iframe = document.createElement('iframe');
[ discourseUrl,
'embed/comments?embed_url=', ['discourseUrl', 'discourseEmbedUrl', 'discourseUserName'].forEach(function(i) {
encodeURIComponent(discourseEmbedUrl) if (window[i]) { DE[i] = DE[i] || window[i]; }
].join(''); });
} else {
iframe.src = var queryParams = {};
[ discourseUrl,
'embed/comments?embed_url=', if (DE.discourseEmbedUrl) {
encodeURIComponent(discourseEmbedUrl), queryParams.embed_url = encodeURIComponent(DE.discourseEmbedUrl);
'&discourse_username=',
discourseUserName
].join('');
} }
if (DE.discourseUserName) {
queryParams.discourse_username = DE.discourseUserName;
}
if (DE.topicId) {
queryParams.topic_id = DE.topicId;
}
var src = DE.discourseUrl + 'embed/comments';
var keys = Object.keys(queryParams);
if (keys.length > 0) {
src += "?";
for (var i=0; i<keys.length; i++) {
if (i > 0) { src += "&"; }
var k = keys[i];
src += k + "=" + queryParams[k];
}
}
iframe.src = src;
iframe.id = 'discourse-embed-frame'; iframe.id = 'discourse-embed-frame';
iframe.width = "100%"; iframe.width = "100%";
iframe.frameBorder = "0"; iframe.frameBorder = "0";
@ -48,7 +65,7 @@
function postMessageReceived(e) { function postMessageReceived(e) {
if (!e) { return; } if (!e) { return; }
if (discourseUrl.indexOf(e.origin) === -1) { return; } if (DE.discourseUrl.indexOf(e.origin) === -1) { return; }
if (e.data) { if (e.data) {
if (e.data.type === 'discourse-resize' && e.data.height) { if (e.data.type === 'discourse-resize' && e.data.height) {

View File

@ -16,6 +16,20 @@ describe EmbedController do
expect(response).not_to be_success expect(response).not_to be_success
end end
context "by topic id" do
before do
SiteSetting.embeddable_hosts = host
controller.request.stubs(:referer).returns('http://eviltrout.com/some-page')
end
it "allows a topic to be embedded by id" do
topic = Fabricate(:topic)
get :comments, topic_id: topic.id
expect(response).to be_success
end
end
context "with a host" do context "with a host" do
before do before do
SiteSetting.embeddable_hosts = host SiteSetting.embeddable_hosts = host