2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-14 19:49:57 -04:00
|
|
|
class PostReplacementsController < ApplicationController
|
2021-06-26 12:25:25 -04:00
|
|
|
respond_to :html, :json
|
2022-01-30 07:33:14 -05:00
|
|
|
before_action :member_only, only: [:create, :new]
|
2023-03-23 14:20:44 -04:00
|
|
|
before_action :approver_only, only: [:approve, :reject, :promote, :toggle_penalize]
|
2023-03-23 15:17:12 -04:00
|
|
|
before_action :admin_only, only: [:destroy]
|
2022-12-27 16:28:06 -05:00
|
|
|
before_action :ensure_uploads_enabled, only: [:new, :create]
|
2022-01-30 07:33:14 -05:00
|
|
|
|
2021-03-27 23:11:25 -04:00
|
|
|
content_security_policy only: [:new] do |p|
|
2022-04-20 15:21:11 -04:00
|
|
|
p.img_src :self, :data, :blob, "*"
|
2022-05-22 16:18:03 -04:00
|
|
|
p.media_src :self, :data, :blob, "*"
|
2021-03-27 23:11:25 -04:00
|
|
|
end
|
2017-05-14 19:49:57 -04:00
|
|
|
|
2017-05-14 19:52:34 -04:00
|
|
|
def new
|
2022-01-30 07:33:14 -05:00
|
|
|
check_allow_create
|
2021-04-25 22:36:38 -04:00
|
|
|
@post = Post.find(params[:post_id])
|
|
|
|
@post_replacement = @post.replacements.new
|
2018-09-26 18:17:46 -04:00
|
|
|
respond_with(@post_replacement)
|
2017-05-14 19:52:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2022-01-30 07:33:14 -05:00
|
|
|
check_allow_create
|
2017-05-14 19:52:34 -04:00
|
|
|
@post = Post.find(params[:post_id])
|
2020-05-10 18:07:43 -04:00
|
|
|
@post_replacement = @post.replacements.create(create_params.merge(creator_id: CurrentUser.id, creator_ip_addr: CurrentUser.ip_addr))
|
2024-07-13 20:05:50 -04:00
|
|
|
@post_replacement.notify_reupload
|
2022-08-20 11:13:36 -04:00
|
|
|
if @post_replacement.errors.none?
|
2021-06-26 09:05:44 -04:00
|
|
|
flash[:notice] = "Post replacement submitted"
|
2020-06-11 20:55:37 -04:00
|
|
|
end
|
2024-07-25 13:26:05 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-08-20 11:13:36 -04:00
|
|
|
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?
|
|
|
|
|
|
|
|
render json: { success: true, location: post_path(@post) }
|
|
|
|
end
|
|
|
|
end
|
2017-05-14 19:52:34 -04:00
|
|
|
end
|
|
|
|
|
2020-05-10 18:07:43 -04:00
|
|
|
def approve
|
2017-12-18 18:57:05 -05:00
|
|
|
@post_replacement = PostReplacement.find(params[:id])
|
2021-06-26 07:01:26 -04:00
|
|
|
@post_replacement.approve!(penalize_current_uploader: params[:penalize_current_uploader])
|
2021-06-26 12:25:25 -04:00
|
|
|
|
2021-06-26 18:21:01 -04:00
|
|
|
respond_with(@post_replacement, location: post_path(@post_replacement.post))
|
2020-05-10 18:07:43 -04:00
|
|
|
end
|
|
|
|
|
2021-06-26 08:01:07 -04:00
|
|
|
def toggle_penalize
|
|
|
|
@post_replacement = PostReplacement.find(params[:id])
|
|
|
|
@post_replacement.toggle_penalize!
|
2021-06-26 12:25:25 -04:00
|
|
|
|
2021-06-26 08:01:07 -04:00
|
|
|
respond_with(@post_replacement)
|
|
|
|
end
|
|
|
|
|
2020-05-10 18:07:43 -04:00
|
|
|
def reject
|
|
|
|
@post_replacement = PostReplacement.find(params[:id])
|
|
|
|
@post_replacement.reject!
|
|
|
|
|
2021-06-26 12:25:25 -04:00
|
|
|
respond_with(@post_replacement, location: post_path(@post_replacement.post))
|
2020-05-10 18:07:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@post_replacement = PostReplacement.find(params[:id])
|
|
|
|
@post_replacement.destroy
|
2017-12-18 18:57:05 -05:00
|
|
|
|
2021-06-26 18:21:01 -04:00
|
|
|
respond_with(@post_replacement, location: post_path(@post_replacement.post))
|
2017-12-18 18:57:05 -05:00
|
|
|
end
|
|
|
|
|
2020-06-11 20:55:37 -04:00
|
|
|
def promote
|
|
|
|
@post_replacement = PostReplacement.find(params[:id])
|
2021-06-26 09:05:44 -04:00
|
|
|
@upload = @post_replacement.promote!
|
|
|
|
if @post_replacement.errors.any?
|
2021-06-26 12:25:25 -04:00
|
|
|
respond_with(@post_replacement)
|
2021-06-26 09:05:44 -04:00
|
|
|
elsif @upload.errors.any?
|
|
|
|
respond_with(@upload)
|
2020-11-20 14:21:37 -05:00
|
|
|
else
|
2021-06-26 18:21:01 -04:00
|
|
|
respond_with(@upload.post)
|
2020-11-20 14:21:37 -05:00
|
|
|
end
|
2020-06-11 20:55:37 -04:00
|
|
|
end
|
|
|
|
|
2017-05-14 19:49:57 -04:00
|
|
|
def index
|
2022-01-30 07:33:14 -05:00
|
|
|
params[:search][:post_id] = params.delete(:post_id) if params.key?(:post_id)
|
2021-06-25 11:34:16 -04:00
|
|
|
@post_replacements = PostReplacement.includes(:post).visible(CurrentUser.user).search(search_params).paginate(params[:page], limit: params[:limit])
|
2017-05-14 19:49:57 -04:00
|
|
|
|
|
|
|
respond_with(@post_replacements)
|
|
|
|
end
|
2017-05-14 19:52:34 -04:00
|
|
|
|
2022-01-30 07:33:14 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def check_allow_create
|
|
|
|
return if CurrentUser.can_replace?
|
|
|
|
|
|
|
|
raise User::PrivilegeError, "You are not part of the replacements beta"
|
|
|
|
end
|
|
|
|
|
2017-05-14 19:52:34 -04:00
|
|
|
def create_params
|
2024-07-25 13:26:05 -04:00
|
|
|
params.require(:post_replacement).permit(:replacement_url, :replacement_file, :reason, :source, :as_pending)
|
2017-12-18 18:57:05 -05:00
|
|
|
end
|
2022-12-27 16:28:06 -05:00
|
|
|
|
|
|
|
def ensure_uploads_enabled
|
2024-12-08 22:28:31 -05:00
|
|
|
access_denied if Security::Lockdown.uploads_disabled? || CurrentUser.user.level < Security::Lockdown.uploads_min_level
|
2022-12-27 16:28:06 -05:00
|
|
|
end
|
2017-05-14 19:49:57 -04:00
|
|
|
end
|