[Admin] Allow marking content as having earned a user a warning/ban

This commit is contained in:
Kira 2021-04-04 22:06:06 -07:00
parent 3851941e39
commit 1feee5e9a5
16 changed files with 166 additions and 10 deletions

View File

@ -0,0 +1,44 @@
module UserWarnable
extend ActiveSupport::Concern
WARNING_TYPES = {
'warning' => 1,
'record' => 2,
'ban' => 3,
'unmark' => nil
}
included do
scope :user_warned, -> { where('warning_type IS NOT NULL') }
validates :warning_type, inclusion: { :in => [1, 2, 3, nil] }
end
def user_warned!(type, user=CurrentUser.id)
unless WARNING_TYPES.has_key?(type)
errors.add(:warning_type, 'invalid warning type')
return
end
update({warning_type: WARNING_TYPES[type], warning_user_id: user})
end
def remove_user_warning!
update_columns({warning_type: nil, warning_user_id: nil})
end
def was_warned?
!warning_type.nil?
end
def warning_type_string
case warning_type
when 1
"User received a warning for the contents of this message."
when 2
"User received a record for the contents of this message."
when 3
"User was banned for the contents of this message."
else
"[This is a bug with the website. Woo!]"
end
end
end

View File

@ -2,7 +2,7 @@ class BlipsController < ApplicationController
class BlipTooOld < Exception ; end
respond_to :html, :json
before_action :member_only, only: [:create, :new, :update, :edit, :hide]
before_action :moderator_only, only: [:unhide, :destroy]
before_action :moderator_only, only: [:unhide, :destroy, :warning]
rescue_from BlipTooOld, with: :blip_too_old
@ -84,6 +84,16 @@ class BlipsController < ApplicationController
end
end
def warning
@blip = Blip.find(params[:id])
if params[:record_type] == 'unmark'
@blip.remove_user_warning!
else
@blip.user_warned!(params[:record_type])
end
respond_with(@blip)
end
private
def search_params

View File

@ -1,7 +1,7 @@
class CommentsController < ApplicationController
respond_to :html, :json
before_action :member_only, :except => [:index, :search, :show]
before_action :moderator_only, only: [:unhide, :destroy]
before_action :moderator_only, only: [:unhide, :destroy, :warning]
skip_before_action :api_check
def index
@ -79,6 +79,16 @@ class CommentsController < ApplicationController
respond_with(@comment)
end
def warning
@comment = Comment.find(params[:id])
if params[:record_type] == 'unmark'
@comment.remove_user_warning!
else
@comment.user_warned!(params[:record_type])
end
respond_with(@comment)
end
private
def index_by_post
tags = params[:tags] || ""

View File

@ -1,8 +1,8 @@
class ForumPostsController < ApplicationController
respond_to :html, :json
before_action :member_only, :except => [:index, :show, :search]
before_action :moderator_only, only: [:destroy, :unhide]
before_action :load_post, :only => [:edit, :show, :update, :destroy, :hide, :unhide]
before_action :moderator_only, only: [:destroy, :unhide, :warning]
before_action :load_post, :only => [:edit, :show, :update, :destroy, :hide, :unhide, :warning]
before_action :check_min_level, :only => [:edit, :show, :update, :destroy, :hide, :unhide]
skip_before_action :api_check
@ -72,6 +72,15 @@ class ForumPostsController < ApplicationController
respond_with(@forum_post)
end
def warning
if params[:record_type] == 'unmark'
@forum_post.remove_user_warning!
else
@forum_post.user_warned!(params[:record_type])
end
respond_with(@forum_post)
end
private
def load_post
@forum_post = ForumPost.includes(topic: [:category]).find(params[:id])

View File

@ -0,0 +1,22 @@
import Utility from './utility.js';
$(() => {
$('.item-mark-user-warned').on('click', function(evt) {
const target = $(evt.target);
const type = target.data('item-type');
const id = target.data('item-id');
const record_type = target.data('record-type');
$.ajax({
type: "POST",
url: `/${type}s/${id}/warning.json`,
data: {
'record_type': record_type
},
}).done(function(data) {
location.reload();
}).fail(function(data) {
Utility.error("Failed to mark as warned.");
});
});
});

View File

@ -69,6 +69,7 @@
@import "specific/user_deletions.scss";
@import "specific/user_feedback.scss";
@import "specific/user_name_change_requests.scss";
@import "specific/user_warned.scss";
@import "specific/users.scss";
@import "specific/wiki_pages.scss";
@import "specific/wiki_page_versions.scss";

View File

@ -0,0 +1,3 @@
.user-warning em {
@include themable { color: themed("color-rating-explicit"); }
}

View File

@ -1,4 +1,5 @@
class Blip < ApplicationRecord
include UserWarnable
simple_versioning
belongs_to_creator
user_status_counter :blip_count
@ -48,6 +49,7 @@ class Blip < ApplicationRecord
end
def can_edit?(user)
return false if was_warned? && !user.is_moderator?
(creator_id == user.id && created_at > 5.minutes.ago) || user.is_moderator?
end

