Skip to content

Commit

Permalink
Insight Section for Spam2 (publiclab#8289)
Browse files Browse the repository at this point in the history
* insights for spam2

* ui changes and refactoring

* refactoring

* tests for spam2

* ui update and bug fixes

* minor bug fixes
  • Loading branch information
keshavsethi authored and alvesitalo committed Oct 14, 2020
1 parent f9da099 commit 457767d
Show file tree
Hide file tree
Showing 19 changed files with 532 additions and 123 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/spam2.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function table_main(id) {
"info": false,
"bPaginate": false,
"language": {
"search": "Search in this page"
"search": "Search"
}
});
$('#selectall').click(function () {
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/spam2.css
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@
#info-btn {
margin-left: 5vw;
}
#stats_spam, #timeline_spam{
margin-left: 3vw;
margin-right: 3vw !important;
}
}
3 changes: 1 addition & 2 deletions app/controllers/batch_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,13 @@ def batch_comment
comment.publish
when 'spam'
comment.spam
user.ban
when 'delete'
comment.delete
else
flash[:notice] = 'Invalid Url'
end
end
flash[:notice] = comment_total.to_s + ' comment moderated'
flash[:notice] = comment_total.to_s + ' comments moderated.'
redirect_back fallback_location: root_path
else
flash[:error] = 'Only admins and moderators can moderate comments.'
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/spam2_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def _spam
@nodes.where(type: 'page', status: 1).order('changed DESC')
when 'unmoderated'
@nodes.where(status: 4).order('changed DESC')
when 'published'
@nodes.where(status: 1).order('changed DESC')
when 'spammed'
@nodes.where(status: 0).order('changed DESC')
when 'created'
Expand Down Expand Up @@ -104,12 +106,28 @@ def _spam_revisions
end
end

def _spam_insights
if logged_in_as(%w(admin moderator))
@graph_spammed = Node.spam_graph_making(0)
@graph_unmoderated = Node.spam_graph_making(4)
@graph_flagged = Node.where('flag > ?', 0).spam_graph_making(1)
@moderator_tag = Tag.tag_frequency(30)
@popular_tags = Tag.tag_frequency(10)
render template: 'spam2/_spam'
else
flash[:error] = 'Only moderators and admins can access this page.'
redirect_to '/dashboard'
end
end

def _spam_comments
if logged_in_as(%w(moderator admin))
@comments = Comment.paginate(page: params[:page], per_page: params[:pagination])
@comments = case params[:type]
when 'unmoderated'
@comments.where(status: 4).order('timestamp DESC')
when 'published'
@comments.where(status: 1).order('timestamp DESC')
when 'spammed'
@comments.where(status: 0).order('timestamp DESC')
when 'flagged'
Expand Down
15 changes: 15 additions & 0 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,21 @@ def comments_viewable_by(user)
end
end

def self.spam_graph_making(status)
start = Time.now - 1.year
fin = Time.now
time_hash = {}
week = start.to_date.step(fin.to_date, 7).count
while week >= 1
months = (fin - (week * 7 - 1).days)
range = (fin.to_i - week.weeks.to_i)..(fin.to_i - (week - 1).weeks.to_i)
nodes = Node.where(created: range).where(status: status).select(:created).size
time_hash[months.to_f * 1000] = nodes
week -= 1
end
time_hash
end

def notify_callout_users
# notify mentioned users
mentioned_users.each do |user|
Expand Down
7 changes: 7 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ def subscription_graph(start = DateTime.now - 1.year, fin = DateTime.now)
date_hash
end

def self.tag_frequency(limit)
uids = User.where('rusers.role = ?', 'moderator').or(User.where('rusers.role = ?', 'admin')).collect(&:uid)
tids = TagSelection.where(following: true, user_id: uids).collect(&:tid)
hash = tids.uniq.map { |id| p (Tag.find id).name, tids.count(id) }.to_h
hash.sort_by { |_, v| v }.reverse.first(limit).to_h
end

private

