Skip to content

Spam2: Digest for Unmoderated posts with settings #8058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,9 @@ def smtp_test

s.close
end

def test_digest_email_spam
DigestSpamJob.perform_async(0)
redirect_to "/spam"
end
end
4 changes: 3 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ def save_settings

digest_settings = [
'digest:weekly',
'digest:daily'
'digest:daily',
'digest:weekly:spam',
'digest:daily:spam'
]
digest_settings.each do |setting|
if params[setting] == "on"
Expand Down
15 changes: 15 additions & 0 deletions app/jobs/digest_spam_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class DigestSpamJob
include Sidekiq::Worker
def perform(frequency_digest)
if frequency_digest.zero?
tag_digest = 'digest:daily:spam'
elsif frequency_digest == 1
tag_digest = 'digest:weekly:spam'
end
users = User.where(role: %w(moderator admin))
.includes(:user_tags)
.where('user_tags.value=?', tag_digest)
.references(:user_tags)
users.each(&:send_digest_email_spam)
end
end
11 changes: 11 additions & 0 deletions app/mailers/admin_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,15 @@ def notify_moderators_of_spam(node, moderator)
subject: subject
)
end

def send_digest_spam(nodes, frequency_digest)
if frequency_digest == User::Frequency::DAILY
@subject = 'Your daily digest for moderation'
elsif frequency_digest == User::Frequency::WEEKLY
@subject = 'Your weekly digest for moderation'
end
moderators = User.where(role: %w(moderator admin)).collect(&:email)
@nodes = nodes
mail(to: moderators, subject: @subject)
end
end
30 changes: 30 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,23 @@ def content_followed_in_period(start_time, end_time, node_type = 'note', include
.distinct
end

def unmoderated_in_period(start_time, end_time)
tag_following = TagSelection.where(following: true, user_id: uid)
ids = []
tag_following.each do |tagname|
ids += NodeTag.where(tid: tagname.tid).collect(&:nid)
end
range = "(created >= #{start_time.to_i} AND created <= #{end_time.to_i})"
Node.where(nid: ids)
.includes(:revision, :tag)
.references(:node_revision)
.where('node.status = 4')
.where(type: 'note')
.where(range)
.order('node_revisions.timestamp DESC')
.distinct
end

def social_link(site)
return nil unless has_power_tag(site)

Expand Down Expand Up @@ -356,6 +373,19 @@ def send_digest_email
end
end

def send_digest_email_spam
if has_tag('digest:weekly:spam')
@frequency_digest = Frequency::WEEKLY
@nodes_unmoderated = unmoderated_in_period(1.week.ago, Time.current)
elsif has_tag('digest:daily:spam')
@frequency_digest = Frequency::DAILY
@nodes_unmoderated = unmoderated_in_period(1.day.ago, Time.current)
end
if @nodes_unmoderated.size.positive?
AdminMailer.send_digest_spam(@nodes_unmoderated, @frequency_digest).deliver_now
end
end

def tag_counts
tags = {}
Node.order('nid DESC').where(type: 'note', status: 1, uid: id).limit(20).each do |node|
Expand Down
39 changes: 39 additions & 0 deletions app/views/admin_mailer/send_digest_spam.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<header style="margin:0 auto; text-align:center; width:100%;">
<img src="https://avatars2.githubusercontent.com/u/4621650?s=200&v=4">
<p style="font-size:1.5em; color:black;">[Public Lab] <i style="color:grey;"><%= @subject %></i></p>
<p style="font-size:1em; color:grey;"><%= Time.now.strftime("%B %-d %Y") %></p>
<p style="font-size:1em; font-family:sans-serif; margin-bottom:0; color:grey;">Unmoderated Notes at <a href="https://publiclab.org" style="color:black; text-decoration:none;">PublicLab.org</a></p>
<hr style="height:1px; background-color:black; width:50%;">
</header>

<ul style="list-style-type:none; height:auto; font-family:sans-serif;width:auto;">
<% @nodes.each do |n| %>
<li style="position:relative; height:auto; width:90vw;">
<div style="padding:3vh 0 0 5vw ;">
<a style="text-decoration:none; color:black; font-weight:500; font-size:1.5em; " href="<%= ActionMailer::Base.default_url_options[:host] + n.path %>"><%= n.title %></a>
</div>
<table style="padding-left:5vw; font-size:1em;width:70vw">
<tbody> <tr><td style="width:8vh;">
<% if n.author.photo? %>
<img style="width:7vh; height:7vh; border-radius:50%; padding-top:2vh" src="https://<%= ActionMailer::Base.default_url_options[:host] %><%= n.author.photo_path(:thumb) %>"/>
<% else %>
<img style="width:7vh; height:7vh;border-radius:50%;padding-top:2vh;" src="https://www.gravatar.com/avatar/1aedb8d9dc4751e229a335e371db8058"/>
<% end %>
</td>
<td style= "font-size: 2vh;">
<p style="font-weight:500;"><%= n.author.username.capitalize %> </p>
<span style="color:#999;font-weight:500;">Created at <%= n.created_at.strftime("%B %-d %Y") %></span>
</td></tr>
</table>
<div style="padding-left:5vw;">
<p style="color:grey; font-size:1.0em; line-height: 1.6;"><i><%= n.body.truncate(300) %></i></p>
</div>
<a style="color:blue; text-decoration:none; padding-left:5vw" href="https://<%= ActionMailer::Base.default_url_options[:host] %>/moderate/publish/<%= n.id %>">Approve</a> <a style="color:blue; text-decoration:none;"href="https://<%= ActionMailer::Base.default_url_options[:host] %>/moderate/spam/<%= n.id %>">Spam</a></div>
<hr style="border:none; height:1px; width: 45%; background-color:grey; margin-top:2%;">
</li>
<% end %>
</ul>
<div style="margin:5% 0 0 8vw; font-size:1em;">
<p style=" text-decoration:none;">Click <a style="color:blue; text-decoration:none;"href="https://<%= ActionMailer::Base.default_url_options[:host] %>/subscriptions">here</a> to choose your followed topics</p>
<p style=" text-decoration:none;">Click <a style="color:blue; text-decoration:none;" href="https://<%= ActionMailer::Base.default_url_options[:host] %>/settings">here</a> to change your subscription settings</p>
</div>
3 changes: 3 additions & 0 deletions app/views/users/profile.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@
<%= form_tag "/users/test_digest_email", method: :post do %>
<%= submit_tag "Test Digest Email", class: "btn btn-info", style: "width: 100%;" %>
<% end %>
<%= form_tag "/admin/test_digest_email_spam", method: :post do %>
<%= submit_tag "Test Spam Digest Email", class: "btn btn-sm btn-primary mt-2", style: "width: 100%;" %>
<% end %>
<% end %>

<% end %>
Expand Down
63 changes: 53 additions & 10 deletions app/views/users/settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<hr style="width: 35%; margin-left: 0;" />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to be notified by email for comments on your posts? </span>
<span>Do you want to be notified by a email for comments on your posts? </span>
<span>
<label class="switch">
<p> Notification switch </p>
Expand All @@ -18,7 +18,7 @@
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to be notified by email for likes on your posts? </span>
<span>Do you want to be notified by a email for likes on your posts? </span>
<span>
<label class="switch">
<p> Notification switch </p>
Expand All @@ -32,7 +32,7 @@
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to be notified by email for comments on all posts you've commented on? </span>
<span>Do you want to be notified by a email for comments on all posts you've commented on? </span>
<span>
<label class="switch">
<p> Notification switch </p>
Expand All @@ -47,7 +47,7 @@

<% if logged_in_as(['admin', 'moderator']) %>
<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to be notified by email for moderation emails? </span>
<span>Do you want to be notified by a email for moderation emails? </span>
<span>
<label class="switch">
<p> Notification switch </p>
Expand All @@ -63,7 +63,7 @@


<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive customized digest weekly?</span>
<span>Do you want to receive a customized digest weekly?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<p> Notification switch </p>
Expand All @@ -77,7 +77,7 @@
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive customized digest daily?</span>
<span>Do you want to receive a customized digest daily?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<p> Notification switch </p>
Expand All @@ -91,7 +91,7 @@
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive browser notification when you are mentioned?</span>
<span>Do you want to receive a browser notification when you are mentioned?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<p> Notification switch </p>
Expand All @@ -105,7 +105,7 @@
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive browser notification for all ?</span>
<span>Do you want to receive a browser notification for all ?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<p> Notification switch </p>
Expand All @@ -119,7 +119,7 @@
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive browser notification when someone likes your work?</span>
<span>Do you want to receive a browser notification when someone likes your work?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<p> Notification switch </p>
Expand Down Expand Up @@ -172,8 +172,40 @@
<!-- <br />-->
<!-- <br />-->

</div>

<br>
<% if current_user.can_moderate? %>
<h4><b>Spam Digest Settings</b></h4>
<hr style="width: 35%; margin-left: 0;" />
<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive a weekly digest for unmoderated posts?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<p> Notification switch </p>
<input type="checkbox" name="digest:weekly:spam" <% if UserTag.exists?(current_user.id, 'digest:weekly:spam') %>checked<% end %>>
<span class="slider round"></span>
</label>
</span>
</div>

<br />
<br />

<div style="display: inline-flex; justify-content: space-between; width: 90%;">
<span>Do you want to receive a daily digest for unmoderated posts?</span>
<span>
<label style=" vertical-align: middle;" class="switch">
<p> Notification switch </p>
<input type="checkbox" name="digest:daily:spam" <% if UserTag.exists?(current_user.id, 'digest:daily:spam') %>checked<% end %>>
<span class="slider round"></span>
</label>
</span>
</div>

<br />
<br />
<% end %>
</div>

<br />
<br />
Expand Down Expand Up @@ -268,5 +300,16 @@
$(':input[name="digest:daily"]').prop('checked', false);
}
});
$(':input[name="digest:daily:spam"]').change(function () {
if($(':input[name="digest:daily:spam"]').prop('checked') === true){
$(':input[name="digest:weekly:spam"]').prop('checked', false);
}
});

$(':input[name="digest:weekly:spam"]').change(function () {
if($(':input[name="digest:weekly:spam"]').prop('checked') === true){
$(':input[name="digest:daily:spam"]').prop('checked', false);
}
});
});
</script>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@
get 'questions/liked(/:tagnames)' => 'questions#liked'

post 'users/test_digest_email' => 'users#test_digest_email'
post 'admin/test_digest_email_spam' => 'admin#test_digest_email_spam'

get 'comment/delete/:id' => 'comment#delete'
get 'comment/update/:id' => 'comment#update'
Expand Down
2 changes: 2 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@

every 1.day do
runner "DigestMailJob.perform_async(0)"
runner "DigestSpamJob.perform_async(0)"
end

every 1.week do
runner "DigestMailJob.perform_async(1)"
runner "DigestSpamJob.perform_async(1)"
end
6 changes: 6 additions & 0 deletions test/functional/admin_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,10 @@ def teardown
assert_response :success
assert_not_nil assigns(:users)
end

test "test digest emails to moderators" do
UserSession.create(users(:moderator))
post :test_digest_email_spam
assert_redirected_to '/spam'
end
end