A platform for community discussion. Free, open, simple.
DEV: Gate Reply-count endpoint for non-public embedded topics (#38498)
Isaac Janzen915d38edb8bb
diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rbindex d58f26160ce..45a8d861b2b 100644--- a/app/controllers/embed_controller.rb+++ b/app/controllers/embed_controller.rb@@ -149,6 +149,8 @@ class EmbedController < ApplicationControllertopic_embeds = TopicEmbed.where(embed_url: urls).includes(:topic).references(:topic)topic_embeds.each do |te|+ next if te.topic.present? && !guardian.can_see?(te.topic)+url = te.embed_urlurl = "#{url}#discourse-comments" if params[:embed_url].exclude?(url)if te.topic.present?diff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rbindex 3f230c15012..66bbd5bc999 100644--- a/spec/requests/embed_controller_spec.rb+++ b/spec/requests/embed_controller_spec.rb@@ -480,4 +480,68 @@ RSpec.describe EmbedController doendendend++ describe "#count" do+ fab!(:embeddable_host)++ it "returns counts for public topics" do+ topic_embed = Fabricate(:topic_embed, embed_url: "http://eviltrout.com/public-article")++ get "/embed/count.json", params: { embed_url: ["http://eviltrout.com/public-article"] }++ expect(response.status).to eq(200)+ expect(response.parsed_body["counts"]).to have_key("http://eviltrout.com/public-article")+ end++ it "does not return counts for topics in restricted categories" do+ restricted_category = Fabricate(:category)+ restricted_category.set_permissions(staff: :full)+ restricted_category.save!++ restricted_topic = Fabricate(:topic, category: restricted_category, posts_count: 5)+ Fabricate(+ :topic_embed,+ post: Fabricate(:post, topic: restricted_topic),+ topic: restricted_topic,+ embed_url: "http://eviltrout.com/private-article",+ )++ get "/embed/count.json", params: { embed_url: ["http://eviltrout.com/private-article"] }++ expect(response.status).to eq(200)+ expect(response.parsed_body["counts"]).not_to have_key("http://eviltrout.com/private-article")+ end++ it "returns counts only for visible topics when both public and restricted are requested" do+ public_topic = Fabricate(:topic, posts_count: 3)+ Fabricate(+ :topic_embed,+ post: Fabricate(:post, topic: public_topic),+ topic: public_topic,+ embed_url: "http://eviltrout.com/public-post",+ )++ restricted_category = Fabricate(:category)+ restricted_category.set_permissions(staff: :full)+ restricted_category.save!++ restricted_topic = Fabricate(:topic, category: restricted_category, posts_count: 5)+ Fabricate(+ :topic_embed,+ post: Fabricate(:post, topic: restricted_topic),+ topic: restricted_topic,+ embed_url: "http://eviltrout.com/secret-post",+ )++ get "/embed/count.json",+ params: {+ embed_url: %w[http://eviltrout.com/public-post http://eviltrout.com/secret-post],+ }++ expect(response.status).to eq(200)+ counts = response.parsed_body["counts"]+ expect(counts).to have_key("http://eviltrout.com/public-post")+ expect(counts).not_to have_key("http://eviltrout.com/secret-post")+ end+ endend