Add embed/info endpoint for TopicEmbed queries

This commit is contained in:
Jude Aakjaer 2015-05-06 01:00:31 +00:00
parent 83efde79f0
commit 9cca510944
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