FEATURE: Allow using 'top' view for topic list embed (#12825)

This commit is contained in:
Rafael dos Santos Silva 2021-04-26 18:10:04 -03:00 committed by GitHub
parent a872df87f6
commit a74783d157
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -56,7 +56,14 @@ class EmbedController < ApplicationController
end end
topic_query = TopicQuery.new(current_user, list_options) topic_query = TopicQuery.new(current_user, list_options)
@list = topic_query.list_latest top_period = params[:top_period]&.to_sym
valid_top_period = TopTopic.periods.include?(top_period)
@list = if valid_top_period
topic_query.list_top_for(top_period)
else
topic_query.list_latest
end
end end
def comments def comments

View File

@ -100,6 +100,22 @@ describe EmbedController do
expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"") expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")
end end
it "returns a list of topics" do
bad_topic = Fabricate(:topic)
good_topic = Fabricate(:topic, like_count: 1000, posts_count: 100)
TopTopic.refresh!
get '/embed/topics?discourse_embed_id=de-1234&top_period=yearly', headers: {
'REFERER' => 'https://example.com/evil-trout'
}
expect(response.status).to eq(200)
expect(response.headers['X-Frame-Options']).to be_nil
expect(response.body).to match("data-embed-id=\"de-1234\"")
expect(response.body).to match("data-topic-id=\"#{good_topic.id}\"")
expect(response.body).not_to match("data-topic-id=\"#{bad_topic.id}\"")
expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")
end
it "returns no referer if not supplied" do it "returns no referer if not supplied" do
get '/embed/topics?discourse_embed_id=de-1234' get '/embed/topics?discourse_embed_id=de-1234'
expect(response.status).to eq(200) expect(response.status).to eq(200)