diff --git a/app/controllers/api/v0/comments_controller.rb b/app/controllers/api/v0/comments_controller.rb index b25283497..264cd41d4 100644 --- a/app/controllers/api/v0/comments_controller.rb +++ b/app/controllers/api/v0/comments_controller.rb @@ -9,14 +9,14 @@ module Api private_constant :ATTRIBUTES_FOR_SERIALIZATION def index - article = Article.find(params[:a_id]) + commentable = params[:a_id] ? Article.find(params[:a_id]) : PodcastEpisode.find(params[:p_id]) - @comments = article.comments + @comments = commentable.comments .includes(:user) .select(ATTRIBUTES_FOR_SERIALIZATION) .arrange - set_surrogate_key_header article.record_key, Comment.table_key, edge_cache_keys(@comments) + set_surrogate_key_header commentable.record_key, Comment.table_key, edge_cache_keys(@comments) end def show diff --git a/config/fastly/snippets/safe_params_list.vcl b/config/fastly/snippets/safe_params_list.vcl index 1af167044..bbc24a957 100644 --- a/config/fastly/snippets/safe_params_list.vcl +++ b/config/fastly/snippets/safe_params_list.vcl @@ -2,6 +2,6 @@ import querystring; sub vcl_recv { # return this URL with only the parameters that match this regular expression if (req.url !~ "/admin/" && req.url !~ "/search/" && req.url !~ "/bulk_show") { - set req.url = querystring.regfilter_except(req.url, "^(a_id|args|article_id|article_ids|articles|asc|callback_url|category|chat_channel_id|client_id|code|collection_id|commentable_id|commentable_type|confirmation_token|created_at|end|filter|followable_id|followable_type|fork_id|i|key|message_offset|name|oauth_token|oauth_verifier|offset|org_id|organization_id|p|page|per_page|prefill|preview|purchaser|reactable_ids|redirect_uri|reported_url|reporter_username|response_type|scope|search|signature|sort|source_id|source_type|start|state|status|tag|tag_list|top|type_of|url|username|invitation_token|reset_password_token|ut|verb|invitation_slug)$"); + set req.url = querystring.regfilter_except(req.url, "^(a_id|args|article_id|article_ids|articles|asc|callback_url|category|chat_channel_id|client_id|code|collection_id|commentable_id|commentable_type|confirmation_token|created_at|end|filter|followable_id|followable_type|fork_id|i|key|message_offset|name|oauth_token|oauth_verifier|offset|org_id|organization_id|p|page|per_page|p_id|prefill|preview|purchaser|reactable_ids|redirect_uri|reported_url|reporter_username|response_type|scope|search|signature|sort|source_id|source_type|start|state|status|tag|tag_list|top|type_of|url|username|invitation_token|reset_password_token|ut|verb|invitation_slug)$"); } } diff --git a/docs/api_v0.yml b/docs/api_v0.yml index 5d6c20f86..e8c44d572 100644 --- a/docs/api_v0.yml +++ b/docs/api_v0.yml @@ -2221,7 +2221,7 @@ paths: summary: Comments description: | This endpoint allows the client to retrieve all comments belonging to an - article as threaded conversations. + article or podcast episode as threaded conversations. It will return the all top level comments with their nested comments as threads. See the format specification for further details. @@ -2236,6 +2236,14 @@ paths: format: int32 minimum: 1 example: 270180 + - name: p_id + in: query + description: Podcast Episode identifier. + schema: + type: integer + format: int32 + minimum: 1 + example: 124 responses: "200": description: A list of threads of comments @@ -2266,6 +2274,10 @@ paths: label: curl (all comments of an article) source: | curl https://dev.to/api/comments?a_id=270180 + - lang: Shell + label: curl (all comments of a podcast episode) + source: | + curl https://dev.to/api/comments?p_id=124 /comments/{id}: get: diff --git a/spec/requests/api/v0/comments_spec.rb b/spec/requests/api/v0/comments_spec.rb index d07caf448..6e63ba8fe 100644 --- a/spec/requests/api/v0/comments_spec.rb +++ b/spec/requests/api/v0/comments_spec.rb @@ -156,6 +156,22 @@ RSpec.describe "Api::V0::Comments", type: :request do expect(find_child_comment(response)["children"]).not_to be_empty end end + + context "when getting by podcast episode id" do + let(:podcast) { create(:podcast) } + let(:podcast_episode) { create(:podcast_episode, podcast: podcast) } + let!(:comment) { create(:comment, commentable: podcast_episode) } + + it "not found if bad podcast episode id" do + get api_comments_path(p_id: "asdfghjkl") + expect(response).to have_http_status(:not_found) + end + it "returns comment if good podcast episode id" do + get api_comments_path(p_id: podcast_episode.id) + expect(response).to have_http_status(:ok) + expect(response.parsed_body.size).to eq(1) + end + end end describe "GET /api/comments/:id" do