Class: EmailLog
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EmailLog
- Defined in:
- app/models/email_log.rb
Constant Summary collapse
- CRITICAL_EMAIL_TYPES =
Set.new %w[ account_created admin_login confirm_new_email confirm_old_email confirm_old_email_add forgot_password notify_old_email notify_old_email_add signup signup_after_approval ]
- SMTP_ERROR_CODE_REGEXP =
Regexp.new(/\d\.\d\.\d+|\d{3}/).freeze
Class Method Summary collapse
- .count_per_day(start_date, end_date) ⇒ Object
- .for(reply_key) ⇒ Object
- .last_sent_email_address ⇒ Object
- .reached_max_emails?(user, email_type = nil) ⇒ Boolean
- .unique_email_per_post(post, user) ⇒ Object
Instance Method Summary collapse
- #as_mail_message ⇒ Object
- #bounce_key ⇒ Object
- #cc_addresses_split ⇒ Object
- #cc_users ⇒ Object
- #raw_body ⇒ Object
- #raw_headers ⇒ Object
- #topic ⇒ Object
Class Method Details
.count_per_day(start_date, end_date) ⇒ Object
77 78 79 80 81 82 |
# File 'app/models/email_log.rb', line 77 def self.count_per_day(start_date, end_date) where("created_at BETWEEN ? AND ?", start_date, end_date) .group("DATE(created_at)") .order("DATE(created_at)") .count end |
.for(reply_key) ⇒ Object
84 85 86 |
# File 'app/models/email_log.rb', line 84 def self.for(reply_key) self.find_by(reply_key: reply_key) end |
.last_sent_email_address ⇒ Object
88 89 90 |
# File 'app/models/email_log.rb', line 88 def self.last_sent_email_address self.where(email_type: "signup").order(created_at: :desc).limit(1).pluck(:to_address).first end |
.reached_max_emails?(user, email_type = nil) ⇒ Boolean
67 68 69 70 71 72 73 74 75 |
# File 'app/models/email_log.rb', line 67 def self.reached_max_emails?(user, email_type = nil) if SiteSetting.max_emails_per_day_per_user == 0 || CRITICAL_EMAIL_TYPES.include?(email_type) return false end count = where("created_at > ?", 1.day.ago).where(user_id: user.id).count count >= SiteSetting.max_emails_per_day_per_user end |
.unique_email_per_post(post, user) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/email_log.rb', line 55 def self.unique_email_per_post(post, user) return yield unless post && user DistributedMutex.synchronize("email_log_#{post.id}_#{user.id}") do if where(post_id: post.id, user_id: user.id).exists? nil else yield end end end |
Instance Method Details
#as_mail_message ⇒ Object
105 106 107 108 |
# File 'app/models/email_log.rb', line 105 def return if self.raw.blank? @mail_message ||= Mail.new(self.raw) end |
#bounce_key ⇒ Object
92 93 94 |
# File 'app/models/email_log.rb', line 92 def bounce_key super&.delete("-") end |
#cc_addresses_split ⇒ Object
101 102 103 |
# File 'app/models/email_log.rb', line 101 def cc_addresses_split @cc_addresses_split ||= self.cc_addresses&.split(";") || [] end |
#cc_users ⇒ Object
96 97 98 99 |
# File 'app/models/email_log.rb', line 96 def cc_users return [] if !self.cc_user_ids @cc_users ||= User.where(id: self.cc_user_ids) end |
#raw_body ⇒ Object
115 116 117 118 |
# File 'app/models/email_log.rb', line 115 def raw_body return if self.raw.blank? .body end |
#raw_headers ⇒ Object
110 111 112 113 |
# File 'app/models/email_log.rb', line 110 def raw_headers return if self.raw.blank? .header.raw_source end |
#topic ⇒ Object
51 52 53 |
# File 'app/models/email_log.rb', line 51 def topic @topic ||= self.topic_id.present? ? Topic.find_by(id: self.topic_id) : self.post&.topic end |