Skip to content

A platform for community discussion. Free, open, simple.

FIX: Filter hidden posts from raw topic markdown endpoint (#38237)

Loïc Guitautc11c6c3ba22f
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index ed96ef7a713..6369ec52a94 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -35,28 +35,9 @@ class PostsController < ApplicationController
end
def markdown_num
- if params[:revision].present?
- post_revision = find_post_revision_from_topic_id
- render plain: post_revision.modifications[:raw].last
- elsif params[:post_number].present?
- markdown Post.find_by(
- topic_id: params[:topic_id].to_i,
- post_number: params[:post_number].to_i,
- )
- else
- opts = params.slice(:page)
- opts[:limit] = MARKDOWN_TOPIC_PAGE_SIZE
- topic_view = TopicView.new(params[:topic_id], current_user, opts)
- content = topic_view.posts.map { |p| <<~MD }
- #{p.user.username} | #{p.updated_at} | ##{p.post_number}
-
- #{p.raw}
-
- -------------------------
-
- MD
- render plain: content.join
- end
+ return render plain: markdown_for_revision if params[:revision].present?
+ return markdown_for_post if params[:post_number].present?
+ render plain: markdown_for_topic
end
def latest
@@ -839,6 +820,33 @@ class PostsController < ApplicationController
private
+ def markdown_for_revision
+ find_post_revision_from_topic_id.modifications[:raw].last
+ end
+
+ def markdown_for_post
+ post = Post.find_by(topic_id: params[:topic_id].to_i, post_number: params[:post_number].to_i)
+ markdown(post)
+ end
+
+ def markdown_for_topic
+ topic_view =
+ TopicView.new(
+ params[:topic_id],
+ current_user,
+ page: params[:page],
+ limit: MARKDOWN_TOPIC_PAGE_SIZE,
+ )
+ topic_view.posts.select { |post| guardian.can_see?(post) }.map { |post| <<~MD }.join
+ #{post.user.username} | #{post.updated_at} | ##{post.post_number}
+
+ #{post.raw}
+
+ -------------------------
+
+ MD
+ end
+
def user_posts(guardian, user_id, opts)
# Topic.unscoped is necessary to remove the default deleted_at: nil scope
posts =
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 5b62ce6b922..3d1cd29d294 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -2940,6 +2940,95 @@ RSpec.describe PostsController do
expect(response.status).to eq(200)
expect(response.body).to include("123456789", "abcdefghij")
end
+
+ context "with a hidden post" do
+ fab!(:topic)
+ fab!(:visible_post) do
+ Fabricate(:post, topic: topic, post_number: 1, raw: "visible post content")
+ end
+ fab!(:hidden_post) do
+ Fabricate(:post, topic: topic, post_number: 2, raw: "hidden post content", hidden: true)
+ end
+ fab!(:post_author) { hidden_post.user }
+
+ before { SiteSetting.hidden_post_visible_groups = Group::AUTO_GROUPS[:trust_level_4] }
+
+ context "when fetching a single hidden post" do
+ context "when logged out" do
+ it "returns not found" do
+ get "/raw/#{topic.id}/#{hidden_post.post_number}"
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context "when logged in as a regular user" do
+ before { sign_in(user) }
+
+ it "returns not found" do
+ get "/raw/#{topic.id}/#{hidden_post.post_number}"
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context "when logged in as the post author" do
+ before { sign_in(post_author) }
+
+ it "returns the hidden post content" do
+ get "/raw/#{topic.id}/#{hidden_post.post_number}"
+ expect(response.body).to eq("hidden post content")
+ end
+ end
+
+ context "when logged in as a moderator" do
+ before { sign_in(moderator) }
+
+ it "returns the hidden post content" do
+ get "/raw/#{topic.id}/#{hidden_post.post_number}"
+ expect(response.body).to eq("hidden post content")
+ end
+ end
+ end
+
+ context "when fetching the whole topic" do
+ context "when logged out" do
+ it "excludes hidden post content" do
+ get "/raw/#{topic.id}"
+ expect(response.body).to include("visible post content").and exclude(
+ "hidden post content",
+ )
+ end
+ end
+
+ context "when logged in as a regular user" do
+ before { sign_in(user) }
+
+ it "excludes hidden post content" do
+ get "/raw/#{topic.id}"
+ expect(response.body).to include("visible post content").and exclude(
+ "hidden post content",
+ )
+ end
+ end
+
+ context "when logged in as the post author" do
+ before { sign_in(post_author) }
+
+ it "includes hidden post content" do
+ get "/raw/#{topic.id}"
+ expect(response.body).to include("visible post content", "hidden post content")
+ end
+ end
+
+ context "when logged in as a moderator" do
+ before { sign_in(moderator) }
+
+ it "includes hidden post content" do
+ get "/raw/#{topic.id}"
+ expect(response.body).to include("visible post content", "hidden post content")
+ end
+ end
+ end
+ end
end
describe "#short_link" do
diff --git a/spec/support/negated_matcher.rb b/spec/support/negated_matcher.rb
index 8ef4694982d..cac5013f625 100644
--- a/spec/support/negated_matcher.rb
+++ b/spec/support/negated_matcher.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
RSpec::Matchers.define_negated_matcher :not_change, :change
+RSpec::Matchers.define_negated_matcher :exclude, :include