def tids
Expand Down
38 changes: 15 additions & 23 deletions app/views/spam2/_comments.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ $(document).ready(function () {
});
$('#unmoderated').on('click', function () { // unmoderated filter
search_table("unmoderated", "/spam2/comments/filter/");
});
$('#published').on('click', function () { // published filter
search_table("published", "/spam2/comments/filter/");
});
$("#batch-spam").bind('click', function (e) { //batch spam
batch_nav("batch_comment/spam");
Expand All @@ -49,11 +52,14 @@ $(document).ready(function () {
<a id="spammed" class="btn nav-link <% if params[:type] == "spammed" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-ban <% if params[:type] == "spammed" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all Spammed Comments "></i> Spammed </a>
</li>
<li class="nav-item">
<a id="unmoderated" class="btn nav-link <% if params[:type] == "unmoderated" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-check-square-o <% if params[:type] == "unmoderated" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all unmoderated Comments"></i> Unmoderated</a>
<a id="unmoderated" class="btn nav-link <% if params[:type] == "unmoderated" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-exclamation-circle <% if params[:type] == "unmoderated" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all unmoderated Comments"></i> Unmoderated</a>
</li>
<li class="nav-item">
<a id="flagged" class="btn nav-link <% if params[:type] == "flagged" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-flag <% if params[:type] == "flagged" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all Flagged Comments and sort by flag count"></i> Flagged</a>
</li>
<li class="nav-item">
<a id="published" class="btn nav-link <% if params[:type] == "published" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-check-circle <% if params[:type] == "published" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all Published Comments"></i> Published</a>
</li>
</ul>
</div>
<div class="card-body" style="overflow-x:hidden;" >
Expand Down Expand Up @@ -84,50 +90,36 @@ $(document).ready(function () {
<%= comment.body.truncate(20) %>
</td>
<td>
<a href="/profile/<%= comment.author&.name %>" class="text-info"><%= comment.author&.name.truncate(15) %></a> <span class="badge badge-pill badge-light text-warning"><%= comment.flag%></span>
<a href="/profile/<%= comment.author&.name %>" class="text-info"><%= comment.author&.name.truncate(15) %></a><br> <% if comment.flag > 0 %><span class="text-secondary small"><%= comment.flag%> Flagged </span><% end %>
</td>
<td>
<% unless comment.node.nil? %>
<span class="text-secondary"><%= time_ago_in_words(comment.timestamp) %> ago</span>
<% end %>
</td>
<td style="height:35px !important;">
<a class="btn btn-sm font-weight-bold btn<% if comment.status != 1 %>-success<% else %>-secondary disabled<% end %> publish" data-remote="true" href="/admin/publish_comment/<%= comment.id %>" ><i class="fa fa-check-circle fa-white"></i> Publish</a>
<a class="btn btn-sm font-weight-bold btn<% if comment.status != 0 %>-danger<% else %>-secondary disabled<% end %> spam" data-remote="true" href="/admin/mark_comment_spam/<%= comment.id %>"><i class="fa fa-ban fa-white"></i> Spam</a>
<a class="btn btn-sm font-weight-bold btn-secondary ban a<%= comment.author.id %>" <% if comment.author.status == 0 %>style="display:none;"<% end %> data-remote="true" href="/ban/<%= comment.author.id %>">Ban user</a>
<a class="btn btn-sm font-weight-bold btn-secondary unban a-unban<%= comment.author.id %>" <% if comment.author.status == 1 %>style="display:none;"<% end %> data-remote="true" href="/unban/<%= comment.author.id %>">Unban user</a>
<a class="btn btn-sm text-dark font-weight-bold unflag btn-<% if comment.flag > 0%>warning <% else %>warning disabled<% end %>" data-remote="true" href="/moderate/remove_flag_comment/<%= comment.id %>">Unflag</a>
<a class="btn btn-xs border-curve font-weight-bold btn<% if comment.status != 1 %>-success<% else %>-secondary disabled<% end %> publish" data-remote="true" href="/admin/publish_comment/<%= comment.id %>" ><i class="fa fa-check-circle fa-white"></i> Publish</a>
<a class="btn btn-xs border-curve font-weight-bold btn<% if comment.status != 0 %>-danger<% else %>-secondary disabled<% end %> spam" data-remote="true" href="/admin/mark_comment_spam/<%= comment.id %>"><i class="fa fa-ban fa-white"></i> Spam</a>
<a class="btn btn-xs border-curve text-dark font-weight-bold unflag btn-<% if comment.flag > 0%>warning <% else %>warning disabled<% end %>" data-remote="true" href="/moderate/remove_flag_comment/<%= comment.id %>">Unflag</a>
<%= link_to "/comment/delete/#{comment.cid}", data: { confirm: "Are you sure you want to delete this comment?" }, :remote => true, :class => "btn border-curve btn-sm font-weight-bold btn-light delete" do %>
<i class="fa fa-trash text-dark"></i>
<% end %>
<script>
$('#n<%= comment.id %> .delete').bind('ajax:success', function(e){
$('#n<%= comment.id %>').fadeOut()
$('#deleted').fadeIn();
$('#deleted').fadeOut(3000);
notyNotification('relax', 3000, 'danger', 'topRight', 'Comment deleted');
});
$('#n<%= comment.id %> .publish').bind('ajax:success', function(e){
$('#n<%= comment.id %>').hide()
$('#npublish').fadeIn(); // published!
$('#npublish').fadeOut(3000);
notyNotification('relax', 3000, 'success', 'topRight', 'Comment published');
});
$('#n<%= comment.id %> .spam').bind('ajax:success', function(e){
$('#n<%= comment.id %>').hide()
$('#nspam').fadeIn(); // spammed!
$('#nspam').fadeOut(3000);
notyNotification('relax', 3000, 'danger', 'topRight', 'Comment spammed');
});
$('#n<%= comment.id %> .unflag').bind('ajax:success', function(e){
$('#n<%= comment.id %>').hide()
$('#nuflag').fadeIn(); // unflag!
$('#nunflag').fadeOut(3000);
});
$('.a<%= comment.author.id %>.ban').bind('ajax:success', function(e){
$('.a<%= comment.author.id %>').hide() // ban toggle
$('.a-unban<%= comment.author.id %>').show()
});
$('.a-unban<%= comment.author.id %>.unban').bind('ajax:success', function(e){
$('.a-unban<%= comment.author.id %>').hide()
$('.a<%= comment.author.id %>').show()
notyNotification('relax', 3000, 'warning', 'topRight', 'Comment unflagged');
});
</script>
</td>
Expand Down
22 changes: 9 additions & 13 deletions app/views/spam2/_flags.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ $(document).ready(function () {
<a id="spammed" class="btn nav-link <% if params[:type] == "spammed" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-ban <% if params[:type] == "spammed" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all flag Nodes which are spammed"></i> Spammed </a>
</li>
<li class="nav-item">
<a id="unmoderated" class="btn nav-link <% if params[:type] == "unmoderated" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-check-square-o <% if params[:type] == "unmoderated" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all flag Nodes which are unmoderated"></i> Unmoderated</a>
<a id="unmoderated" class="btn nav-link <% if params[:type] == "unmoderated" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-exclamation-circle <% if params[:type] == "unmoderated" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all flag Nodes which are unmoderated"></i> Unmoderated</a>
</li>
<li class="nav-item">
<a id="page" class="btn nav-link <% if params[:type] == "page" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-file-text-o <% if params[:type] == "page" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all Wikis which are flagged"></i> Page</a>
Expand Down Expand Up @@ -101,32 +101,28 @@ $(document).ready(function () {
<span class="text-dark"><%= time_ago_in_words(flag.updated_at) %> ago</span>
</td>
<td style="height:35px !important;">
<a class="btn btn-sm font-weight-bold btn<% if flag.status != 1 %>-success<% else %>-secondary disabled<% end %> publish" data-remote="true" href="/moderate/publish/<%= flag.id %>" ><i class="fa fa-check-circle fa-white"></i> Publish post</a>
<a class="btn btn-sm font-weight-bold btn<% if flag.status != 0 %>-danger<% else %>-secondary disabled<% end %> spam" data-remote="true" href="/moderate/spam/<%= flag.id %>"><i class="fa fa-ban fa-white"></i> Spam post</a>
<a class="btn btn-sm font-weight-bold btn-warning unflag" data-remote="true"href="/moderate/remove_flag_node/<%= flag.id %>">Unflag</a>
<a class="btn btn-xs border-curve font-weight-bold btn<% if flag.status != 1 %>-success<% else %>-secondary disabled<% end %> publish" data-remote="true" href="/moderate/publish/<%= flag.id %>" ><i class="fa fa-check-circle fa-white"></i> Publish post</a>
<a class="btn btn-xs border-curve font-weight-bold btn<% if flag.status != 0 %>-danger<% else %>-secondary disabled<% end %> spam" data-remote="true" href="/moderate/spam/<%= flag.id %>"><i class="fa fa-ban fa-white"></i> Spam post</a>
<a class="btn btn-xs border-curve font-weight-bold btn-warning unflag" data-remote="true"href="/moderate/remove_flag_node/<%= flag.id %>">Unflag</a>
<%= link_to "/notes/delete/#{flag.id}", data: { confirm: "Are you sure you want to delete #{flag.path}?" }, :remote => true, :class => "btn border-curve btn-sm font-weight-bold btn-light delete" do %>
<i class="fa fa-trash text-dark"></i>
<% end %>
<script>
$('#n<%= flag.id %> .delete').bind('ajax:success', function(e){
$('#n<%= flag.id %>').fadeOut()
$('#deleted').fadeIn();
$('#deleted').fadeOut(3000);
notyNotification('relax', 3000, 'danger', 'topRight', 'Node deleted');
});
$('#n<%= flag.id %> .publish').bind('ajax:success', function(e){
$('#n<%= flag.id %>').hide()
$('#npublish').fadeIn(); // published!
$('#npublish').fadeOut(3000);
notyNotification('relax', 3000, 'success', 'topRight', 'Node published');
});
$('#n<%= flag.id %> .spam').bind('ajax:success', function(e){
$('#n<%= flag.id %>').hide()
$('#nspam').fadeIn(); // spammed!
$('#nspam').fadeOut(3000);
notyNotification('relax', 3000, 'danger', 'topRight', 'Node spammed');
});
$('#n<%= flag.id %> .unflag').bind('ajax:success', function(e){
$('#n<%= flag.id %>').hide()
$('#nunflag').fadeIn(); // unflag!
$('#nunflag').fadeOut(3000);
notyNotification('relax', 3000, 'warning', 'topRight', 'Node unflagged');
});
</script>
</td>
Expand Down Expand Up @@ -159,7 +155,7 @@ $(document).ready(function () {
<div class="float-right">
<%= will_paginate @flags, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || @flags.empty?%>
</div>
<div class="float-left">
<div class="float-left text-secondary">
Page <%= @flags.current_page%> of <%= @flags.total_pages %> total Pages
</div>
</div>
Loading

0 comments on commit 457767d

Please sign in to comment.