Skip to content

Commit

Permalink
Spam2: system for flagging nodes and comments to moderators and User …
Browse files Browse the repository at this point in the history
…moderation (publiclab#8107)

* spam2 flag nodes

* nav align

* flag comment moderation

* flag comment order

* spam2 flag comments

* spam2 flag and filters update

* codeclimate issue

* codeclimate issue2

* reduce filters

* spam2 code climate

* test issues spam2

* spam2 flag filters

* spam2 flag tests

* flag spam2 test issue

* flag count and ui update

* spam2 typo in tests

* spam2 syatem test update

* user moderation and search feature

* indentation

* user moderation

* indentation

* UI update of spam2

* spam2 ui update

* bulk user moderation

* bulk user moderation spam2

* refactoring spam2

* spam2 refactoring
  • Loading branch information
keshavsethi authored and reginaalyssa committed Oct 16, 2021
1 parent bc245df commit 7e3decc
Show file tree
Hide file tree
Showing 24 changed files with 860 additions and 214 deletions.
30 changes: 26 additions & 4 deletions app/assets/javascripts/spam2.js
Expand Up @@ -10,11 +10,10 @@ function table_main(id) {
"regex": true
},
"scrollX": true,
"info": false,
"bPaginate": false,
"language": {
"search": "Search in this batch",
"info": "Showing _START_ to _END_ of _TOTAL_ entries in batch",
"infoFiltered": "(filtered from this batch of _MAX_ entries)",
"lengthMenu": "Show _MENU_ entries for this batch"
"search": "Search in this page"
}
});
$('#selectall').click(function () {
Expand All @@ -37,4 +36,27 @@ function disable_buttons(id) {
} else {
$("#batch-spam, #batch-publish, #delete-batch, #batch-ban, #batch-unban").addClass("disabled");
}
}
var pageselect = localStorage.getItem('page-select') || '30';
var filter_value = localStorage.getItem('filter') || 'all';

function pagination(id, url) {
var currValue = $(id).val();
localStorage.setItem('page-select', currValue);
window.location = url + filter_value + "/" + $(id).val();
$(id).val(pageselect);
}

function search_table(filter, url) {
localStorage.setItem('filter', filter);
localStorage.setItem('page-select', pageselect);
window.location = url + filter + "/" + $("#pageselect").val();
}

function batch_nav(bulk) {
vals = []
$('.selectedId').each(function (i, a) { // batch nav
if (a.checked) vals.push(a.value);
});
window.location = "/spam2/" + bulk + "/" + vals.join(',');
}
35 changes: 33 additions & 2 deletions app/assets/stylesheets/spam2.css
Expand Up @@ -41,6 +41,10 @@
color: blue !important;
}

#docs-btn:hover {
color: #17a2b8!important
}

#table-card {
width: 100%;
}
Expand All @@ -50,11 +54,24 @@
}

#bulk-nav {
margin-top: 2%;
margin-bottom: 1%;
margin-top: 2vh;
margin-bottom: 1vh;
width: 100%;
}

#info-btn {
margin: 10px 0 0 1vw;
}

#info-div {
padding: 10px;
text-align: center;
line-height: 1.6;
}
.border-curve {
border-radius: 40px;
}

.active .drop {
color: white !important;
}
Expand All @@ -74,6 +91,17 @@
margin-top: 10px;
}

.card .navbar .nav .nav-item .active {
color: blue;
border-bottom: 2px blue solid;
}

