Add permabans, for those special individuals that need them

This commit is contained in:
Kira 2019-08-10 00:22:34 -07:00
parent db390427c1
commit 9721705b64
10 changed files with 47 additions and 13 deletions

View File

@ -51,7 +51,7 @@ class BansController < ApplicationController
private
def ban_params(context)
permitted_params = %i[reason duration expires_at]
permitted_params = %i[reason duration expires_at is_permaban]
permitted_params += %i[user_id user_name] if context == :create
params.fetch(:ban, {}).permit(permitted_params)

View File

@ -1,4 +1,5 @@
class Ban < ApplicationRecord
attr_accessor :is_permaban
after_create :create_feedback
after_create :update_user_on_create
after_create :create_ban_mod_action
@ -9,12 +10,13 @@ class Ban < ApplicationRecord
validate :user_is_inferior
validates_presence_of :user_id, :reason, :duration
before_validation :initialize_banner_id, :on => :create
before_validation :initialize_permaban, on: [:update, :create]
scope :unexpired, -> { where("bans.expires_at > ?", Time.now) }
scope :expired, -> { where("bans.expires_at <= ?", Time.now) }
scope :unexpired, -> { where("bans.expires_at > ? OR bans.expires_at IS NULL", Time.now) }
scope :expired, -> { where("bans.expires_at IS NOT NULL").where("bans.expires_at <= ?", Time.now) }
def self.is_banned?(user)
exists?(["user_id = ? AND expires_at > ?", user.id, Time.now])
exists?(["user_id = ? AND (expires_at > ? OR expires_at IS NULL)", user.id, Time.now])
end
def self.reason_matches(query)
@ -69,6 +71,12 @@ class Ban < ApplicationRecord
self.banner_id = CurrentUser.id if self.banner_id.blank?
end
def initialize_permaban
if is_permaban == "1"
self.duration = -1
end
end
def user_is_inferior
if user
if user.is_admin?
@ -105,7 +113,12 @@ class Ban < ApplicationRecord
end
def duration=(dur)
self.expires_at = dur.to_i.days.from_now
dur = dur.to_i
if dur < 0
self.expires_at = nil
else
self.expires_at = dur.days.from_now
end
@duration = dur
end
@ -114,11 +127,22 @@ class Ban < ApplicationRecord
end
def humanized_duration
return 'permanent' if expires_at == nil
ApplicationController.helpers.distance_of_time_in_words(created_at, expires_at)
end
def humanized_expiration
return 'never' if expires_at == nil
ApplicationController.helpers.compact_time expires_at
end
def expire_days
return 'never' if expires_at == nil
ApplicaitonController.helpers.time_ago_in_words(expires_at)
end
def expired?
expires_at < Time.now
expires_at != nil && expires_at < Time.now
end
def create_feedback

View File

@ -19,7 +19,7 @@ class UserPresenter
def ban_reason
if user.is_banned?
"#{user.recent_ban.reason}; expires #{user.recent_ban.expires_at} (#{user.bans.count} bans total)"
"#{user.recent_ban.reason}; expires #{user.recent_ban.expires_at || 'never'} (#{user.bans.count} bans total)"
else
nil
end

View File

@ -3,6 +3,7 @@
<%= f.input :user_name, :as => :string %>
<%= f.input :duration, :hint => "in days" %>
<%= f.input :is_permaban, as: :boolean, hint: 'ignores days' %>
<%= f.input :reason %>
<%= f.button :submit, :value => "Ban" %>
<% end %>

View File

@ -6,6 +6,7 @@
<%= error_messages_for("ban") %>
<%= f.input :duration, :hint => "in days" %>
<%= f.input :is_permaban, as: :boolean, hint: 'ignores days' %>
<%= f.input :reason %>
<%= f.button :submit, :value => "Ban" %>
<% end %>

View File

@ -11,6 +11,7 @@
<th>Banner</th>
<th>Banned</th>
<th>Duration</th>
<th>Expiration</th>
<th>Reason</th>
<th></th>
</tr>
@ -27,7 +28,8 @@
<%= link_to "»", bans_path(search: search_params.merge(banner_name: ban.banner.name)) %>
</td>
<td><%= time_ago_in_words_tagged(ban.created_at) %></td>
<td><%= humanized_duration(ban.created_at, ban.expires_at) %></td>
<td><%= ban.humanized_duration %></td>
<td><%= ban.humanized_expiration %></td>
<td class="col-expand"><%= format_text ban.reason %></td>
<td>
<% if CurrentUser.is_moderator? %>

View File

@ -3,7 +3,7 @@
<h1>Show Ban</h1>
<ul style="margin-bottom: 1em;">
<li><strong>User</strong>: <%= link_to_user(@ban.user) %></li>
<li><strong>Expires</strong>: <%= compact_time @ban.expires_at %></li>
<li><strong>Expires</strong>: <%= @ban.humanized_expiration %></li>
<li><strong>Reason</strong>: <%= format_text @ban.reason %></li>
</ul>

View File

@ -1,5 +1,5 @@
<div class="ui-corner-all ui-state-error" id="ban-notice">
<h1>Your account has been temporarily banned</h1>
<h1>Your account has been banned</h1>
<p>Reason: <%= format_text CurrentUser.user.recent_ban.reason %></p>
<p>Your ban will expire in <%= time_ago_in_words(CurrentUser.user.recent_ban.expires_at) %></p>
<p>Ban expires: <%= CurrentUser.user.recent_ban.expire_days %></p>
</div>

View File

@ -0,0 +1,5 @@
class AllowNullBanExpiresAt < ActiveRecord::Migration[5.2]
def change
change_column_null :bans, :expires_at, true
end
end

View File

@ -488,7 +488,7 @@ CREATE TABLE public.bans (
user_id integer,
reason text NOT NULL,
banner_id integer NOT NULL,
expires_at timestamp without time zone NOT NULL,
expires_at timestamp without time zone,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
@ -5186,6 +5186,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190717205018'),
('20190718201354'),
('20190801210547'),
('20190804010156');
('20190804010156'),
('20190810064211');