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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.strip_emoji(text) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/wco_email/message.rb', line 154

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



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

def self.unread
  where( read_at: nil )
end

Instance Method Details

#all_ccsObject



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

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

#apply_filter(filter) ⇒ Object



85
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/wco_email/message.rb', line 85

def apply_filter filter
  puts! filter, 'WcoEmail::Message#apply_filter' if DEBUG
  conv.filter = filter

  case filter.kind

  when WcoEmail::EmailFilter::KIND_DESTROY_SCHS
    conv.tags.push   Wco::Tag.trash
    conv.tags -= [ Wco::Tag.inbox ]
    lead.schs.each do |sch|
      sch.update_attributes!({ state: ::Sch::STATE_TRASH })
    end

  when WcoEmail::EmailFilter::KIND_ADD_TAG
    conv.tags.push filter.tag
    if filter.tag == Wco::Tag.trash || filter.tag == Wco::Tag.spam
      conv.tags -= [ Wco::Tag.inbox ]
    end

  when WcoEmail::EmailFilter::KIND_REMOVE_TAG
    conv.tags -= [ filter.tag ]

  when WcoEmail::EmailFilter::KIND_AUTORESPOND_TMPL
    WcoEmail::Context.create!({
      email_template: filter.email_template,
      lead_id:        lead.id,
      send_at:        Time.now,
    })

  when WcoEmail::EmailFilter::KIND_AUTORESPOND_EACT
    ##
    ## This error is normal:
    ## Mongoid::Errors::Validations `Email action template is already taken`
    ##
    out = Sch.create({
      email_action_template: filter.email_action_template,
      status:                Sch::STATUS_ACTIVE,
      lead_id:               lead.id,
      perform_at:            Time.now,
    })

  else
    if filter.actions.present?
      filter.actions.each do |act|
        case act.kind
        when ::WcoEmail::ACTION_REMOVE_TAG
          this_tag = Wco::Tag.find( act.value )
          conv.tags -= [ this_tag ]
        when ::WcoEmail::ACTION_ADD_TAG
          this_tag = Wco::Tag.find( act.value )
          conv.tags += [ this_tag ]
        when ::WcoEmail::ACTION_AUTORESPOND
          this_template = WcoEmail::EmailTemplate.find( act.value )
          WcoEmail::Context.create!({
            email_template: this_template,
            lead_id:        lead.id,
            send_at:        Time.now,
          })
        end
      end
    else
      raise "unknown filter kind: #{filter.kind}"
    end
  end

  conv.save!
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”



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/models/wco_email/message.rb', line 191

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



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

def conv ; conversation ; end

#object_keyObject

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



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

field     :object_key

#part_html_fully_sanitizedObject



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

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

#part_html_sanitizedObject

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



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

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



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

def preview_str
  part_html_fully_sanitized[0..200]
end

#received_atObject



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

def received_at ; date ; end

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



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

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

#strip_emoji(text) ⇒ Object



175
176
177
# File 'app/models/wco_email/message.rb', line 175

def strip_emoji text
  WcoEmail::Message.strip_emoji text
end