Merge pull request #3743 from sitepoint/topic-embed-info

Add embed/info endpoint for TopicEmbed queries
This commit is contained in:
Robin Ward 2015-09-18 11:00:01 -04:00
commit cc516d3be0
5 changed files with 70 additions and 1 deletions

View File

@ -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?))

View File

@ -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

View File

@ -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"

View File

@ -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) }

View File

@ -0,0 +1,4 @@
Fabricator(:topic_embed) do
post
topic {|te| te[:post].topic }
end