Skip to content

Commit 4f1ff4a

Browse files
authored
Switch to instant mail queue for 7-day streak letter (#454)
1 parent 3676966 commit 4f1ff4a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

app/models/physical_mail.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class PhysicalMail < ApplicationRecord
1414
first_time_7_streak: 1
1515
}
1616

17+
def self.instant_mission_types
18+
%w[first_time_7_streak]
19+
end
20+
1721
scope :pending_delivery, -> {
1822
where(status: :pending)
1923
.joins(:user)
@@ -36,14 +40,19 @@ def deliver!
3640
return if status == :sent || theseus_id.present?
3741

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

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

4352
return nil unless user.mailing_address.present?
4453

4554
# authorization: Bearer <token>
46-
response = HTTP.auth("Bearer #{ENV["MAIL_HACKCLUB_TOKEN"]}").post("https://mail.hackclub.com/api/v1/letter_queues/#{slug}", json: {
55+
response = HTTP.auth("Bearer #{ENV["MAIL_HACKCLUB_TOKEN"]}").post(endpoint, json: {
4756
recipient_email: user.email_addresses.first.email,
4857
address: {
4958
first_name: user.mailing_address.first_name,
@@ -70,6 +79,9 @@ def deliver!
7079
update(status: :failed)
7180
raise "Failed to deliver physical mail: #{response.body}"
7281
end
82+
rescue OpenSSL::SSL::SSLError => e
83+
Rails.logger.warn "SSL error during mail delivery (request likely succeeded): #{e.message}"
84+
update(status: :sent)
7385
rescue => e
7486
update(status: :failed)
7587
raise e

config/initializers/good_job.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@
124124
cron: "0 * * * *", # Run before AttemptToDeliverPhysicalMailJob
125125
class: "CheckStreakPhysicalMailJob"
126126
},
127-
# attempt_to_deliver_physical_mail: {
128-
# cron: "5 * * * *", # Run after physical mail is created
129-
# class: "AttemptToDeliverPhysicalMailJob"
130-
# },
127+
attempt_to_deliver_physical_mail: {
128+
cron: "5 * * * *", # Run after physical mail is created
129+
class: "AttemptToDeliverPhysicalMailJob"
130+
},
131131
sync_neighborhood_from_airtable: {
132132
cron: "*/15 * * * *",
133133
class: "Neighborhood::SyncFromAirtableJob"

0 commit comments

Comments
 (0)