[Replacements] Allow janitor+ to auto approve submitted replacements (#650)

This commit is contained in:
Tarrgon 2024-07-25 13:26:05 -04:00 committed by Cinder
parent 8edb1b9226
commit 1425332c0b
4 changed files with 77 additions and 6 deletions

View File

@ -27,6 +27,19 @@ class PostReplacementsController < ApplicationController
if @post_replacement.errors.none?
flash[:notice] = "Post replacement submitted"
end
if CurrentUser.can_approve_posts? && !@post_replacement.upload_as_pending?
if @post_replacement.errors.any?
respond_to do |format|
format.json do
return render json: { success: false, message: @post_replacement.errors.full_messages.join("; ") }, status: 412
end
end
end
@post_replacement.approve!(penalize_current_uploader: CurrentUser.id != @post.uploader_id)
end
respond_to do |format|
format.json do
return render json: { success: false, message: @post_replacement.errors.full_messages.join("; ") }, status: 412 if @post_replacement.errors.any?
@ -92,7 +105,7 @@ class PostReplacementsController < ApplicationController
end
def create_params
params.require(:post_replacement).permit(:replacement_url, :replacement_file, :reason, :source)
params.require(:post_replacement).permit(:replacement_url, :replacement_file, :reason, :source, :as_pending)
end
def ensure_uploads_enabled

View File

@ -19,6 +19,12 @@
<span class="hint">Tell us why this file should replace the original.</span>
</div>
<div class="input" v-if="canApprove">
<label class="section-label"><input type="checkbox" id="as_pending" v-model="uploadAsPending"/>
Upload as pending
</label>
</div>
<div class="sect_red error_message" v-if="showErrors && errorMessage !== undefined">
{{ errorMessage }}
</div>
@ -35,6 +41,7 @@ import autocompletableInput from "./autocompletable_input.vue";
import filePreview from "./uploader/file_preview.vue";
import fileInput from "./uploader/file_input.vue";
import sources from "./uploader/sources.vue";
import Utility from "./utility";
export default {
components: {
@ -57,6 +64,8 @@ export default {
sourceWarning: false,
submitting: false,
submittedReason: undefined,
canApprove: Utility.meta("current-user-can-approve-posts") === "true",
uploadAsPending: false,
};
},
mounted() {
@ -87,6 +96,7 @@ export default {
}
formData.append("post_replacement[source]", this.sources[0]);
formData.append("post_replacement[reason]", this.reason);
formData.append("post_replacement[as_pending]", this.uploadAsPending);
this.submittedReason = this.reason;

View File

@ -6,7 +6,7 @@ class PostReplacement < ApplicationRecord
belongs_to :creator, class_name: "User"
belongs_to :approver, class_name: "User", optional: true
belongs_to :uploader_on_approve, class_name: "User", foreign_key: :uploader_id_on_approve, optional: true
attr_accessor :replacement_file, :replacement_url, :tags, :is_backup, :is_destroyed_reupload
attr_accessor :replacement_file, :replacement_url, :tags, :is_backup, :as_pending, :is_destroyed_reupload
validate :user_is_not_limited, on: :create
validate :post_is_valid, on: :create
@ -314,6 +314,10 @@ class PostReplacement < ApplicationRecord
user.is_janitor?
end
def upload_as_pending?
as_pending.to_s.truthy?
end
include ApiMethods
include StorageMethods
include FileMethods

View File

@ -6,6 +6,7 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest
context "The post replacements controller" do
setup do
@user = create(:moderator_user, can_approve_posts: true, created_at: 1.month.ago)
@regular_user = create(:member_user, replacements_beta: true, created_at: 1.month.ago)
as(@user) do
@upload = UploadService.new(attributes_for(:jpg_upload).merge({ uploader: @user })).start!
@post = @upload.post
@ -21,8 +22,9 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest
post_id: @post.id,
post_replacement: {
replacement_file: file,
reason: 'test replacement'
}
reason: "test replacement",
as_pending: true,
},
}
assert_difference(-> { @post.replacements.size }) do
@ -33,6 +35,48 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest
assert_equal @response.parsed_body["location"], post_path(@post)
end
context "with as_pending false" do
should "immediately approve a replacement" do
file = fixture_file_upload("alpha.png")
params = {
format: :json,
post_id: @post.id,
post_replacement: {
replacement_file: file,
reason: "test replacement",
as_pending: false,
},
}
post_auth post_replacements_path, @user, params: params
@post.reload
# 200be2be97a465ecd2054a51522f65b5 is the md5 of alpha.png
assert_equal "200be2be97a465ecd2054a51522f65b5", @post.md5
assert_equal @response.parsed_body["location"], post_path(@post)
end
should "always upload as pending if user can't approve posts" do
file = fixture_file_upload("test.gif")
params = {
format: :json,
post_id: @post.id,
post_replacement: {
replacement_file: file,
reason: "test replacement",
as_pending: false,
},
}
post_auth post_replacements_path, @regular_user, params: params
@post.reload
# 1e2edf6bdbd971d8c3cc4da0f98f38ab is the md5 of test.gif
assert_not_equal "1e2edf6bdbd971d8c3cc4da0f98f38ab", @post.md5
assert_equal @response.parsed_body["location"], post_path(@post)
end
end
context "with a previously destroyed post" do
setup do
@admin = create(:admin_user)
@ -104,7 +148,7 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest
put_auth toggle_penalize_post_replacement_path(@replacement), @user
assert_redirected_to post_replacement_path(@replacement)
@replacement.reload
assert !@replacement.penalize_uploader_on_approve
assert_not @replacement.penalize_uploader_on_approve
end
end
@ -117,7 +161,7 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest
context "new action" do
should "render" do
get_auth new_post_replacement_path, @user, params: {post_id: @post.id}
get_auth new_post_replacement_path, @user, params: { post_id: @post.id }
assert_response :success
end
end