Skip to content

Switch to instant mail queue for 7-day streak letter #454

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion app/models/physical_mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class PhysicalMail < ApplicationRecord
first_time_7_streak: 1
}

def self.instant_mission_types
%w[first_time_7_streak]
end

scope :pending_delivery, -> {
where(status: :pending)
.joins(:user)
Expand All @@ -36,14 +40,19 @@ def deliver!
return if status == :sent || theseus_id.present?

slug = "hackatime-#{mission_type.to_s.gsub("_", "-")}"
endpoint = if self.class.instant_mission_types.include?(mission_type)
"https://mail.hackclub.com/api/v1/letter_queues/instant/#{slug}-instant"
else
"https://mail.hackclub.com/api/v1/letter_queues/#{slug}"
end

flavors = FlavorText.compliment
flavors.concat(FlavorText.rare_compliment) if rand(10) == 0

return nil unless user.mailing_address.present?

# authorization: Bearer <token>
response = HTTP.auth("Bearer #{ENV["MAIL_HACKCLUB_TOKEN"]}").post("https://mail.hackclub.com/api/v1/letter_queues/#{slug}", json: {
response = HTTP.auth("Bearer #{ENV["MAIL_HACKCLUB_TOKEN"]}").post(endpoint, json: {
recipient_email: user.email_addresses.first.email,
address: {
first_name: user.mailing_address.first_name,
Expand All @@ -70,6 +79,9 @@ def deliver!
update(status: :failed)
raise "Failed to deliver physical mail: #{response.body}"
end
rescue OpenSSL::SSL::SSLError => e
Rails.logger.warn "SSL error during mail delivery (request likely succeeded): #{e.message}"
update(status: :sent)
rescue => e
update(status: :failed)
raise e
Expand Down
8 changes: 4 additions & 4 deletions config/initializers/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@
cron: "0 * * * *", # Run before AttemptToDeliverPhysicalMailJob
class: "CheckStreakPhysicalMailJob"
},
# attempt_to_deliver_physical_mail: {
# cron: "5 * * * *", # Run after physical mail is created
# class: "AttemptToDeliverPhysicalMailJob"
# },
attempt_to_deliver_physical_mail: {
cron: "5 * * * *", # Run after physical mail is created
class: "AttemptToDeliverPhysicalMailJob"
},
sync_neighborhood_from_airtable: {
cron: "*/15 * * * *",
class: "Neighborhood::SyncFromAirtableJob"
Expand Down