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
# obligatory '2001: A Space Odyssey' reference
flash[:notice] = t('flash.ban.error', name: name)
unless current_user.ban_reason.nil?
flash[:notice] += "\n#{t('flash.ban.reason', reason: current_user.ban_reason)}"
current_ban = current_user.bans.current.first
unless current_ban&.reason.nil?
flash[:notice] += "\n#{t('flash.ban.reason', reason: current_user.bans.current.first.reason)}"
end
if not current_user.permanently_banned?
unless current_ban&.permanently_banned?
# TODO format banned_until
flash[:notice] += "\n#{t('flash.ban.until', time: current_user.banned_until)}"
end

View File

@ -237,7 +237,17 @@ class User < ApplicationRecord
# @param reason [String] Reason for the ban. This is displayed to the user.
# @param banned_by [User] User who instated the ban
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
def can_export?

View File

@ -1,7 +1,7 @@
- current_ban = user.bans.current.first
.modal.fade#modal-ban{ aria: { hidden: true, labelledby: 'modal-ban-label' }, role: :dialog, tabindex: -1 }
.modal-dialog
.modal-content
.modal-content#ban-control-super
.modal-header
%h5.modal-title#modal-ban-label
= t 'views.modal.bancontrol.title'
@ -11,7 +11,7 @@
= bootstrap_form_tag(url: '/mod/ban', html: { method: :post, novalidate: :novalidate }) do |f|
= f.hidden_field :user, value: user.screen_name
- if current_ban.nil?
.modal-body#ban-control-super
.modal-body
= f.check_box :ban, label: t('views.modal.bancontrol.ban'), checked: user.banned?
#ban-controls{ style: user.banned? ? '' : 'display: none' }
= 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, 'weeks', label: 'Weeks'
= 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
%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'
- else
= f.hidden_field :ban, value: '0'
.modal-body#ban-control-super
.modal-body
- if current_ban.expires_at.nil?
This user is currently permanently banned for
%strong= current_ban.reason
@ -40,7 +40,7 @@
- if current_ban.banned_by.present?
%br
This ban was instated by
%strong= current_ban.banned_by.safe_name
%strong= current_ban.banned_by.profile.safe_name
on
%strong= current_ban.created_at
.modal-footer

View File

@ -24,13 +24,15 @@ module UseCase
if reason == REASON_SPAM
target_user.update!(
display_name: nil,
bio: '',
location: '',
website: '',
profile_picture: nil,
profile_header: nil
)
target_user.profile.update!(
display_name: nil,
description: '',
location: '',
website: '',
)
end
{
@ -43,7 +45,11 @@ module UseCase
end
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

View File

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