.spam-alert {
position: fixed;
width: 82vw;
top: 10vh;
z-index: 10;
}
@media (max-width: 750px) {
#table-card,
#bulk-nav,
Expand All @@ -90,4 +118,7 @@
#card-top .card-header {
font-size: 18px;
}
#info-btn {
margin-left: 5vw;
}
}
3 changes: 3 additions & 0 deletions app/controllers/admin_controller.rb
Expand Up @@ -125,6 +125,7 @@ def mark_spam
if @node.status == 1 || @node.status == 4
@node.spam
@node.author.ban
@node.unflag_node
# No longer notifying other moderators as of https://github.com/publiclab/plots2/issues/6246
# AdminMailer.notify_moderators_of_spam(@node, current_user).deliver_later
flash[:notice] = "Item marked as spam and author banned. You can undo this on the <a href='/spam'>spam moderation page</a>."
Expand All @@ -150,6 +151,7 @@ def mark_comment_spam
@comment.spam
user = @comment.author
user.ban
@comment.unflag_comment
# No longer notifying other moderators as of https://github.com/publiclab/plots2/issues/6246
# AdminMailer.notify_moderators_of_comment_spam(@comment, current_user).deliver_later
flash[:notice] = "Comment has been marked as spam and comment author has been banned. You can undo this on the <a href='/spam/comments'>spam moderation page</a>."
Expand All @@ -173,6 +175,7 @@ def publish_comment
if @comment.author.banned?
@comment.author.unban
end
@comment.unflag_comment
if first_timer_comment
AdminMailer.notify_author_of_comment_approval(@comment, current_user).deliver_later
# No longer notifying other moderators as of https://github.com/publiclab/plots2/issues/6246
Expand Down
174 changes: 151 additions & 23 deletions app/controllers/spam2_controller.rb
Expand Up @@ -3,27 +3,77 @@ class Spam2Controller < ApplicationController

def _spam
if logged_in_as(%w(moderator admin))
@nodes = Node.order('changed DESC')
.paginate(page: params[:page], per_page: 100)
@nodes = if params[:type] == 'wiki'
@nodes.where(type: 'page', status: 1)
@nodes = Node.paginate(page: params[:page], per_page: params[:pagination])
@nodes = case params[:type]
when 'wiki'
@nodes.where(type: 'page', status: 1).order('changed DESC')
when 'unmoderated'
@nodes.where(status: 4).order('changed DESC')
when 'spammed'
@nodes.where(status: 0).order('changed DESC')
when 'created'
@nodes.where(status: [0, 4]).order('created DESC')
else
@nodes.where(status: [0, 4]).order('changed DESC')
end
@node_unmoderated_count = Node.where(status: 4).length
@node_flag_count = Node.where('flag > ?', 0).length
else
flash[:error] = 'Only moderators can moderate posts.'
redirect_to '/dashboard'
end
end

def _spam_flags
if logged_in_as(%w(moderator admin))
@flags = Node.where('flag > ?', 0)
.order('flag DESC')
.paginate(page: params[:page], per_page: params[:pagination])
@flags = case params[:type]
when 'unmoderated'
@flags.where(status: 4)
when 'spammed'
@flags.where(status: 0)
when 'page'
@flags.where(type: 'page')
when 'note'
@flags.where(type: 'node')
else
@nodes.where(status: [0, 4])
@flags
end
@spam_count = @nodes.where(status: 0).length
@unmoderated_count = @nodes.where(status: 4).length
@page_count = @nodes.where(type: 'page').length
@note_count = @nodes.where(type: 'note').length
render template: 'spam2/_spam'
else
flash[:error] = 'Only moderators can moderate posts.'
redirect_to '/dashboard'
end
end

def _spam_users
if logged_in_as(%w(moderator admin))
@users = User.paginate(page: params[:page], per_page: params[:pagination])
@users = case params[:type]
when 'banned'
@users.where('rusers.status = 0')
when 'moderator'
@users.where('rusers.role = ?', params[:type])
when 'admin'
@users.where('rusers.role = ?', params[:type])
else
@users.where('rusers.status = 1')
end
@user_active_count = User.where('rusers.status = 1').length
@user_ban_count = User.where('rusers.status = 0').length
render template: 'spam2/_spam'
else
flash[:error] = 'Only moderators can moderate other users.'
redirect_to '/dashboard'
end
end

def _spam_revisions
if logged_in_as(%w(admin moderator))
@revisions = Revision.where(status: 0)
.paginate(page: params[:page], per_page: 100)
.paginate(page: params[:page], per_page: 50)
.order('timestamp DESC')
render template: 'spam2/_spam'
else
Expand All @@ -34,9 +84,21 @@ def _spam_revisions

