Update usages of bans

This commit is contained in:
Karina Kwiatek 2021-12-30 00:20:09 +01:00
parent ea62d91014
commit 6500d7ac71
5 changed files with 52 additions and 33 deletions

View File

@ -40,10 +40,11 @@ class ApplicationController < ActionController::Base
name = current_user.screen_name name = current_user.screen_name
# obligatory '2001: A Space Odyssey' reference # obligatory '2001: A Space Odyssey' reference
flash[:notice] = t('flash.ban.error', name: name) flash[:notice] = t('flash.ban.error', name: name)
unless current_user.ban_reason.nil? current_ban = current_user.bans.current.first
flash[:notice] += "\n#{t('flash.ban.reason', reason: current_user.ban_reason)}" unless current_ban&.reason.nil?
flash[:notice] += "\n#{t('flash.ban.reason', reason: current_user.bans.current.first.reason)}"
end end
if not current_user.permanently_banned? unless current_ban&.permanently_banned?
# TODO format banned_until # TODO format banned_until
flash[:notice] += "\n#{t('flash.ban.until', time: current_user.banned_until)}" flash[:notice] += "\n#{t('flash.ban.until', time: current_user.banned_until)}"
end end

View File

@ -237,7 +237,17 @@ class User < ApplicationRecord
# @param reason [String] Reason for the ban. This is displayed to the user. # @param reason [String] Reason for the ban. This is displayed to the user.
# @param banned_by [User] User who instated the ban # @param banned_by [User] User who instated the ban
def ban(duration, duration_unit = 'hours', reason = nil, banned_by = nil) def ban(duration, duration_unit = 'hours', reason = nil, banned_by = nil)
self.bans.create(expires_at: expiry, reason: reason, banned_by: banned_by) if duration
expiry = duration.public_send(duration_unit)
else
expiry = nil
end
UseCase::User::Ban.call(
target_user_id: id,
expiry: expiry,
reason: reason,
source_user_id: banned_by&.id
)
end end
def can_export? def can_export?

View File

@ -1,7 +1,7 @@
- current_ban = user.bans.current.first - current_ban = user.bans.current.first
.modal.fade#modal-ban{ aria: { hidden: true, labelledby: 'modal-ban-label' }, role: :dialog, tabindex: -1 } .modal.fade#modal-ban{ aria: { hidden: true, labelledby: 'modal-ban-label' }, role: :dialog, tabindex: -1 }
.modal-dialog .modal-dialog
.modal-content .modal-content#ban-control-super
.modal-header .modal-header
%h5.modal-title#modal-ban-label %h5.modal-title#modal-ban-label
= t 'views.modal.bancontrol.title' = t 'views.modal.bancontrol.title'
@ -11,7 +11,7 @@
= bootstrap_form_tag(url: '/mod/ban', html: { method: :post, novalidate: :novalidate }) do |f| = bootstrap_form_tag(url: '/mod/ban', html: { method: :post, novalidate: :novalidate }) do |f|
= f.hidden_field :user, value: user.screen_name = f.hidden_field :user, value: user.screen_name
- if current_ban.nil? - if current_ban.nil?
.modal-body#ban-control-super .modal-body
= f.check_box :ban, label: t('views.modal.bancontrol.ban'), checked: user.banned? = f.check_box :ban, label: t('views.modal.bancontrol.ban'), checked: user.banned?
#ban-controls{ style: user.banned? ? '' : 'display: none' } #ban-controls{ style: user.banned? ? '' : 'display: none' }
= f.check_box :permaban, label: t('views.modal.bancontrol.permanent'), checked: user.permanently_banned? = f.check_box :permaban, label: t('views.modal.bancontrol.permanent'), checked: user.permanently_banned?
@ -22,13 +22,13 @@
= f.radio_button :duration_unit, 'days', label: 'Days' = f.radio_button :duration_unit, 'days', label: 'Days'
= f.radio_button :duration_unit, 'weeks', label: 'Weeks' = f.radio_button :duration_unit, 'weeks', label: 'Weeks'
= f.radio_button :duration_unit, 'months', label: 'Months' = f.radio_button :duration_unit, 'months', label: 'Months'
= f.text_field :reason, placeholder: t('views.modal.bancontrol.reason'), value: user.ban_reason = f.text_field :reason, placeholder: t('views.modal.bancontrol.reason'), value: user.bans.current.first&.reason
.modal-footer .modal-footer
%button.btn.btn-default{ name: 'stop-time', type: :button, data: { dismiss: :modal } }= t 'views.actions.close' %button.btn.btn-default{ name: 'stop-time', type: :button, data: { dismiss: :modal } }= t 'views.actions.close'
= f.submit t('views.modal.bancontrol.hammertime'), class: 'btn btn-primary', name: 'hammer-time' = f.submit t('views.modal.bancontrol.hammertime'), class: 'btn btn-primary', name: 'hammer-time'
- else - else
= f.hidden_field :ban, value: '0' = f.hidden_field :ban, value: '0'
.modal-body#ban-control-super .modal-body
- if current_ban.expires_at.nil? - if current_ban.expires_at.nil?
This user is currently permanently banned for This user is currently permanently banned for
%strong= current_ban.reason %strong= current_ban.reason
@ -40,7 +40,7 @@
- if current_ban.banned_by.present? - if current_ban.banned_by.present?
%br %br
This ban was instated by This ban was instated by
%strong= current_ban.banned_by.safe_name %strong= current_ban.banned_by.profile.safe_name
on on
%strong= current_ban.created_at %strong= current_ban.created_at
.modal-footer .modal-footer

