Class: Nuntius::Message

Inherits:
ApplicationRecord show all
Includes:
Concerns::MetadataScoped
Defined in:
app/models/nuntius/message.rb

Overview

Stores individual messages to individual recipients

Nuntius will have messages in states:

pending - nothing done yet
sent - we've sent it on to the provider
delivered - have delivery confirmation
undelivered - have confirmation of non-delivery

Not all transports may provide all states

Instance Method Summary collapse

Instance Method Details

#add_attachment(options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/nuntius/message.rb', line 87

def add_attachment(options)
  attachment = {}

  uri = options[:url] && URI.parse(options[:url])

  if uri&.scheme == "file"
    # FIXME: This is a possible security problem
    attachment[:io] = File.open(uri.path)
  elsif uri
    client = Faraday.new do |builder|
      builder.response :follow_redirects
      builder.adapter Faraday.default_adapter
    end

    response = client.get(options[:url])
    content_disposition = response.headers["Content-Disposition"] || ""

    options[:filename] ||= content_disposition[/filename="([^"]+)"/, 1]
    attachment[:content_type] = response.headers["Content-Type"]
    attachment[:io] = if response.body.is_a? String
      StringIO.new(response.body)
    else
      # Assume IO object
      response.body
    end
  elsif options[:content].respond_to?(:read)
    attachment[:content_type] = options[:content_type]
    attachment[:io] = options[:content]
  else
    raise "Cannot add attachment without url or content"
  end

  # Set the filename
  attachment[:filename] = options[:filename] || uri.path.split("/").last || "attachment"

  # (Try to) add file extension if it is missing
  file_extension = File.extname(attachment[:filename]).delete(".")
  attachment[:filename] += ".#{Mime::Type.lookup(attachment[:content_type].split(";").first).to_sym}" if file_extension.blank? && attachment[:content_type]

  # Fix content type if file extension known but content type blank
  attachment[:content_type] ||= Mime::Type.lookup_by_extension(file_extension)&.to_s if file_extension

  if options[:auto_zip] && attachment[:io].size > 1024 * 1024
    zip_stream = Zip::OutputStream.write_buffer do |zio|
      zio.put_next_entry attachment[:file_name]
      zio.write attachment[:io].read
    end
    attachment[:content_type] = "application/zip"
    attachment[:io] = zip_stream
  end

  nuntius_attachment = Nuntius::Attachment.new
  nuntius_attachment.content.attach(io: attachment[:io],
    filename: attachment[:filename],
    content_type: attachment[:content_type])

  attachments.push(nuntius_attachment)
rescue => e
  Nuntius.config.logger.error "Message: Could not attach #{attachment[:filename]} #{e.message}"
end

#cleanup!Object

Removes only pending child messages



83
84
85
# File 'app/models/nuntius/message.rb', line 83

def cleanup!
  Nuntius::Message.where(state: "pending").where(parent_message: self).destroy_all
end

#cleanup_attachmentsObject



148
149
150
151
152
# File 'app/models/nuntius/message.rb', line 148

def cleanup_attachments
  attachments.each do |attachment|
    attachment.destroy if attachment.messages.where.not(id: id).blank?
  end
end

#clicked?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/nuntius/message.rb', line 78

def clicked?
  clicked_at.present?
end

#deliver_as(transport) ⇒ Object

Convenience method to easily send messages without having a template



170
171
172
173
174
# File 'app/models/nuntius/message.rb', line 170

def deliver_as(transport)
  klass = BaseTransport.class_from_name(transport).new
  klass.deliver(self)
  self
end

#delivered_or_blocked?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/nuntius/message.rb', line 70

def delivered_or_blocked?
  delivered? || blocked?
end

Returns:

  • (Boolean)


176
177
178
# File 'app/models/nuntius/message.rb', line 176

def link_tracking_enabled?
  template&.link_tracking? || campaign&.link_tracking?
end

#nuntius_provider(message) ⇒ Object



154
155
156
157
158
# File 'app/models/nuntius/message.rb', line 154

def nuntius_provider(message)
  klass = Nuntius::BaseProvider.class_from_name(provider, transport)
  klass ||= Nuntius::BaseProvider
  klass.new(message)
end

#open_tracking_enabled?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'app/models/nuntius/message.rb', line 180

def open_tracking_enabled?
  template&.open_tracking? || campaign&.open_tracking?
end

#opened?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/nuntius/message.rb', line 74

def opened?
  opened_at.present?
end

#resendObject



160
161
162
163
164
165
# File 'app/models/nuntius/message.rb', line 160

def resend
  return if pending?
  return unless transport

  deliver_as(transport)
end