Class: WcoEmail::Message

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
Defined in:
app/models/wco_email/message.rb

Overview

When I receive one. 2023-12-28 vp Continue. 2026-03-27 vp continue

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.strip_emoji(text) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/wco_email/message.rb', line 135

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

.unreadObject



33
34
35
# File 'app/models/wco_email/message.rb', line 33

def self.unread
  where( read_at: nil )
end

Instance Method Details

#all_ccsObject



68
# File 'app/models/wco_email/message.rb', line 68

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

#apply_filter_action(action) ⇒ Object



86
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
# File 'app/models/wco_email/message.rb', line 86

def apply_filter_action action
  message = self
  aject = action.aject
  case action.kind
  when WcoEmail::EmailFilterAction::KIND_ADD_TAG
    conv.tags.push aject
    if aject == Wco::Tag.trash || aject == Wco::Tag.spam
      conv.tags -= [ Wco::Tag.inbox ]
    end

  when WcoEmail::EmailFilterAction::KIND_RM_TAG
    conv.tags -= [ aject ]

  when WcoEmail::EmailFilterAction::KIND_AUTORESPOND
    ctx = WcoEmail::Context.new({
      email_template: action.email_template,
      lead_id:        message.lead_id,
      send_at:        Time.now,
    })
    if action.email_template.respond_inline
      ctx.reply_to_message_id = self.id
    end
    ctx.save!

  when WcoEmail::EmailFilterAction::KIND_OAT
    config = {
      'email_message_id' => message.id.to_s,
      # 'lead_id'          => message.lead_id.to_s,
      # 'stub_id'          => message.stub_id.to_s,
    }
    oa = Wco::OfficeAction.create!({
      office_action_template: aject,
      lead:                   message.lead,
      status:                 'active',
      perform_at:             Time.now,
      config:                 config,
    })

  when WcoEmail::EmailFilterAction::KIND_RM_OAT
    oa = Wco::OfficeAction.where({
      office_action_template: aject,
      lead: message.lead }).first
    oa.update_attributes!({ status: 'inactive' })

  end
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"



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
# File 'app/models/wco_email/message.rb', line 172

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 = strip_emoji( 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



78
# File 'app/models/wco_email/message.rb', line 78

def conv ; conversation ; end

#object_keyObject

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



23
# File 'app/models/wco_email/message.rb', line 23

field     :object_key

#part_html_fully_sanitizedObject



50
51
52
# File 'app/models/wco_email/message.rb', line 50

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

#part_html_sanitizedObject

scope :unread, ->() { where( read_at: nil ) }



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/wco_email/message.rb', line 38

def part_html_sanitized
  doc = Nokogiri::HTML part_html
  images = doc.search('img')
  images.each do |img|
    img['src'] = 'missing'
  end
  doc.search('script').remove
  doc.search('meta').remove
  # doc.xpath('//@style').remove
  doc.to_s.gsub('http', '').gsub('href="s://', 'href="https://')
end

#preview_strObject



54
55
56
# File 'app/models/wco_email/message.rb', line 54

def preview_str
  part_html_fully_sanitized[0..200]
end

#received_atObject



75
# File 'app/models/wco_email/message.rb', line 75

def received_at ; date ; end

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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/models/wco_email/message.rb', line 228

def save_attachment att, filename: "no-filename-specified"
  config = JSON.parse(stub.config)
  if defined?( config['process_images'] ) &&
    false == config['process_images']
    return
  end

  content_type = att.content_type.split(';')[0]
  if content_type.include? 'image'
    photo = ::Wco::Photo.new({
      content_type:      content_type,
      email_message_id:  self.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 )
    sio = StringIO.new att.body.decoded
    File.open("/tmp/#{filename}", 'w:UTF-8:ASCII-8BIT') do |f|
      f.puts(sio.read)
    end
    asset = ::Wco::Asset.new({
      email_message: self,
      filename:      filename,
      object:        File.open("/tmp/#{filename}"),
    })
    if !asset.save
      self.logs.push "Could not save a wco asset: #{asset.errors.full_messages.join(", ")}"
      self.save
    end
  end
end

#save_attachment_postal(att) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/models/wco_email/message.rb', line 208

def save_attachment_postal att
  if att['content_type'].include? 'image'
    photo = ::Wco::Photo.new({
      content_type:      att['content_type'],
      email_message_id:  self.id,
      image_data:        att['data'],
      original_filename: att['filename'],
    })
    photo.decode_base64_image ## _TODO: why?
    puts! photo.save, 'saved image attachment?'
  else
    asset = ::Wco::Asset.new({
      email_message: self,
      filename:      att['filename'],
      object:        att['data'],
    })
    puts! asset.save, 'saved non-img asset?'
  end
end

#strip_emoji(text) ⇒ Object



156
157
158
# File 'app/models/wco_email/message.rb', line 156

def strip_emoji text
  WcoEmail::Message.strip_emoji text
end