View File

@ -24,13 +24,15 @@ module UseCase
if reason == REASON_SPAM if reason == REASON_SPAM
target_user.update!( target_user.update!(
display_name: nil,
bio: '',
location: '',
website: '',
profile_picture: nil, profile_picture: nil,
profile_header: nil profile_header: nil
) )
target_user.profile.update!(
display_name: nil,
description: '',
location: '',
website: '',
)
end end
{ {
@ -43,7 +45,11 @@ module UseCase
end end
def source_user def source_user
@source_user ||= ::User.find(source_user_id) if source_user_id
@source_user ||= ::User.find(source_user_id)
else
nil
end
end end
end end
end end

View File

@ -373,9 +373,9 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do
{ {
user: user_param, user: user_param,
ban: ban, ban: ban,
permaban: permaban,
reason: "just a prank, bro", reason: "just a prank, bro",
until: wrongly_formatted_date_ugh duration: duration,
duration_unit: duration_unit,
} }
end end
@ -414,17 +414,17 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do
context "when ban = 0" do context "when ban = 0" do
let(:ban) { "0" } let(:ban) { "0" }
let(:wrongly_formatted_date_ugh) { nil }
"01".each_char do |pb| "01".each_char do |pb|
context "when permaban = #{pb}" do context "when permaban = #{pb}" do
let(:permaban) { pb } let(:duration) { nil }
let(:duration_unit) { nil }
context "when user is already banned" do context "when user is already banned" do
before { target_user.ban } before { target_user.ban(nil) }
it "unbans the user" do it "unbans the user" do
expect { subject }.to(change { target_user.reload.banned? }.from(true).to(false)) expect { subject }.to change { target_user.reload.banned? }.from(true).to(false)
end end
include_examples "returns the expected response" include_examples "returns the expected response"
@ -443,16 +443,18 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do
context "when ban = 1" do context "when ban = 1" do
let(:ban) { "1" } let(:ban) { "1" }
let(:wrongly_formatted_date_ugh) { "4/20/2420 12:00 AM" } let(:duration) { 3 }
let(:duration_unit) { 'hours' }
context "when permaban = 0" do context "when permaban = 0" do
let(:permaban) { "0" } let(:permaban) { "0" }
it "bans the user until 2420-04-20" do it "bans the user for 3 hours" do
expect { subject }.to(change { target_user.reload.banned? }.from(false).to(true)) Timecop.freeze do
expect(target_user).not_to be_permanently_banned expect { subject }.to(change { target_user.reload.banned? }.from(false).to(true))
expect(target_user.ban_reason).to eq("just a prank, bro") expect(target_user.bans.current.first.reason).to eq("just a prank, bro")
expect(target_user.banned_until).to eq(DateTime.strptime(wrongly_formatted_date_ugh, "%m/%d/%Y %I:%M %p")) expect(target_user.bans.current.first.expires_at).to eq(Time.now.utc + 3.hours)
end
end end
include_examples "returns the expected response" include_examples "returns the expected response"
@ -461,13 +463,13 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do
end end
context "when permaban = 1" do context "when permaban = 1" do
let(:permaban) { "1" } let(:duration) { nil }
let(:duration_unit) { nil }
it "bans the user for all eternity" do it "bans the user for all eternity" do
expect { subject }.to(change { target_user.reload.banned? }.from(false).to(true)) expect { subject }.to change { target_user.reload.banned? }.from(false).to(true)
expect(target_user).to be_permanently_banned expect(target_user.bans.current.first.reason).to eq("just a prank, bro")
expect(target_user.ban_reason).to eq("just a prank, bro") expect(target_user.bans.current.first.expires_at).to be_nil
expect(target_user.banned_until).to be_nil
end end
include_examples "returns the expected response" include_examples "returns the expected response"
@ -480,8 +482,8 @@ describe Ajax::ModerationController, :ajax_controller, type: :controller do
context "when user does not exist" do context "when user does not exist" do
let(:user_param) { "fritz-fantom" } let(:user_param) { "fritz-fantom" }
let(:ban) { "1" } let(:ban) { "1" }
let(:permaban) { "1" } let(:duration) { nil }
let(:wrongly_formatted_date_ugh) { "4/20/2420 12:00 AM" } let(:duration_unit) { nil }
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,