def _spam_comments
if logged_in_as(%w(moderator admin))
@comments = Comment.where(status: 0)
.order('timestamp DESC')
.paginate(page: params[:page], per_page: 100)
@comments = Comment.paginate(page: params[:page], per_page: params[:pagination])
@comments = case params[:type]
when 'unmoderated'
@comments.where(status: 4).order('timestamp DESC')
when 'spammed'
@comments.where(status: 0).order('timestamp DESC')
when 'flagged'
@comments.where('flag > ?', 0).order('flag DESC')
else
@comments.where(status: [0, 4])
.or(@comments.where('flag > ?', 0))
.order('timestamp DESC')
end
@comment_unmoderated_count = Comment.where(status: 4).length
@comment_flag_count = Comment.where('flag > ?', 0).length
render template: 'spam2/_spam'
else
flash[:error] = 'Only moderators can moderate comments.'
Expand Down Expand Up @@ -103,13 +165,11 @@ def batch_delete
def batch_ban
if logged_in_as(%w(admin moderator))
user_ban = []
node_ban = 0
params[:ids].split(',').uniq.each do |nid|
node = Node.find nid
user = node.author
user.ban
user_ban << user.id
node_ban += 1
user.ban
end
flash[:notice] = user_ban.length.to_s + ' users banned.'
redirect_back fallback_location: root_path
Expand All @@ -120,19 +180,87 @@ def batch_ban
end

def batch_unban
unbanned_users = []
if logged_in_as(%w(moderator admin))
users_unban = []
params[:ids].split(',').uniq.each do |nid|
node = Node.find nid
user = node.author
user.unban
users_unban << user.id
params[:ids].split(',').uniq.each do |node_id|
node = Node.find node_id
user_unban = node.author
unbanned_users << user_unban.id
user_unban.unban
end
flash[:notice] = users_unban.length.to_s + ' users unbanned.'
flash[:notice] = unbanned_users.length.to_s + ' users unbanned.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can unban users.'
redirect_to '/dashboard'
end
end

def batch_ban_user
users = []
if logged_in_as(%w(admin moderator))
params[:ids].split(',').uniq.each do |user_id|
user_ban = User.find user_id
users << user_ban.id
user_ban.ban
end
flash[:notice] = users.length.to_s + ' users banned.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only moderators can moderate users.'
redirect_to '/dashboard'
end
end

def batch_unban_user
if logged_in_as(%w(moderator admin))
params[:ids].split(',').uniq.each do |id|
unban_user = User.find id
unban_user.unban
end
flash[:notice] = 'Success! users unbanned.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can moderate users.'
redirect_to '/dashboard'
end
end

def flag_node
@node = Node.find params[:id]
@node.flag_node
flash[:notice] = 'Node flagged.'
redirect_back fallback_location: root_path
end

def remove_flag_node
if logged_in_as(%w(moderator admin))
@node = Node.find params[:id]
if @node.flag.zero?
flash[:notice] = 'Node already unflagged.'
else
@node.unflag_node
end
else
flash[:error] = 'Only admins and moderators can unflag nodes.'
redirect_to '/dashboard'
end
end

def flag_comment
@comment = Comment.find params[:id]
@comment.flag_comment
flash[:notice] = 'Comment flagged.'
redirect_back fallback_location: root_path
end

def remove_flag_comment
if logged_in_as(%w(admin moderator))
@comment = Comment.find params[:id]
@comment.unflag_comment
else
flash[:error] = 'Only moderators can unflag comments.'
redirect_to '/dashboard'
end
end
end
12 changes: 12 additions & 0 deletions app/models/comment.rb
Expand Up @@ -193,6 +193,18 @@ def publish
self
end

def flag_comment
self.flag += 1
save
self
end

def unflag_comment
self.flag = 0
save
self
end

def liked_by(user_id)
likes.where(user_id: user_id).count > 0
end
Expand Down
12 changes: 12 additions & 0 deletions app/models/node.rb
Expand Up @@ -237,6 +237,18 @@ def spam
self
end

def flag_node
self.flag += 1
save
self
end

def unflag_node
self.flag = 0
save
self
end

def files
drupal_files
end
Expand Down

0 comments on commit 7e3decc

Please sign in to comment.