Skip to content
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
19 changes: 19 additions & 0 deletions app/jobs/send_summary_emails_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class SendSummaryEmailsJob < ApplicationJob
queue_as :default

def perform
staff = User.where(staff: true)
posts = Post.unscoped.qa_only.where(created_at: SummaryMailer::TIMEFRAME.ago..DateTime.now)
.includes(:community, :user)
flags = Flag.unscoped.where(created_at: SummaryMailer::TIMEFRAME.ago..DateTime.now)
.includes(:post, :community, :user)
comments = Comment.unscoped.where(created_at: SummaryMailer::TIMEFRAME.ago..DateTime.now)
.includes(:user, :post, :comment_thread, post: :community)
users = User.where(created_at: SummaryMailer::TIMEFRAME.ago..DateTime.now).includes(:community_users)
staff.each do |u|
SummaryMailer.with(to: u.email, posts: posts.to_a, flags: flags.to_a, comments: comments.to_a, users: users.to_a)
.content_summary
.deliver_later
end
end
end
16 changes: 16 additions & 0 deletions app/mailers/summary_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class SummaryMailer < ApplicationMailer
TIMEFRAME = 30.minutes

helper :application, :post_types, :users

def content_summary
@posts = params[:posts]
@flags = params[:flags]
@comments = params[:comments]
@users = params[:users]

mail(from: "#{SiteSetting['NoReplySenderName']} <#{SiteSetting['NoReplySenderEmail']}>",
subject: 'Codidact Content Summary',
to: params[:to])
end
end
76 changes: 76 additions & 0 deletions app/views/summary_mailer/content_summary.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<h1 class="has-font-size-heading">Codidact Content Summary</h2>
<p>
Covering
<%= SummaryMailer::TIMEFRAME.ago.strftime '%a %d %b, %H:%M' %>
to
<%= DateTime.now.strftime '%a %d %b, %H:%M' %>
</p>

<h2 class="has-font-size-larger">New Posts</h2>
<% if @posts.any? %>
<% @posts.first(10).each do |p| %>
<p>
<strong><%= link_to p.title, generic_share_link(p, host: p.community.host) %></strong><br/>
by <%= user_link p.user, { host: p.community.host } %> on <%= p.community.name %>
</p>
<% end %>
<% if @posts.size > 10 %>
<p>and <%= @posts.size - 10 %> more</p>
<% end %>
<% else %>
<p>No new posts in this timeframe.</p>
<% end %>

<h2 class="has-font-size-larger">New Flags</p>
<% if @flags.any? %>
<% @flags.first(10).each do |f| %>
<p>
<strong><%= f.post_flag_type&.name || 'other' %></strong>
flag: <%= f.reason.present? ? "\"#{f.reason}\"" : "" %>
<% status_colors = { helpful: 'is-green', declined: 'is-red', pending: 'is-muted' } %>
<span class="badge is-tag is-small <%= status_colors[(f.status || 'pending').to_sym] %> is-filled">
<%= f.status || 'pending' %>
</span><br/>
by <%= user_link f.user, { host: f.community.host } %>
on <%= link_to f.post.title, generic_share_link(f.post, host: f.community.host) %>
on <%= f.community.name %><br/>
</p>
<% end %>
<% if @flags.size > 10 %>
<p>and <%= @flags.size - 10 %> more</p>
<% end %>
<% else %>
<p>No new flags in this timeframe</p>
<% end %>

<h2 class="has-font-size-larger">New Comments</h2>
<% if @comments.any? %>
<% @comments.first(10).each do |c| %>
<blockquote><%= c.content %></blockquote>
<p class="has-margin-top-0">
by <%= user_link c.user, { host: c.post.community.host } %>
on <%= link_to c.post.title, generic_share_link(c.post, host: c.post.community.host) %>
on <%= c.post.community.name %>
</p>
<% end %>
<% if @comments.size > 10 %>
<p>and <%= @comments.size - 10 %> more</p>
<% end %>
<% else %>
<p>No new comments in this timeframe</p>
<% end %>

<h2 class="has-font-size-larger">New Users</h2>
<% if @users.any? %>
<% @users.first(10).each do |u| %>
<p>
<%= user_link u, { host: u.community_users.unscoped.first.community.host } %>
on <%= u.community_users.unscoped.first.community.name %>
</p>
<% end %>
<% if @users.size > 10 %>
<p>and <%= @users.size - 10 %> more</p>
<% end %>
<% else %>
<p>No new users in this timeframe</p>
<% end %>
4 changes: 4 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@
every 6.hours do
runner 'scripts/recalc_abilities.rb'
end

every 30.minutes do
runner 'scripts/run_summary_mailer.rb'
end
1 change: 1 addition & 0 deletions scripts/run_summary_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SendSummaryEmailsJob.perform_later
10 changes: 10 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ global_admin:
is_global_moderator: false
confirmed_at: 2020-01-01T00:00:00.000000Z

staff:
email: [email protected]
encrypted_password: '$2a$11$roUHXKxecjyQ72Qn7DWs3.9eRCCoRn176kX/UNb/xiue3aGqf7xEW'
sign_in_count: 42
username: staff
is_global_admin: true
is_global_moderator: true
staff: true
confirmed_at: 2020-01-01T00:00:00.000000Z

no_community_user:
email: [email protected]
encrypted_password: '$2a$11$roUHXKxecjyQ72Qn7DWs3.9eRCCoRn176kX/UNb/xiue3aGqf7xEW'
Expand Down
21 changes: 21 additions & 0 deletions test/jobs/send_summary_emails_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

class SendSummaryEmailsJobTest < ActiveJob::TestCase
include ActionMailer::TestCase::ClearTestDeliveries

test 'should correctly send summary emails' do
perform_enqueued_jobs do
SendSummaryEmailsJob.perform_later
end

assert_performed_jobs(2)

delivered = SummaryMailer.deliveries.first

to_email = users(:staff).email

assert_equal 1, delivered.recipients.size
assert delivered.recipients.include?(to_email),
"Expected #{to_email} to be a recipient, actual: #{delivered.recipients.join(', ')}"
end
end
17 changes: 17 additions & 0 deletions test/mailers/previews/summary_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Preview all emails at http://localhost:3000/rails/mailers/summary_mailer
class SummaryMailerPreview < ActionMailer::Preview
def content_summary
test_timeframe = 1.year
staff = User.where(staff: true)
posts = Post.unscoped.qa_only.where(created_at: test_timeframe.ago..DateTime.now)
.includes(:community, :user)
flags = Flag.unscoped.where(created_at: test_timeframe.ago..DateTime.now)
.includes(:post, :community, :user)
comments = Comment.unscoped.where(created_at: test_timeframe.ago..DateTime.now)
.includes(:user, :post, :comment_thread, post: :community)
users = User.where(created_at: test_timeframe.ago..DateTime.now).includes(:community_users)

SummaryMailer.with(to: staff.first.email, posts: posts.to_a, flags: flags.to_a, comments: comments.to_a, users: users.to_a)
.content_summary
end
end
7 changes: 7 additions & 0 deletions test/mailers/summary_mailer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class SummaryMailerTest < ActionMailer::TestCase
# test "the truth" do
# assert true
# end
end
Loading