2020-04-28 11:27:59 -07:00
|
|
|
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])
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :fail
|
|
|
|
@response[:message] = I18n.t('messages.moderation.vote.fail')
|
2014-12-28 14:26:16 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@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
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :fail
|
|
|
|
@response[:message] = I18n.t('messages.moderation.destroy_vote.fail')
|
2014-12-28 14:26:16 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@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
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :fail
|
|
|
|
@response[:message] = I18n.t('messages.moderation.destroy_report.fail')
|
2014-12-28 15:21:06 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@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])
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :rec_inv
|
|
|
|
@response[:message] = I18n.t('messages.moderation.create_comment.rec_inv')
|
2014-12-28 15:50:14 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@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
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2014-12-28 16:05:30 -08:00
|
|
|
comment = ModerationComment.find(params[:comment])
|
|
|
|
|
|
|
|
unless current_user == comment.user
|
2020-04-28 11:27:59 -07:00
|
|
|
@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
|
|
|
|
2020-04-28 11:27:59 -07: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
|
|
|
|
2015-04-22 17:56:29 -07:00
|
|
|
def ban
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
|
|
|
@response[:message] = I18n.t('messages.moderation.ban.error')
|
2015-04-22 17:56:29 -07:00
|
|
|
|
|
|
|
params.require :user
|
|
|
|
params.require :ban
|
|
|
|
params.require :permaban
|
2021-08-14 09:04:58 -07:00
|
|
|
params.require :duration
|
|
|
|
params.require :duration_unit
|
2015-04-22 17:56:29 -07:00
|
|
|
|
2021-08-14 09:04:58 -07:00
|
|
|
duration = params[:duration].to_s
|
|
|
|
duration_unit = params[:duration_unit].to_s
|
|
|
|
reason = params[:reason].to_s
|
2020-05-01 13:41:26 -07:00
|
|
|
target = User.find_by_screen_name!(params[:user])
|
2021-08-14 09:04:58 -07:00
|
|
|
unban = params[:ban] == '0'
|
|
|
|
perma = params[:permaban] == '1'
|
2015-04-22 17:56:29 -07:00
|
|
|
|
2020-04-19 13:35:58 -07:00
|
|
|
if !unban && target.has_role?(:administrator)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :nopriv
|
|
|
|
@response[:message] = I18n.t('messages.moderation.ban.nopriv')
|
2015-04-22 18:01:48 -07:00
|
|
|
return
|
2015-04-22 17:56:29 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if unban
|
|
|
|
target.unban
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:message] = I18n.t('messages.moderation.ban.unban')
|
|
|
|
@response[:success] = true
|
2015-04-22 17:56:29 -07:00
|
|
|
elsif perma
|
2021-08-14 09:04:58 -07:00
|
|
|
target.ban nil, nil, reason, current_user
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:message] = I18n.t('messages.moderation.ban.perma')
|
2015-04-22 17:56:29 -07:00
|
|
|
else
|
2021-08-14 09:04:58 -07:00
|
|
|
target.ban duration, duration_unit, reason, current_user
|
|
|
|
@response[:message] = "User banned for #{duration} #{duration_unit}"
|
2015-04-22 17:56:29 -07:00
|
|
|
end
|
|
|
|
target.save!
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = target.banned? == !unban
|
2015-04-22 17:56:29 -07:00
|
|
|
end
|
|
|
|
|
2015-02-03 08:48:30 -08:00
|
|
|
def privilege
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2015-02-03 08:48:30 -08:00
|
|
|
|
|
|
|
params.require :user
|
|
|
|
params.require :type
|
|
|
|
params.require :status
|
|
|
|
|
|
|
|
status = params[:status] == 'true'
|
|
|
|
|
2020-05-01 13:41:26 -07:00
|
|
|
target_user = User.find_by_screen_name!(params[:user])
|
2015-02-03 08:48:30 -08:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:message] = I18n.t('messages.moderation.privilege.nope')
|
2020-04-25 13:20:02 -07:00
|
|
|
return unless %w(moderator admin).include? params[:type].downcase
|
2015-02-03 08:48:30 -08:00
|
|
|
|
2020-04-25 14:23:37 -07:00
|
|
|
unless current_user.has_role?(:administrator)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :nopriv
|
|
|
|
@response[:message] = I18n.t('messages.moderation.privilege.nopriv')
|
2015-02-03 08:48:30 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:checked] = status
|
2020-04-19 13:35:58 -07:00
|
|
|
type = params[:type].downcase
|
2021-08-14 09:04:58 -07:00
|
|
|
target_role = {'admin' => 'administrator'}.fetch(type, type).to_sym
|
2020-04-25 13:20:02 -07:00
|
|
|
|
2020-04-25 14:23:37 -07:00
|
|
|
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!
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:message] = I18n.t('messages.moderation.privilege.checked', privilege: params[:type])
|
2015-02-03 08:48:30 -08:00
|
|
|
|
2020-04-28 11:27:59 -07: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
|