Class: DiscourseDev::EmailLog

Inherits:
Record
  • Object
show all
Defined in:
lib/discourse_dev/email_log.rb

Constant Summary

Constants inherited from Record

Record::AUTO_POPULATED, Record::DEFAULT_COUNT

Instance Attribute Summary

Attributes inherited from Record

#model, #type

Instance Method Summary collapse

Methods inherited from Record

#create!, #current_count, populate!, random

Constructor Details

#initializeEmailLog

Returns a new instance of EmailLog.



8
9
10
# File 'lib/discourse_dev/email_log.rb', line 8

def initialize
  super(::EmailLog, DiscourseDev.config.email_logs[:count])
end

Instance Method Details

#create_bounced!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/discourse_dev/email_log.rb', line 16

def create_bounced!
  bounce_key = SecureRandom.hex
  email_local_part, email_domain = SiteSetting.notification_email.split("@")
  bounced_to_address = "#{email_local_part}+verp-#{bounce_key}@#{email_domain}"
  bounce_data =
    email_log_data.merge(
      to_address: bounced_to_address,
      bounced: true,
      bounce_key: bounce_key,
      bounce_error_code: "5.0.0",
    )

  # Bounced email logs require a matching incoming email record
  ::IncomingEmail.create!(
    incoming_email_data.merge(to_addresses: bounced_to_address, is_bounce: true),
  )
  ::EmailLog.create!(bounce_data)
end

#create_rejected!Object



35
36
37
# File 'lib/discourse_dev/email_log.rb', line 35

def create_rejected!
  ::IncomingEmail.create!(incoming_email_data)
end

#create_sent!Object



12
13
14
# File 'lib/discourse_dev/email_log.rb', line 12

def create_sent!
  ::EmailLog.create!(email_log_data)
end

#email_log_dataObject



39
40
41
42
43
44
45
46
# File 'lib/discourse_dev/email_log.rb', line 39

def email_log_data
  {
    to_address: User.random.email,
    email_type: :digest,
    user_id: User.random.id,
    raw: Faker::Lorem.paragraph,
  }
end

#incoming_email_dataObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/discourse_dev/email_log.rb', line 48

def incoming_email_data
  user = User.random
  subject = Faker::Lorem.sentence
  email_content = <<-EMAIL
    Return-Path: #{user.email}
    From: #{user.email}
    Date: #{Date.today}
    Mime-Version: "1.0"
    Content-Type: "text/plain"
    Content-Transfer-Encoding: "7bit"

    #{Faker::Lorem.paragraph}
  EMAIL

  {
    user_id: user.id,
    from_address: user.email,
    raw: email_content,
    error: Faker::Lorem.sentence,
    rejection_message: I18n.t("emails.incoming.errors.bounced_email_error"),
  }
end

#populate!Object



71
72
73
74
75
# File 'lib/discourse_dev/email_log.rb', line 71

def populate!
  @count.times { create_sent! }
  @count.times { create_bounced! }
  @count.times { create_rejected! }
end