diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rb index e309776c6..843a95092 100644 --- a/app/controllers/embed_controller.rb +++ b/app/controllers/embed_controller.rb @@ -1,6 +1,8 @@ class EmbedController < ApplicationController skip_before_filter :check_xhr, :preload_json, :verify_authenticity_token - before_filter :ensure_embeddable + + before_filter :ensure_embeddable, except: [ :info ] + before_filter :ensure_api_request, only: [ :info ] layout 'embed' @@ -35,6 +37,15 @@ class EmbedController < ApplicationController discourse_expires_in 1.minute end + 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 + def count embed_urls = params[:embed_url] by_url = {} @@ -55,6 +66,10 @@ class EmbedController < ApplicationController private + def ensure_api_request + raise Discourse::InvalidAccess.new('api key not set') if !is_api? + end + def ensure_embeddable if !(Rails.env.development? && current_user.try(:admin?)) diff --git a/app/serializers/topic_embed_serializer.rb b/app/serializers/topic_embed_serializer.rb new file mode 100644 index 000000000..ac06bf1f3 --- /dev/null +++ b/app/serializers/topic_embed_serializer.rb @@ -0,0 +1,15 @@ +class TopicEmbedSerializer < ApplicationSerializer + attributes \ + :topic_id, + :post_id, + :topic_slug, + :comment_count + + def topic_slug + object.topic.slug + end + + def comment_count + object.topic.posts_count - 1 + end +end diff --git a/config/routes.rb b/config/routes.rb index 66125570f..187b51d2c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -441,6 +441,7 @@ Discourse::Application.routes.draw do get 'embed/comments' => 'embed#comments' get 'embed/count' => 'embed#count' + get 'embed/info' => 'embed#info' get "new-topic" => "list#latest" diff --git a/spec/controllers/embed_controller_spec.rb b/spec/controllers/embed_controller_spec.rb index 316c9a689..9947844c7 100644 --- a/spec/controllers/embed_controller_spec.rb +++ b/spec/controllers/embed_controller_spec.rb @@ -29,6 +29,40 @@ describe EmbedController do end end + context ".info" do + context "without api key" do + it "fails" do + get :info, format: :json + expect(response).not_to be_success + end + end + + context "with api key" do + + let(:api_key) { ApiKey.create_master_key } + + context "with valid embed url" do + let(:topic_embed) { Fabricate(:topic_embed, embed_url: embed_url) } + + it "returns information about the topic" do + get :info, format: :json, embed_url: topic_embed.embed_url, api_key: api_key.key, api_username: "system" + json = JSON.parse(response.body) + expect(json['topic_id']).to eq(topic_embed.topic.id) + expect(json['post_id']).to eq(topic_embed.post.id) + expect(json['topic_slug']).to eq(topic_embed.topic.slug) + end + end + + context "without invalid embed url" do + it "returns error response" do + get :info, format: :json, embed_url: "http://nope.com", api_key: api_key.key, api_username: "system" + json = JSON.parse(response.body) + expect(json["error_type"]).to eq("not_found") + end + end + end + end + context "with a host" do let!(:embeddable_host) { Fabricate(:embeddable_host) } diff --git a/spec/fabricators/topic_embed_fabricator.rb b/spec/fabricators/topic_embed_fabricator.rb new file mode 100644 index 000000000..407df3db9 --- /dev/null +++ b/spec/fabricators/topic_embed_fabricator.rb @@ -0,0 +1,4 @@ +Fabricator(:topic_embed) do + post + topic {|te| te[:post].topic } +end