forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: aptos-labs#964
- Loading branch information
Showing
10 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
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
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
14 changes: 14 additions & 0 deletions
14
ecosystem/platform/server/app/jobs/send_confirm_email_job.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) Aptos | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
class SendConfirmEmailJob < SendEmailJob | ||
# Ex args: { user_id: 32, template_vars: { CONFIRM_LINK: 'https://...' } } | ||
|
||
private | ||
|
||
def template_name | ||
:confirm | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) Aptos | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'MailchimpTransactional' | ||
|
||
class SendEmailJobError < StandardError; end | ||
|
||
BAD_STATUSES = %w[rejected invalid].freeze | ||
|
||
class SendEmailJob < ApplicationJob | ||
# Ex args: { user_id: 32, template_name: 'some-template', template_vars: { SOME_MAILMERGE_KEY: 'My Value!' } } | ||
def perform(args) | ||
@args = args | ||
@user = User.find(args[:user_id]) | ||
sentry_scope.set_user(id: @user.id) | ||
|
||
sentry_scope.set_context(:job_args, { template_name: }) | ||
sentry_scope.set_context(:job_args, { template_vars: }) | ||
|
||
send_email | ||
end | ||
|
||
# https://mailchimp.com/developer/transactional/api/messages/send-using-message-template/ | ||
def send_email | ||
client = MailchimpTransactional::Client.new(ENV.fetch('MAILCHIMP_API_KEY', nil)) | ||
body = email_body | ||
sentry_scope.set_context(:email_body, body) | ||
results = client.messages.send_template(body) | ||
handle_results(results) | ||
rescue StandardError => e | ||
Sentry.capture_exception(e) | ||
puts "Error: #{e}" | ||
end | ||
|
||
def email_body | ||
{ | ||
template_name:, | ||
template_content: [{}], | ||
message: | ||
} | ||
end | ||
|
||
private | ||
|
||
def handle_results(results) | ||
# [{"email"=>"[email protected]", "status"=>"sent", "_id"=>"some-id", "reject_reason"=>nil}, ...] | ||
results.each do |res| | ||
# the sending status of the recipient. Possible values: "sent", "queued", "rejected", or "invalid". | ||
raise SendEmailJobError, "Could not send email: #{res['reject_reason']}" if BAD_STATUSES.include? res['status'] | ||
rescue StandardError => e | ||
Rails.logger.warn e | ||
Sentry.capture_exception(e) | ||
end | ||
end | ||
|
||
# Override any of these to have much nicer email job classes! | ||
def template_name | ||
@args[:template_name] | ||
end | ||
|
||
def template_vars | ||
@args[:template_vars] || {} | ||
end | ||
|
||
def message | ||
{ | ||
global_merge_vars:, | ||
from_email:, | ||
to: to_emails.map { |em| { email: em } } | ||
} | ||
end | ||
|
||
def to_emails | ||
[@user.email] | ||
end | ||
|
||
def from_email | ||
'[email protected]' | ||
end | ||
|
||
def global_merge_vars | ||
template_vars.map { |k, v| { name: k, content: v } } | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
ecosystem/platform/server/app/jobs/send_registration_complete_email_job.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) Aptos | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
class SendRegistrationCompleteEmailJob < SendEmailJob | ||
# Ex args: { user_id: 32 } | ||
|
||
private | ||
|
||
def template_name | ||
:'registration-complete' | ||
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