View File

@ -1,5 +1,5 @@
class Comment < ApplicationRecord
include UserWarnable
simple_versioning
belongs_to_creator
belongs_to_updater
@ -168,6 +168,7 @@ class Comment < ApplicationRecord
end
def editable_by?(user)
return false if was_warned? && !user.is_moderator?
creator_id == user.id || user.is_moderator?
end

View File

@ -1,5 +1,5 @@
class ForumPost < ApplicationRecord
include UserWarnable
simple_versioning
attr_readonly :topic_id
belongs_to_creator
@ -183,6 +183,7 @@ class ForumPost < ApplicationRecord
end
def editable_by?(user)
return false if was_warned? && !user.is_moderator?
(creator_id == user.id || user.is_moderator?) && visible?(user)
end

View File

@ -56,6 +56,15 @@
<strong>IP</strong>
<span><%= link_to_ip blip.creator_ip_addr %></span>
</li>
<li>|</li>
<% if !blip.was_warned? %>
<li>Mark for:</li>
<li><%= tag.a "Warning", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'warning' } %></li>
<li><%= tag.a "Record", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'record' } %></li>
<li><%= tag.a "Ban", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'ban' } %></li>
<% else %>
<li><%= tag.a "Unmark", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'unmark' } %></li>
<% end %>
<% end %>
</menu>
</div>

View File

@ -24,6 +24,12 @@
<%= render "application/update_notice", record: comment %>
</div>
<% if comment.was_warned? %>
<div class="user-warning">
<hr>
<em><%= comment.warning_type_string %></em>
</div>
<% end %>
<div class="content-menu">
<menu>
<% if @post || @posts || comment.post.present? %>
@ -58,6 +64,15 @@
<strong>IP</strong>
<span><%= link_to_ip comment.creator_ip_addr %></span>
</li>
<li>|</li>
<% if !comment.was_warned? %>
<li>Mark for:</li>
<li><%= tag.a "Warning", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'warning' } %></li>
<li><%= tag.a "Record", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'record' } %></li>
<li><%= tag.a "Ban", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'ban' } %></li>
<% else %>
<li><%= tag.a "Unmark", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'unmark' } %></li>
<% end %>
<% end %>
<% end %>
</menu>

View File

@ -65,6 +65,15 @@
<span><%= link_to_ip forum_post.creator_ip_addr %></span>
</li>
<li>|</li>
<% if !forum_post.was_warned? %>
<li>Mark for:</li>
<li><%= tag.a "Warning", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'warning' } %></li>
<li><%= tag.a "Record", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'record' } %></li>
<li><%= tag.a "Ban", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'ban' } %></li>
<% else %>
<li><%= tag.a "Unmark", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'unmark' } %></li>
<% end %>
<li>|</li>
<% end %>
<% if params[:controller] == "forum_posts" %>
<li><%= link_to "Parent", forum_topic_path(forum_post.topic, :page => forum_post.forum_topic_page, :anchor => "forum_post_#{forum_post.id}") %></li>

View File

@ -136,6 +136,7 @@ Rails.application.routes.draw do
member do
post :hide
post :unhide
post :warning
end
end
resources :comment_votes, only: [:index, :delete, :lock] do
@ -167,6 +168,7 @@ Rails.application.routes.draw do
member do
post :hide
post :unhide
post :warning
end
collection do
get :search
@ -365,6 +367,7 @@ Rails.application.routes.draw do
member do
post :hide
post :unhide
post :warning
end
end
resources :post_report_reasons

View File

@ -0,0 +1,10 @@
class AddUserWarnFields < ActiveRecord::Migration[6.1]
def change
add_column :blips, :warning_type, :integer
add_column :blips, :warning_user_id, :integer
add_column :forum_posts, :warning_type, :integer
add_column :forum_posts, :warning_user_id, :integer
add_column :comments, :warning_type, :integer
add_column :comments, :warning_user_id, :integer
end
end

View File

@ -412,7 +412,9 @@ CREATE TABLE public.blips (
is_hidden boolean DEFAULT false,
body_index tsvector NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
updated_at timestamp without time zone NOT NULL,
warning_type integer,
warning_user_id integer
);
@ -527,7 +529,9 @@ CREATE TABLE public.comments (
updater_ip_addr inet,
do_not_bump_post boolean DEFAULT false NOT NULL,
is_hidden boolean DEFAULT false NOT NULL,
is_sticky boolean DEFAULT false NOT NULL
is_sticky boolean DEFAULT false NOT NULL,
warning_type integer,
warning_user_id integer
);
@ -919,7 +923,9 @@ CREATE TABLE public.forum_posts (
is_hidden boolean DEFAULT false NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone,
creator_ip_addr inet
creator_ip_addr inet,
warning_type integer,
warning_user_id integer
);
@ -5084,6 +5090,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20201113073842'),
('20201220172926'),
('20201220190335'),
('20210117173030');
('20210117173030'),
('20210405040522');