-
-
Save fbuys/428524cf30bec2e32f6dc10ce5de5b9e to your computer and use it in GitHub Desktop.
Threaded emails: Our first approach
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
class NotificationMailer < ApplicationMailer | |
def review_notification(recipient, review) | |
@review = review | |
@recipient = recipient | |
mail_settings = { | |
to: "#{recipient.name} <#{recipient.email}>", | |
subject: "Review: #{review.book.title}" | |
}.merge(headers(review)) | |
mail(mail_settings) | |
end | |
private | |
def headers(review) | |
{ | |
in_reply_to: in_reply_to(review), | |
message_id: message_id(review), | |
references: references(review) | |
}.reject { |_, v| v.blank? } | |
end | |
def in_reply_to(review) | |
previous_review = reviews_in_thread(review).last | |
message_id(previous_review) | |
end | |
def message_id(review) | |
return "" if review.nil? | |
"<notification-#{review.id}-#{review.created_at.to_i}@#{ENV["domain"]}>" | |
end | |
def references(review) | |
reviews_in_thread(review).map { |x| message_id(x) }.join(" ") | |
end | |
def reviews_in_thread(review) | |
Review.where.not(id: review.id).where("book_id", review.book_id).order(:created_at) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment