2021-08-19 08:50:24 -07:00
|
|
|
require 'use_case/user/ban'
|
|
|
|
require 'use_case/user/unban'
|
|
|
|
require 'errors'
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::ModerationController < AjaxController
|
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
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".error")
|
2014-12-28 15:21:06 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".success")
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:success] = true
|
2014-12-28 15:21:06 -08:00
|
|
|
end
|
2014-12-28 15:50:14 -08:00
|
|
|
|
2015-04-22 17:56:29 -07:00
|
|
|
def ban
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".error")
|
2015-04-22 17:56:29 -07:00
|
|
|
|
|
|
|
params.require :user
|
|
|
|
params.require :ban
|
|
|
|
|
2021-08-19 08:50:24 -07:00
|
|
|
duration = params[:duration].to_i
|
2021-08-14 09:04:58 -07:00
|
|
|
duration_unit = params[:duration_unit].to_s
|
|
|
|
reason = params[:reason].to_s
|
2021-08-19 08:50:24 -07:00
|
|
|
target_user = User.find_by_screen_name!(params[:user])
|
2021-08-14 09:04:58 -07:00
|
|
|
unban = params[:ban] == '0'
|
2021-12-30 12:06:21 -08:00
|
|
|
perma = params[:duration].blank?
|
2015-04-22 17:56:29 -07:00
|
|
|
|
2021-08-19 08:50:24 -07:00
|
|
|
if !unban && target_user.has_role?(:administrator)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :nopriv
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".nopriv")
|
2015-04-22 18:01:48 -07:00
|
|
|
return
|
2015-04-22 17:56:29 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if unban
|
2021-08-19 08:50:24 -07:00
|
|
|
UseCase::User::Unban.call(target_user.id)
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".success.unban")
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:success] = true
|
2021-08-19 08:50:24 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
return
|
2015-04-22 17:56:29 -07:00
|
|
|
elsif perma
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".success.permanent")
|
2021-08-19 08:50:24 -07:00
|
|
|
expiry = nil
|
2015-04-22 17:56:29 -07:00
|
|
|
else
|
2021-08-19 08:50:24 -07:00
|
|
|
params.require :duration
|
|
|
|
params.require :duration_unit
|
|
|
|
|
|
|
|
raise Errors::InvalidBanDuration unless %w[hours days weeks months].include? duration_unit
|
|
|
|
|
|
|
|
expiry = DateTime.now + duration.public_send(duration_unit)
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = I18n.t(".success.temporary", date: expiry.to_s)
|
2015-04-22 17:56:29 -07:00
|
|
|
end
|
2021-08-19 08:50:24 -07:00
|
|
|
|
|
|
|
UseCase::User::Ban.call(
|
|
|
|
target_user_id: target_user.id,
|
|
|
|
expiry: expiry,
|
|
|
|
reason: reason,
|
|
|
|
source_user_id: current_user.id)
|
|
|
|
|
|
|
|
target_user.save!
|
2015-04-22 17:56:29 -07:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
2021-08-19 08:50:24 -07:00
|
|
|
@response[:success] = true
|
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
|
|
|
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".error")
|
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
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".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!
|
|
|
|
|
2022-07-06 02:06:23 -07:00
|
|
|
@response[:message] = t(".success", 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
|