Class: Office::EmailMessage

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/office/email_message.rb

Overview

When I receive one.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.strip_emoji(text) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/office/email_message.rb', line 102

def self.strip_emoji(text)
  text = '' if text.blank?
  text = text.force_encoding('utf-8').encode
  clean = ""

  # symbols & pics
  regex = /[\u{1f300}-\u{1f5ff}]/
  clean = text.gsub regex, ""

  # enclosed chars
  regex = /[\u{2500}-\u{2BEF}]/ # I changed this to exclude chinese char
  clean = clean.gsub regex, ""

  # emoticons
  regex = /[\u{1f600}-\u{1f64f}]/
  clean = clean.gsub regex, ""

  #dingbats
  regex = /[\u{2702}-\u{27b0}]/
  clean = clean.gsub regex, ""
end

Instance Method Details

#all_ccsObject



40
# File 'lib/office/email_message.rb', line 40

def all_ccs; (tos||[]) + (ccs||[]) + (froms||[]); end

#apply_filter(filter) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/office/email_message.rb', line 62

def apply_filter filter
  case filter.kind

  when ::Office::EmailFilter::KIND_DESTROY_SCHS
    conv.add_tag    ::WpTag::TRASH
    conv.remove_tag ::WpTag::INBOX
    lead.schs.each do |sch|
      sch.update_attributes({ state: ::Sch::STATE_TRASH })
    end

  when ::Office::EmailFilter::KIND_ADD_TAG
    conv.add_tag filter.wp_term_id
    if ::WpTag::TRASH == ::WpTag.find( filter.wp_term_id ).slug
      conv.remove_tag ::WpTag::INBOX
    end

  when ::Office::EmailFilter::KIND_REMOVE_TAG
    conv.remove_tag filter.wp_term_id

  when ::Office::EmailFilter::KIND_AUTORESPOND_TMPL
    Ish::EmailContext.create({
      email_template: filter.email_template,
      lead_id:        lead.id,
      send_at:        Time.now + 22.minutes,
    })

  when ::Office::EmailFilter::KIND_AUTORESPOND_EACT
    ::Sch.create({
      email_action: filter.email_action,
      state:        ::Sch::STATE_ACTIVE,
      lead_id:      lead.id,
      perform_at:   Time.now + 22.minutes,
    })

  else
    raise "unknown filter kind: #{filter.kind}"
  end
end

#body_sanitizedObject



216
217
218
# File 'lib/office/email_message.rb', line 216

def body_sanitized
  ActionView::Base.full_sanitizer.sanitize( part_html||'' ).squish
end

#churn_subpart(part) ⇒ Object

For recursive parts of type ‘related`. Content dispositions: “inline; creation-date="Tue, 11 Apr 2023 19:39:42 GMT"; filename=image005.png; modification-date="Tue, 11 Apr 2023 19:47:53 GMT"; size=14916”,

Content Types: “application/pdf; name="Securities Forward Agreement – HaulHub Inc – Victor Pudeyev – 2021-10-26.docx.pdf"” “image/jpeg; name=TX_DL_2.jpg” “image/png; name=image005.png” “multipart/alternative; boundary=000_BL0PR10MB2913C560ADE059F0AB3A6D11829A9BL0PR10MB2913namp”, “text/html; charset=utf-8” “text/plain; charset=UTF-8” “text/calendar; charset=utf-8; method=REQUEST”



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/office/email_message.rb', line 136

def churn_subpart part
  if part.content_disposition&.include?('attachment')
    save_attachment( part, filename: "subpart-attachment" )
  else
    if part.content_type.include?("multipart")
      part.parts.each do |subpart|
        churn_subpart( subpart )
      end
    else
      if part.content_type.include?('text/html')
        self.part_html = part.decoded

      elsif part.content_type.include?("text/plain")
        self.part_txt = part.decoded

      elsif part.content_type.include?("text/calendar")
        save_attachment( part, filename: 'subpart-calendar.ics' )

      elsif part.content_type.include?("application/pdf")
        save_attachment( part, filename: 'subpart.pdf' )

      elsif part.content_type.include?("image/jpeg")
        save_attachment( part, filename: 'subpart.jpg' )

      elsif part.content_type.include?("image/png")
        save_attachment( part, filename: 'subpart.png' )

      else
        save_attachment( part, filename: 'subpart-unspecified' )
        self.logs.push "444 No action for a part with content_type #{part.content_type}"

      end
    end
  end
end

#convObject



52
53
54
# File 'lib/office/email_message.rb', line 52

def conv
  email_conversation
end

#leadObject



30
31
32
# File 'lib/office/email_message.rb', line 30

def lead
  Lead.find_by email: from
end

#object_keyObject

aka ‘filename’, use with bucket name + prefix. I need this!



22
# File 'lib/office/email_message.rb', line 22

field :object_key,  type: :string

#preview_strObject



219
220
221
# File 'lib/office/email_message.rb', line 219

def preview_str
  body_sanitized[0..200]
end

#received_atObject



47
48
49
# File 'lib/office/email_message.rb', line 47

def received_at
  date
end

#save_attachment(att, filename: "no-filename-specified") ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/office/email_message.rb', line 172

def save_attachment att, filename: "no-filename-specified"
  content_type = att.content_type.split(';')[0]
  if content_type.include? 'image'
    photo = Photo.new({
      content_type:      content_type,
      email_message_id:  @message.id,
      image_data:        att.body.encoded,
      original_filename: att.content_type_parameters[:name],
    })
    photo.decode_base64_image
    photo.save
  else

    filename   = CGI.escape( att.filename || filename )
    attachment = Office::EmailAttachment.new({
      content:       att.body.decoded,
      content_type:  att.content_type,
      email_message: @message,
      filename:      filename,
    })
    begin
      attachment.save
    rescue Encoding::UndefinedConversionError
      @message.logs.push "Could not save an attachment"
      @message.save
    end

    sio = StringIO.new att.body.decoded
    File.open("/tmp/#{filename}", 'w:UTF-8:ASCII-8BIT') do |f|
      f.puts(sio.read)
    end
    asset3d = ::Gameui::Asset3d.new({
      email_message: @message,
      filename:      filename,
      object:        File.open("/tmp/#{filename}"),
    })
    if !asset3d.save
      @message.logs.push "Could not save an asset3d"
      @message.save
    end

  end
end

#the_mailObject



12
13
14
# File 'lib/office/email_message.rb', line 12

def the_mail
  Mail.new( raw )
end