Class: Mastiff::Email::Message

Inherits:
Object
  • Object
show all
Includes:
Redis::Objects
Defined in:
lib/mastiff/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Message

Returns a new instance of Message.


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
# 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

Instance Attribute Details

#attachment_analyzedObject

Returns the value of attribute attachment_analyzed.


9
10
11
# File 'lib/mastiff/message.rb', line 9

def attachment_analyzed
  @attachment_analyzed
end

#attachment_nameObject

Returns the value of attribute attachment_name.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def attachment_name
  @attachment_name
end

#attachment_sizeObject

Returns the value of attribute attachment_size.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def attachment_size
  @attachment_size
end

#busyObject

Returns the value of attribute busy.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def busy
  @busy
end

#has_attachmentsObject

Returns the value of attribute has_attachments.


9
10
11
# File 'lib/mastiff/message.rb', line 9

def has_attachments
  @has_attachments
end

#headerObject

Returns the value of attribute header.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def header
  @header
end

#mailboxObject

Returns the value of attribute mailbox.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def mailbox
  @mailbox
end

#raw_messageObject

Returns the value of attribute raw_message.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def raw_message
  @raw_message
end

#stored_filenameObject

Returns the value of attribute stored_filename.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def stored_filename
  @stored_filename
end

#uidObject

Returns the value of attribute uid.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def uid
  @uid
end

#uploaderObject

Returns the value of attribute uploader.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def uploader
  @uploader
end

#validity_idObject

Returns the value of attribute validity_id.


8
9
10
# File 'lib/mastiff/message.rb', line 8

def validity_id
  @validity_id
end

Class Method Details

.get(id) ⇒ Object


30
31
32
# File 'lib/mastiff/message.rb', line 30

def self.get(id)
  self.emails[id]
end

.idsObject


26
27
28
29
# 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

.validityObject


22
23
24
# File 'lib/mastiff/message.rb', line 22

def self.validity
  self.uid_validity.value.to_i
end

Instance Method Details

#<=>(other) ⇒ Object


48
49
50
# File 'lib/mastiff/message.rb', line 48

def <=> other
   self.id <=> other.id
end

#as_mailObject


63
64
65
# File 'lib/mastiff/message.rb', line 63

def as_mail
  Mail.new raw_source
end

#attached_file_pathObject


86
87
88
# File 'lib/mastiff/message.rb', line 86

def attached_file_path
  File.join @uploader.store_dir, @stored_filename
end

#busy?Boolean

Returns:

  • (Boolean)

33
34
35
# File 'lib/mastiff/message.rb', line 33

def busy?
  self.header[:busy]
end

#idObject


52
53
54
# File 'lib/mastiff/message.rb', line 52

def id
 "#{self.validity_id}:#{self.uid}"
end

#lockObject


42
43
44
# File 'lib/mastiff/message.rb', line 42

def lock
  @header[:busy] = true
end

#lock_and_saveObject


36
37
38
# File 'lib/mastiff/message.rb', line 36

def lock_and_save
  lock and save
end

#raw_sourceObject


56
57
58
59
60
61
62
# File 'lib/mastiff/message.rb', line 56

def raw_source
  if @raw_message.blank?
    self.class.raw[id]
  else
    @raw_message
  end
end

#saveObject


132
133
134
135
136
137
138
139
140
141
142
143
# 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_attachmentsObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

#unlockObject


45
46
47
# File 'lib/mastiff/message.rb', line 45

def unlock
  @header[:busy] = false
end

#unlock_and_saveObject


39
40
41
# File 'lib/mastiff/message.rb', line 39

def unlock_and_save
  unlock and save
end