A platform for community discussion. Free, open, simple.
FIX: Allow moderators to unhide posts using acting_user context (#36512)
Sam346e4cc28c3f
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rbindex 77eb41c6b47..c66897b83cc 100644--- a/app/controllers/posts_controller.rb+++ b/app/controllers/posts_controller.rb@@ -717,6 +717,7 @@ class PostsController < ApplicationControllerguardian.ensure_can_unhide!(post)+ post.acting_user = current_userpost.unhide!render body: nildiff --git a/app/models/concerns/reviewable_action_builder.rb b/app/models/concerns/reviewable_action_builder.rbindex f745e9fc013..a3275582321 100644--- a/app/models/concerns/reviewable_action_builder.rb+++ b/app/models/concerns/reviewable_action_builder.rb@@ -222,6 +222,7 @@ module ReviewableActionBuilderenddef perform_unhide_post(performed_by, _args)+ target_post.acting_user = performed_bytarget_post.unhide!create_result(:success, :approved, [created_by_id], false)enddiff --git a/app/models/reviewable_post.rb b/app/models/reviewable_post.rbindex 49f0dc37611..62049b40bf1 100644--- a/app/models/reviewable_post.rb+++ b/app/models/reviewable_post.rb@@ -100,6 +100,7 @@ class ReviewablePost < Reviewableenddef perform_approve_and_unhide(performed_by, _args)+ post.acting_user = performed_bypost.unhide!create_result(:success, :approved, [created_by_id], false)diff --git a/lib/post_action_destroyer.rb b/lib/post_action_destroyer.rbindex 0de7906cf46..a44d7e8f2ba 100644--- a/lib/post_action_destroyer.rb+++ b/lib/post_action_destroyer.rb@@ -53,7 +53,10 @@ class PostActionDestroyer).performed!post_action.remove_act!(@destroyed_by)- post_action.post.unhide! if post_action.staff_took_action+ if post_action.staff_took_action+ post_action.post.acting_user = @destroyed_by+ post_action.post.unhide!+ endif @post_action_type_id == post_action_type_view.types[:like]GivenDailyLike.decrement_for(@destroyed_by.id)enddiff --git a/plugins/discourse-ai/app/models/reviewable_ai_post.rb b/plugins/discourse-ai/app/models/reviewable_ai_post.rbindex a89744e29df..19019b7d935 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_post.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_post.rb@@ -105,7 +105,10 @@ class ReviewableAiPost < Reviewabledef perform_disagree(performed_by, args)# Undo hide/silence if applicable- post.unhide! if post.hidden?+ if post.hidden?+ post.acting_user = performed_by+ post.unhide!+ endcreate_result(:success, :rejected) do |result|result.update_flag_stats = { status: :disagreed, user_ids: [created_by_id] }diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rbindex 329a4c7b81c..13097b21647 100644--- a/spec/models/post_spec.rb+++ b/spec/models/post_spec.rb@@ -1656,6 +1656,52 @@ RSpec.describe Post do)end+ it "allows staff to unhide posts containing media added by staff even if the author cannot embed" do+ SiteSetting.embedded_media_post_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]++ moderator = Fabricate(:moderator, refresh_auto_groups: true)+ low_trust_user = Fabricate(:user, trust_level: TrustLevel[0], refresh_auto_groups: true)++ post =+ create_post(+ user: low_trust_user,+ topic: Fabricate(:topic, user: low_trust_user),+ raw: "original content",+ )++ post.hide!(PostActionType.types[:off_topic])++ post.revise(moderator, raw: "updated with media ")++ post.reload++ post.acting_user = moderator+ expect { post.unhide! }.not_to raise_error+ expect(post.reload.hidden).to eq(false)+ end++ it "prevents unhiding posts with embedded media when author lacks permission" do+ SiteSetting.embedded_media_post_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]++ moderator = Fabricate(:moderator, refresh_auto_groups: true)+ low_trust_user = Fabricate(:user, trust_level: TrustLevel[0], refresh_auto_groups: true)++ post =+ create_post(+ user: low_trust_user,+ topic: Fabricate(:topic, user: low_trust_user),+ raw: "original content",+ )++ post.hide!(PostActionType.types[:off_topic])++ post.revise(moderator, raw: "updated with media ")++ post = Post.find(post.id)++ expect { post.unhide! }.to raise_error(ActiveRecord::RecordInvalid)+ end+context "in a topic with multiple replies" dolet!(:second_last_reply) dofreeze_time 1.day.from_now