Skip to content

Instantly share code, notes, and snippets.

@fbuys
Last active October 19, 2021 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbuys/428524cf30bec2e32f6dc10ce5de5b9e to your computer and use it in GitHub Desktop.
Save fbuys/428524cf30bec2e32f6dc10ce5de5b9e to your computer and use it in GitHub Desktop.
Threaded emails: Our first approach
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