Skip to content

Commit c60aaab

Browse files
authored
Spam2: Digest for Unmoderated posts with settings (#8058)
* digest for unmoderated posts * diegst * tag indentation * spam2 digest admins
1 parent 7a5c0bd commit c60aaab

File tree

11 files changed

+168
-11
lines changed

11 files changed

+168
-11
lines changed

app/controllers/admin_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,9 @@ def smtp_test
395395

396396
s.close
397397
end
398+
399+
def test_digest_email_spam
400+
DigestSpamJob.perform_async(0)
401+
redirect_to "/spam"
402+
end
398403
end

app/controllers/users_controller.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,9 @@ def save_settings
401401

402402
digest_settings = [
403403
'digest:weekly',
404-
'digest:daily'
404+
'digest:daily',
405+
'digest:weekly:spam',
406+
'digest:daily:spam'
405407
]
406408
digest_settings.each do |setting|
407409
if params[setting] == "on"

app/jobs/digest_spam_job.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class DigestSpamJob
2+
include Sidekiq::Worker
3+
def perform(frequency_digest)
4+
if frequency_digest.zero?
5+
tag_digest = 'digest:daily:spam'
6+
elsif frequency_digest == 1
7+
tag_digest = 'digest:weekly:spam'
8+
end
9+
users = User.where(role: %w(moderator admin))
10+
.includes(:user_tags)
11+
.where('user_tags.value=?', tag_digest)
12+
.references(:user_tags)
13+
users.each(&:send_digest_email_spam)
14+
end
15+
end

app/mailers/admin_mailer.rb

+11
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,15 @@ def notify_moderators_of_spam(node, moderator)
120120
subject: subject
121121
)
122122
end
123+
124+
def send_digest_spam(nodes, frequency_digest)
125+
if frequency_digest == User::Frequency::DAILY
126+
@subject = 'Your daily digest for moderation'
127+
elsif frequency_digest == User::Frequency::WEEKLY
128+
@subject = 'Your weekly digest for moderation'
129+
end
130+
moderators = User.where(role: %w(moderator admin)).collect(&:email)
131+
@nodes = nodes
132+
mail(to: moderators, subject: @subject)
133+
end
123134
end

app/models/user.rb

+30
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ def content_followed_in_period(start_time, end_time, node_type = 'note', include
268268
.distinct
269269
end
270270

271+
def unmoderated_in_period(start_time, end_time)
272+
tag_following = TagSelection.where(following: true, user_id: uid)
273+
ids = []
274+
tag_following.each do |tagname|
275+
ids += NodeTag.where(tid: tagname.tid).collect(&:nid)
276+
end
277+
range = "(created >= #{start_time.to_i} AND created <= #{end_time.to_i})"
278+
Node.where(nid: ids)
279+
.includes(:revision, :tag)
280+
.references(:node_revision)
281+
.where('node.status = 4')
282+
.where(type: 'note')
283+
.where(range)
284+
.order('node_revisions.timestamp DESC')
285+
.distinct
286+
end
287+
271288
def social_link(site)
272289
return nil unless has_power_tag(site)
273290

@@ -356,6 +373,19 @@ def send_digest_email
356373
end
357374
end
358375

376+
def send_digest_email_spam
377+
if has_tag('digest:weekly:spam')
378+
@frequency_digest = Frequency::WEEKLY
379+
@nodes_unmoderated = unmoderated_in_period(1.week.ago, Time.current)
380+
elsif has_tag('digest:daily:spam')
381+
@frequency_digest = Frequency::DAILY
382+
@nodes_unmoderated = unmoderated_in_period(1.day.ago, Time.current)
383+
end
384+
if @nodes_unmoderated.size.positive?
385+
AdminMailer.send_digest_spam(@nodes_unmoderated, @frequency_digest).deliver_now
386+
end
387+
end
388+
359389
def tag_counts
360390
tags = {}
361391
Node.order('nid DESC').where(type: 'note', status: 1, uid: id).limit(20).each do |node|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<header style="margin:0 auto; text-align:center; width:100%;">
2+
<img src="https://avatars2.githubusercontent.com/u/4621650?s=200&v=4">
3+
<p style="font-size:1.5em; color:black;">[Public Lab] <i style="color:grey;"><%= @subject %></i></p>
4+
<p style="font-size:1em; color:grey;"><%= Time.now.strftime("%B %-d %Y") %></p>
5+
<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>
6+
<hr style="height:1px; background-color:black; width:50%;">
7+
</header>
8+
9+
<ul style="list-style-type:none; height:auto; font-family:sans-serif;width:auto;">
10+
<% @nodes.each do |n| %>
11+
<li style="position:relative; height:auto; width:90vw;">
12+
<div style="padding:3vh 0 0 5vw ;">
13+
<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>
14+
</div>
15+
<table style="padding-left:5vw; font-size:1em;width:70vw">
16+
<tbody> <tr><td style="width:8vh;">
17+
<% if n.author.photo? %>
18+
<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) %>"/>
19+
<% else %>
20+
<img style="width:7vh; height:7vh;border-radius:50%;padding-top:2vh;" src="https://www.gravatar.com/avatar/1aedb8d9dc4751e229a335e371db8058"/>
21+
<% end %>
22+
</td>
23+
<td style= "font-size: 2vh;">
24+
<p style="font-weight:500;"><%= n.author.username.capitalize %> </p>
25+
<span style="color:#999;font-weight:500;">Created at <%= n.created_at.strftime("%B %-d %Y") %></span>
26+
</td></tr>
27+
</table>
28+
<div style="padding-left:5vw;">
29+
<p style="color:grey; font-size:1.0em; line-height: 1.6;"><i><%= n.body.truncate(300) %></i></p>
30+
</div>
31+
<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>
32+
<hr style="border:none; height:1px; width: 45%; background-color:grey; margin-top:2%;">
33+
</li>
34+
<% end %>
35+
</ul>
36+
<div style="margin:5% 0 0 8vw; font-size:1em;">
37+
<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>
38+
<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>
39+
</div>

