mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 07:38:01 +08:00
Merge pull request #3743 from sitepoint/topic-embed-info
Add embed/info endpoint for TopicEmbed queries
This commit is contained in:
commit
cc516d3be0
|
@ -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?))
|
||||
|
|
15
app/serializers/topic_embed_serializer.rb
Normal file
15
app/serializers/topic_embed_serializer.rb
Normal 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
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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) }
|
||||
|
||||
|
|
4
spec/fabricators/topic_embed_fabricator.rb
Normal file
4
spec/fabricators/topic_embed_fabricator.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
Fabricator(:topic_embed) do
|
||||
post
|
||||
topic {|te| te[:post].topic }
|
||||
end
|
Loading…
Reference in New Issue
Block a user