Class: EmailLog

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/email_log.rb

Constant Summary collapse

SMTP_ERROR_CODE_REGEXP =
Regexp.new(/\d\.\d\.\d+|\d{3}/).freeze

Class Method Summary collapse

Instance Method Summary collapse

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_addressObject



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

Returns:

  • (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_messageObject



105
106
107
108
# File 'app/models/email_log.rb', line 105

def as_mail_message
  return if self.raw.blank?
  @mail_message ||= Mail.new(self.raw)
end

#bounce_keyObject



92
93
94
# File 'app/models/email_log.rb', line 92

def bounce_key
  super&.delete("-")
end

#cc_addresses_splitObject



101
102
103
# File 'app/models/email_log.rb', line 101

def cc_addresses_split
  @cc_addresses_split ||= self.cc_addresses&.split(";") || []
end

#cc_usersObject



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_bodyObject



115
116
117
118
# File 'app/models/email_log.rb', line 115

def raw_body
  return if self.raw.blank?
  as_mail_message.body
end

#raw_headersObject



110
111
112
113
# File 'app/models/email_log.rb', line 110

def raw_headers
  return if self.raw.blank?
  as_mail_message.header.raw_source
end

#topicObject



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