-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2200 from samvera/i1019-batch-email-notifications
🎁 1019 batch email notifications
- Loading branch information
Showing
38 changed files
with
787 additions
and
36 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
app/controllers/hyrax/dashboard/profiles_controller_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
class BatchEmailNotificationJob < ApplicationJob | ||
non_tenant_job | ||
after_perform do |job| | ||
reenqueue(job.arguments.first) | ||
end | ||
|
||
def perform(account) | ||
Apartment::Tenant.switch(account.tenant) do | ||
# Query for all users that have email_frequency turned off | ||
users = User.where.not(batch_email_frequency: "never") | ||
users.each do |user| | ||
next unless send_email_today?(user) | ||
# Find all undelivered messages within the frequency range of a user and any emails that haven't been sent | ||
undelivered_messages = | ||
Mailboxer::Message.joins(:receipts) | ||
.where(mailboxer_receipts: { receiver_id: user.id, receiver_type: 'User', is_delivered: false }) | ||
.where('mailboxer_notifications.created_at >= ?', frequency_date(user.batch_email_frequency)) | ||
.select('mailboxer_notifications.*') | ||
.distinct | ||
.to_a | ||
|
||
next if undelivered_messages.blank? | ||
send_email(user, undelivered_messages, account) | ||
|
||
# Mark the as read | ||
undelivered_messages.each do |message| | ||
message.receipts.each do |receipt| | ||
receipt.update(is_delivered: true) | ||
end | ||
end | ||
|
||
user.update(last_emailed_at: Time.current) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def reenqueue(account) | ||
BatchEmailNotificationJob.set(wait_until: Date.tomorrow.midnight).perform_later(account) | ||
end | ||
|
||
def send_email_today?(user) | ||
return true if user.last_emailed_at.nil? | ||
|
||
next_email_date = case user.batch_email_frequency | ||
when "daily" | ||
user.last_emailed_at + 1.day | ||
when "weekly" | ||
user.last_emailed_at + 1.week | ||
when "monthly" | ||
user.last_emailed_at + 1.month | ||
end | ||
|
||
Time.current >= next_email_date | ||
end | ||
|
||
def frequency_date(frequency) | ||
case frequency | ||
when "daily" | ||
1.day.ago | ||
when "weekly" | ||
1.week.ago | ||
when "monthly" | ||
1.month.ago | ||
end | ||
end | ||
|
||
def send_email(user, undelivered_messages, account) | ||
HykuMailer.summary_email(user, undelivered_messages, account).deliver_now | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# OVERRIDE: Mailboxer v0.15.1 to mark receipts as delivered for users that | ||
# set their `batch_email_frequency` to 'never' | ||
|
||
module Mailboxer | ||
module ReceiptDecorator | ||
extend ActiveSupport::Concern | ||
|
||
prepended do | ||
after_create :mark_as_delivered | ||
end | ||
|
||
def mark_as_delivered | ||
user = User.find_by(id: receiver_id) | ||
return unless user&.batch_email_frequency == 'never' | ||
|
||
update(is_delivered: true) | ||
end | ||
end | ||
end | ||
|
||
Mailboxer::Receipt.prepend(Mailboxer::ReceiptDecorator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Notifications</title> | ||
<style> | ||
table { | ||
width: 100%; | ||
border-collapse: collapse; | ||
margin-bottom: 20px; | ||
} | ||
th, td { | ||
border: 1px solid #ddd; | ||
padding: 8px; | ||
text-align: left; | ||
} | ||
th { | ||
background-color: #f2f2f2; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h2>Hello, <%= @user.name %></h2> | ||
<p>You have the following notifications:</p> | ||
<p>Click <a href="<%= @url %>">here</a> to view them on <%= @application_name %>.</p> | ||
<table> | ||
<tr> | ||
<th>Date</th> | ||
<th>Subject</th> | ||
<th>Message</th> | ||
</tr> | ||
<% @messages.each do |message| %> | ||
<tr> | ||
<td><%= message.created_at.to_date %></td> | ||
<td><%= message.subject %></td> | ||
<td><%= message.body.gsub('href="', "href=\"#{URI(@url).origin}").html_safe %></td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<!-- OVERRIDE Hyrax 5.0: Add batch email frequency to User edit --> | ||
|
||
<%= form_for @user, | ||
url: hyrax.dashboard_profile_path(@user.to_param), | ||
html: { multipart: true } do |f| %> | ||
<div class="form-group row"> | ||
<%= f.label :avatar, t(".change_picture").html_safe, class: "col-4 col-form-label" %> | ||
<div class="col-8"> | ||
<%= image_tag @user.avatar.url(:thumb) if @user.avatar? %> | ||
<%= f.file_field :avatar %> | ||
<%= f.hidden_field :avatar_cache %> | ||
<span class="form-text"><%= t('.help_change_picture_type') %></span> | ||
|
||
<div class="form-check"> | ||
<%= f.label :remove_avatar, class: 'form-check-label' do %> | ||
<%= f.check_box :remove_avatar, class: 'form-check-input' %> | ||
<%= t(".delete_picture") %> | ||
<a href="#" id="delete_picture_help" data-toggle="popover" data-content="<%= t('.delete_picture_data_content') %>" data-original-title="<%= t('.delete_picture_data_original_title') %>"><i class="fa fa-question"></i></a> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div><!-- .form-group --> | ||
|
||
<% if Hyrax.config.arkivo_api? %> | ||
<%= render partial: 'zotero', locals: { f: f, user: @user } %> | ||
<% end %> | ||
|
||
<div class="form-group row"> | ||
<%= f.label :orcid, class: 'col-4 col-form-label' do %> | ||
<%= orcid_label %> | ||
<% end %> | ||
<div class="col-8"> | ||
<%= f.text_field :orcid, class: "form-control" %> | ||
</div> | ||
</div><!-- .form-group --> | ||
|
||
<div class="form-group row"> | ||
<%= f.label :twitter_handle, t(".twitter_handle").html_safe, class: 'col-4 col-form-label' %> | ||
<div class="col-8"> | ||
<%= f.text_field :twitter_handle, class: "form-control" %> | ||
</div> | ||
</div><!-- .form-group --> | ||
|
||
<div class="form-group row"> | ||
<%= f.label :facebook_handle, t(".facebook_handle").html_safe, class: 'col-4 col-form-label' %> | ||
<div class="col-8"> | ||
<%= f.text_field :facebook_handle, class: "form-control" %> | ||
</div> | ||
</div><!-- .form-group --> | ||
|
||
<div class="form-group row"> | ||
<%= f.label :googleplus_handle, t(".google_handle").html_safe, class: 'col-4 col-form-label' %> | ||
<div class="col-8"> | ||
<%= f.text_field :googleplus_handle, class: "form-control" %> | ||
</div> | ||
</div><!-- .form-group --> | ||
|
||
<!-- OVERRIDE: Add batch email frequency to edit --> | ||
<div class="form-group row"> | ||
<%= f.label :batch_email_frequency, t("hyrax.user_profile.email_frequency.label").html_safe, class: 'col-4 col-form-label' %> | ||
<div class="col-8"> | ||
<%= f.select :batch_email_frequency, controller.frequency_options, {}, { class: "form-control" } %> | ||
</div> | ||
</div><!-- .form-group --> | ||
|
||
<%= render 'trophy_edit', trophies: @trophies %> | ||
<%= f.button t(".save_profile").html_safe, type: 'submit', class: "btn btn-primary" %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<!-- OVERRIDE Hyrax 5.0: Display batch email frequency --> | ||
|
||
<dl id="user_info"> | ||
|
||
<% if user.orcid.present? %> | ||
<dt><%= orcid_label('profile') %></dt> | ||
<dd><%= link_to user.orcid, user.orcid, { target: '_blank' } %></dd> | ||
<% end %> | ||
<% if Hyrax.config.arkivo_api? && user.zotero_userid.present? %> | ||
<dt><%= zotero_label(html_class: 'profile') %></dt> | ||
<dd><%= link_to zotero_profile_url(user.zotero_userid), zotero_profile_url(user.zotero_userid), { target: '_blank' } %></dd> | ||
<% end %> | ||
<% if user.facebook_handle.present? %> | ||
<dt><span class="fa fa-facebook" aria-hidden="true"></span> Facebook Handle</dt> | ||
<dd><%= link_to user.facebook_handle, "http://facebook.com/#{user.facebook_handle}", {target:'_blank'} %></dd> | ||
<% end %> | ||
<% if user.twitter_handle.present? %> | ||
<dt><span class="fa fa-twitter" aria-hidden="true"></span> Twitter Handle</dt> | ||
<dd><%= link_to user.twitter_handle, "http://twitter.com/#{user.twitter_handle}", {target:'_blank'} %></dd> | ||
<% end %> | ||
<% if user.googleplus_handle.present? %> | ||
<dt><span class="fa fa-google-plus" aria-hidden="true"></span> Google+ Handle</dt> | ||
<dd><%= link_to user.googleplus_handle, "http://google.com/+#{user.googleplus_handle}", {target:'_blank'} %></dd> | ||
<% end %> | ||
<% if user.linkedin_handle.present? %> | ||
<dt><span class="fa fa-linkedin" aria-hidden="true"></span> LinkedIn</dt> | ||
<dd><%= link_to "#{@linkedInUrl}", "#{@linkedInUrl}", { target: '_blank' } %></dd> | ||
<% end %> | ||
|
||
<dt><span class="fa fa-envelope" aria-hidden="true"></span> Email</dt> | ||
<dd><%= mail_to user.email %></dd> | ||
|
||
<% if user.chat_id %> | ||
<dt><span class="fa fa-bullhorn" aria-hidden="true"></span> Chat ID</dt> | ||
<dd><%= user.chat_id %></dd> | ||
<% end %> | ||
<% if user.website %> | ||
<dt><span class="fa fa-globe" aria-hidden="true"></span> Website(s)</dt> | ||
<dd><%= iconify_auto_link(user.website) %></dd> | ||
<% end %> | ||
<% if user.title %> | ||
<dt>Title</dt> | ||
<dd><%= user.title %></dd> | ||
<% end %> | ||
<% if user.admin_area %> | ||
<dt>Administrative Area</dt> | ||
<dd><%= user.admin_area %></dd> | ||
<% end %> | ||
<% if user.department %> | ||
<dt>Department</dt> | ||
<dd><%= user.department %></dd> | ||
<% end %> | ||
<% if user.office %> | ||
<dt>Office</dt> | ||
<dd><%= user.office %></dd> | ||
<% end %> | ||
<% if user.address %> | ||
<dt><span class="fa fa-map-marker" aria-hidden="true"></span> Address</dt> | ||
<dd><%= user.address %></dd> | ||
<% end %> | ||
<% if user.affiliation %> | ||
<dt>Affiliation</dt> | ||
<dd><%= user.affiliation %></dd> | ||
<% end %> | ||
<% if user.telephone %> | ||
<dt><span class="fa fa-phone" aria-hidden="true"></span> Telephone</dt> | ||
<dd><%= link_to_telephone(user) %></dd> | ||
<% end %> | ||
|
||
<!-- OVERRIDE Hyrax 5.0: Display batch email frequency --> | ||
<dt><%= t("hyrax.user_profile.email_frequency.label").html_safe %></dt> | ||
<% frequency = user.batch_email_frequency || 'not_set' %> | ||
<dd><%= t("hyrax.user_profile.email_frequency.#{frequency}") %></dd> | ||
</dl> |
Oops, something went wrong.