class Mastiff::Email::Message

Attributes

attachment_analyzed[RW]
attachment_name[RW]
attachment_size[RW]
busy[RW]
has_attachments[RW]
header[RW]
mailbox[RW]
raw_message[RW]
stored_filename[RW]
uid[RW]
uploader[RW]
validity_id[RW]

Public Class Methods

get(id) click to toggle source
# File lib/mastiff/message.rb, line 30
def self.get(id)
  self.emails[id]
end
ids() click to toggle source
# File lib/mastiff/message.rb, line 26
def self.ids
  prefix = validity
  self.emails.keys.select{|k| /#{prefix}:/ =~ k }.map{|id| id.split(':').last.to_i}.sort
end
new(attrs = {}) click to toggle source
# File lib/mastiff/message.rb, line 89
def initialize(attrs = {})
  attrs.deep_symbolize_keys!
  @uid           = attrs[:uid]
  @validity_id   = attrs[:validity_id]
  @raw_message   = attrs[:raw_message]
  # @mail_message  = attrs[:mail_message]
  mail_message = as_mail
  if mail_message and mail_message.has_attachments?
    @has_attachments = true
    attachment = mail_message.attachments[0]
    @attachment_name  = attachment.filename
    @attachment_size  = 0
    @stored_filename = @attachment_name.squish.gsub(" ", "_")
    @uploader  = Mastiff.attachment_uploader.new

    self.class.pending_attachments << id
    @attachment_analyzed = false
    #decoded           = attachment.body.decoded
    #@attachment_size  = decoded.length
  end
  if attrs[:header].is_a? Hash
    @header = attrs[:header]
  end
  if  mail_message and @header.blank?
    @header  = {
      id: id,
      busy: false,
      from: mail_message[:from].display_names.first,
      sender_email: mail_message.from.first,
      subject: mail_message.subject,
      date: mail_message.date,
      attachment_name: @attachment_name,
      attachment_size: @attachment_size,
      stored_filename: @stored_filename,
    }
  elsif mail_message and not @header.blank?
    # TODO: Change this to a use a gem logger
    Sidekiq::Logging.logger.info "Header already existed"
  end
end
validity() click to toggle source
# File lib/mastiff/message.rb, line 22
def self.validity
  self.uid_validity.value.to_i
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/mastiff/message.rb, line 48
def <=> other
   self.id <=> other.id
end
as_mail() click to toggle source
# File lib/mastiff/message.rb, line 63
def as_mail
  Mail.new raw_source
end
attached_file_path() click to toggle source
# File lib/mastiff/message.rb, line 86
def attached_file_path
  File.join @uploader.store_dir, @stored_filename
end
busy?() click to toggle source
# File lib/mastiff/message.rb, line 33
def busy?
  self.header[:busy]
end
id() click to toggle source
# File lib/mastiff/message.rb, line 52
def id
 "#{self.validity_id}:#{self.uid}"
end
lock() click to toggle source
# File lib/mastiff/message.rb, line 42
def lock
  @header[:busy] = true
end
lock_and_save() click to toggle source
# File lib/mastiff/message.rb, line 36
def lock_and_save
  lock and save
end
raw_source() click to toggle source
# File lib/mastiff/message.rb, line 56
def raw_source
  if @raw_message.blank?
    self.class.raw[id]
  else
    @raw_message
  end
end
save() click to toggle source
# File lib/mastiff/message.rb, line 132
def save
  mail_message = as_mail

  unless mail_message.blank?
    lock if @has_attachments and not @attachment_analyzed
    self.class.raw[id]    = self.raw_message
  end

  self.class.emails[id] = self
  Mastiff.sync_attachment_worker.perform_async(id) if @has_attachments and not @attachment_analyzed

end
sync_message_attachments() click to toggle source
# File lib/mastiff/message.rb, line 66
def sync_message_attachments
  mail_message = as_mail

  if mail_message and mail_message.has_attachments? and not @attachment_analyzed
            attachment = mail_message.attachments[0]
            decoded           = attachment.body.decoded
            @attachment_name  = attachment.filename
            @attachment_size  = decoded.length
            @header[:attachment_size] = @attachment_size
            @stored_filename = @attachment_name.squish.gsub(" ", "_")
            @uploader.store_mime(@stored_filename,decoded)
            @attachment_analyzed = true
            self.class.pending_attachments.delete(id)
            unlock
            save
            Mastiff.process_attachment_worker.perform_async(id)

  end
end
unlock() click to toggle source
# File lib/mastiff/message.rb, line 45
def unlock
  @header[:busy] = false
end
unlock_and_save() click to toggle source
# File lib/mastiff/message.rb, line 39
def unlock_and_save
  unlock and save
end