Retrospring/app/controllers/ajax/moderation_controller.rb

180 lines
4.7 KiB
Ruby
Raw Normal View History

class Ajax::ModerationController < AjaxController
2014-12-28 14:26:16 -08:00
def vote
params.require :id
params.require :upvote
report = Report.find(params[:id])
begin
current_user.report_vote(report, params[:upvote])
rescue => e
2021-12-28 09:32:03 -08:00
Sentry.capture_exception(e)
@response[:status] = :fail
@response[:message] = I18n.t('messages.moderation.vote.fail')
2014-12-28 14:26:16 -08:00
return
end
@response[:count] = report.votes
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.vote.okay')
@response[:success] = true
2014-12-28 14:26:16 -08:00
end
def destroy_vote
params.require :id
report = Report.find(params[:id])
begin
current_user.report_unvote report
rescue => e
2021-12-28 09:32:03 -08:00
Sentry.capture_exception(e)
@response[:status] = :fail
@response[:message] = I18n.t('messages.moderation.destroy_vote.fail')
2014-12-28 14:26:16 -08:00
return
end
@response[:count] = report.votes
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.destroy_vote.okay')
@response[:success] = true
2014-12-28 14:26:16 -08:00
end
2014-12-28 15:21:06 -08:00
def destroy_report
params.require :id
report = Report.find(params[:id])
begin
report.deleted = true
report.save
rescue => e
2021-12-28 09:32:03 -08:00
Sentry.capture_exception(e)
@response[:status] = :fail
@response[:message] = I18n.t('messages.moderation.destroy_report.fail')
2014-12-28 15:21:06 -08:00
return
end
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.destroy_report.okay')
@response[:success] = true
2014-12-28 15:21:06 -08:00
end
2014-12-28 15:50:14 -08:00
def create_comment
params.require :id
params.require :comment
report = Report.find(params[:id])
begin
current_user.report_comment(report, params[:comment])
rescue ActiveRecord::RecordInvalid => e
2021-12-28 09:32:03 -08:00
Sentry.capture_exception(e)
@response[:status] = :rec_inv
@response[:message] = I18n.t('messages.moderation.create_comment.rec_inv')
2014-12-28 15:50:14 -08:00
return
end
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.create_comment.okay')
@response[:success] = true
@response[:render] = render_to_string(partial: 'moderation/discussion', locals: { report: report })
@response[:count] = report.moderation_comments.all.count
2014-12-28 15:50:14 -08:00
end
def destroy_comment
2014-12-28 16:05:30 -08:00
params.require :comment
@response[:status] = :err
2014-12-28 16:05:30 -08:00
comment = ModerationComment.find(params[:comment])
unless current_user == comment.user
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.moderation.destroy_comment.nopriv')
2014-12-28 16:05:30 -08:00
return
end
comment.destroy
2014-12-28 15:50:14 -08:00
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.destroy_comment.okay')
@response[:success] = true
2014-12-28 15:50:14 -08:00
end
2015-02-03 08:48:30 -08:00
def ban
@response[:status] = :err
@response[:message] = I18n.t('messages.moderation.ban.error')
params.require :user
params.require :ban
params.require :permaban
reason = params[:reason]
target = User.find_by_screen_name!(params[:user])
unban = params[:ban] == "0"
perma = params[:permaban] == "1"
buntil = DateTime.strptime params[:until], "%m/%d/%Y %I:%M %p" unless unban || perma
if !unban && target.has_role?(:administrator)
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.moderation.ban.nopriv')
2015-04-22 18:01:48 -07:00
return
end
if unban
target.unban
@response[:message] = I18n.t('messages.moderation.ban.unban')
@response[:success] = true
elsif perma
target.ban nil, reason
@response[:message] = I18n.t('messages.moderation.ban.perma')
else
target.ban buntil, reason
@response[:message] = I18n.t('messages.moderation.ban.temp', date: buntil.to_s)
end
target.save!
@response[:status] = :okay
@response[:success] = target.banned? == !unban
end
2015-02-03 08:48:30 -08:00
def privilege
@response[:status] = :err
2015-02-03 08:48:30 -08:00
params.require :user
params.require :type
params.require :status
status = params[:status] == 'true'
target_user = User.find_by_screen_name!(params[:user])
2015-02-03 08:48:30 -08:00
@response[:message] = I18n.t('messages.moderation.privilege.nope')
return unless %w(moderator admin).include? params[:type].downcase
2015-02-03 08:48:30 -08:00
unless current_user.has_role?(:administrator)
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.moderation.privilege.nopriv')
2015-02-03 08:48:30 -08:00
return
end
@response[:checked] = status
type = params[:type].downcase
target_role = {"admin" => "administrator"}.fetch(type, type).to_sym
if status
target_user.add_role target_role
else
target_user.remove_role target_role
2015-09-01 06:18:56 -07:00
end
2015-02-03 08:48:30 -08:00
target_user.save!
@response[:message] = I18n.t('messages.moderation.privilege.checked', privilege: params[:type])
2015-02-03 08:48:30 -08:00
@response[:status] = :okay
@response[:success] = true
2015-02-03 08:48:30 -08:00
end
2014-12-28 14:26:16 -08:00
end