app/views/users/profile.html.erb

+3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@
209209
<%= form_tag "/users/test_digest_email", method: :post do %>
210210
<%= submit_tag "Test Digest Email", class: "btn btn-info", style: "width: 100%;" %>
211211
<% end %>
212+
<%= form_tag "/admin/test_digest_email_spam", method: :post do %>
213+
<%= submit_tag "Test Spam Digest Email", class: "btn btn-sm btn-primary mt-2", style: "width: 100%;" %>
214+
<% end %>
212215
<% end %>
213216

214217
<% end %>

app/views/users/settings.html.erb

+53-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<hr style="width: 35%; margin-left: 0;" />
55

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

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

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

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

6464

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

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

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

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

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

175-
</div>
176175

176+
<br>
177+
<% if current_user.can_moderate? %>
178+
<h4><b>Spam Digest Settings</b></h4>
179+
<hr style="width: 35%; margin-left: 0;" />
180+
<div style="display: inline-flex; justify-content: space-between; width: 90%;">
181+
<span>Do you want to receive a weekly digest for unmoderated posts?</span>
182+
<span>
183+
<label style=" vertical-align: middle;" class="switch">
184+
<p> Notification switch </p>
185+
<input type="checkbox" name="digest:weekly:spam" <% if UserTag.exists?(current_user.id, 'digest:weekly:spam') %>checked<% end %>>
186+
<span class="slider round"></span>
187+
</label>
188+
</span>
189+
</div>
190+
191+
<br />
192+
<br />
193+
194+
<div style="display: inline-flex; justify-content: space-between; width: 90%;">
195+
<span>Do you want to receive a daily digest for unmoderated posts?</span>
196+
<span>
197+
<label style=" vertical-align: middle;" class="switch">
198+
<p> Notification switch </p>
199+
<input type="checkbox" name="digest:daily:spam" <% if UserTag.exists?(current_user.id, 'digest:daily:spam') %>checked<% end %>>
200+
<span class="slider round"></span>
201+
</label>
202+
</span>
203+
</div>
204+
205+
<br />
206+
<br />
207+
<% end %>
208+
</div>
177209

178210
<br />
179211
<br />
@@ -268,5 +300,16 @@
268300
$(':input[name="digest:daily"]').prop('checked', false);
269301
}
270302
});
303+
$(':input[name="digest:daily:spam"]').change(function () {
304+
if($(':input[name="digest:daily:spam"]').prop('checked') === true){
305+
$(':input[name="digest:weekly:spam"]').prop('checked', false);
306+
}
307+
});
308+
309+
$(':input[name="digest:weekly:spam"]').change(function () {
310+
if($(':input[name="digest:weekly:spam"]').prop('checked') === true){
311+
$(':input[name="digest:daily:spam"]').prop('checked', false);
312+
}
313+
});
271314
});
272315
</script>

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@
344344
get 'questions/liked(/:tagnames)' => 'questions#liked'
345345

346346
post 'users/test_digest_email' => 'users#test_digest_email'
347+
post 'admin/test_digest_email_spam' => 'admin#test_digest_email_spam'
347348

348349
get 'comment/delete/:id' => 'comment#delete'
349350
get 'comment/update/:id' => 'comment#update'

config/schedule.rb

+2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646

4747
every 1.day do
4848
runner "DigestMailJob.perform_async(0)"
49+
runner "DigestSpamJob.perform_async(0)"
4950
end
5051

5152
every 1.week do
5253
runner "DigestMailJob.perform_async(1)"
54+
runner "DigestSpamJob.perform_async(1)"
5355
end

test/functional/admin_controller_test.rb

+6
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,10 @@ def teardown
638638
assert_response :success
639639
assert_not_nil assigns(:users)
640640
end
641+
642+
test "test digest emails to moderators" do
643+
UserSession.create(users(:moderator))
644+
post :test_digest_email_spam
645+
assert_redirected_to '/spam'
646+
end
641647
end

0 commit comments

Comments
 (0)