Class: Ishapi::EmailMessageIntakeJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/ishapi/email_message_intake_job.rb

Overview

2023-02-26 vp Let’s go 2023-03-07 vp Continue

class name: EIJ

Instance Method Summary collapse

Instance Method Details

#perform(id) ⇒ Object

object_key = ‘n0v5mg6q1t4fjjnfh8vj8a96t85rp9la2ud0gdg1’

MsgStub.where({ object_key: object_key }).delete

stub = MsgStub.create!({ object_key: object_key })
id = stub.id

Ishapi::EmailMessageIntakeJob.perform_now( stub.id.to_s )


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
# File 'app/jobs/ishapi/email_message_intake_job.rb', line 23

def perform id
    stub = ::Office::EmailMessageStub.find id
    if !Rails.env.test?
      puts "Performing EmailMessageIntakeJob for object_key #{stub.object_key}"
    end
    if stub.state != ::Office::EmailMessageStub::STATE_PENDING
      raise "This stub has already been processed: #{stub.id.to_s}."
      return
    end

    client = Aws::S3::Client.new({
      region:            ::S3_CREDENTIALS[:region_ses],
      access_key_id:     ::S3_CREDENTIALS[:access_key_id_ses],
      secret_access_key: ::S3_CREDENTIALS[:secret_access_key_ses],
    })

    raw                = client.get_object( bucket: ::S3_CREDENTIALS[:bucket_ses], key: stub.object_key ).body.read
    the_mail           = Mail.new( raw )
    message_id         = the_mail.header['message-id'].decoded
    in_reply_to_id     = the_mail.header['in-reply-to']&.to_s
    email_inbox_tag_id = WpTag.emailtag(WpTag::INBOX).id

    if !the_mail.to
      the_mail.to = [ 'NO-RECIPIENT' ]
    end


    subject   = ::Msg.strip_emoji the_mail.subject
    subject ||= '(wco no subject)'

    @message   = ::Office::EmailMessage.where( message_id: message_id ).first
    @message ||= ::Office::EmailMessage.create({
      raw: raw,

      message_id:     message_id,
      in_reply_to_id: in_reply_to_id,

      object_key:  stub.object_key,
      # object_path: stub.object_path,

      subject: subject,
      date:    the_mail.date,

      from:  the_mail.from ? the_mail.from[0] : "[email protected]",
      froms: the_mail.from,

      to:  the_mail.to ? the_mail.to[0] : nil,
      tos: the_mail.to,

      cc:  the_mail.cc ? the_mail.cc[0] : nil,
      ccs:  the_mail.cc,

      # bccs: the_mail.bcc,
    })
    if !@message.persisted?
      throw "Could not create email_message: #{@message.errors.full_messages.join(', ')} ."
    end

    ## Parts
    the_mail.parts.each do |part|
      @message.churn_subpart( part )
    end

    if the_mail.parts.length == 0
      body = the_mail.body.decoded.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
      if the_mail.content_type&.include?('text/html')
        @message.part_html = body
      elsif the_mail.content_type&.include?('text/plain')
        @message.part_txt = body
      elsif the_mail.content_type.blank?
        @message.part_txt = body
      else
        @message.logs.push "mail body of unknown type: #{the_mail.content_type}"
      end
    end

    ## Attachments
    the_mail.attachments.each do |att|
      content_type = att.content_type.split(';')[0]
      if content_type.include? 'image'
        photo = Photo.new({
          content_type:      content_type,
          original_filename: att.content_type_parameters[:name],
          image_data:        att.body.encoded,
          email_message_id: @message.id,
        })
        photo.decode_base64_image
        photo.save
      elsif att.filename
        filename = CGI.escape( att.filename )

        attachment = Office::EmailAttachment.new({
          content:       att.body.decoded,
          content_type:  att.content_type,
          email_message: @message,
          filename:      filename,
        })
        if !attachment.save
          @message.logs.push "Could not save an attachment"
        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({
          object: File.open("/tmp/#{filename}"),
          email_message: @message,
        })
        if !asset3d.save
          @message.logs.push "Could not save an asset3d"
        end

      else
        @message.logs.push "Has an attachment with no filename?"
      end
    end

    ## Leadset, Lead
    domain  = @message.from.split('@')[1] rescue 'unknown.domain'
    leadset = Leadset.find_or_create_by( company_url: domain )
    lead    = Lead.find_or_create_by( email: @message.from, m3_leadset_id: leadset.id )
    the_mail.cc&.each do |cc|
      domain  = cc.split('@')[1] rescue 'unknown.domain'
      leadset = Leadset.find_or_create_by( company_url: domain )
      Lead.find_or_create_by( email: cc, m3_leadset_id: leadset.id )
    end

    ## Conversation
    if in_reply_to_id
      in_reply_to_msg = ::Office::EmailMessage.where({ message_id: in_reply_to_id }).first
      if !in_reply_to_msg
        conv = ::Office::EmailConversation.find_or_create_by({
          subject: @message.subject,
        })
        in_reply_to_msg = ::Office::EmailMessage.find_or_create_by({
          message_id: in_reply_to_id,
          email_conversation_id: conv.id,
        })
      end
      conv = in_reply_to_msg.email_conversation
    else
      conv = ::Office::EmailConversation.find_or_create_by({
        subject: @message.subject,
      })
    end
    @message.update_attributes({ email_conversation_id: conv.id })
    conv.update_attributes({
      state:       Conv::STATE_UNREAD,
      latest_at:   the_mail.date || Time.now.to_datetime,
      from_emails: ( conv.from_emails + the_mail.from ).uniq,
      preview: @message.body_sanitized[0...200],
    })
    conv.add_tag( ::WpTag::INBOX )
    conv_lead_tie = Office::EmailConversationLead.find_or_create_by({
      lead_id: lead.id,
      email_conversation_id: conv.id,
    })


    ## Actions & Filters
    email_filters = Office::EmailFilter.active
    email_filters.each do |filter|
      if ( filter.from_regex.blank? ||     @message.from.match(                 filter.from_regex    ) ) &&
        ( filter.from_exact.blank? ||     @message.from.downcase.include?(     filter.from_exact&.downcase ) ) &&
        ( filter.body_exact.blank? ||     @message.part_html&.include?(         filter.body_exact    ) ) &&
        ( filter.subject_regex.blank? ||  @message.subject.match(              filter.subject_regex ) ) &&
        ( filter.subject_exact.blank? ||  @message.subject.downcase.include?(  filter.subject_exact&.downcase ) )

        # || MiaTagger.analyze( @message.part_html, :is_spammy_recruite ).score > .5

        puts! "applying filter #{filter} to conv #{conv}" if DEBUG

        @message.apply_filter( filter )
      end
    end

    stub.update_attributes({ state: ::Office::EmailMessageStub::STATE_PROCESSED })

    ## Notification
    conv = Conv.find( conv.id )
    if conv.in_emailtag? WpTag::INBOX
      out = ::Ishapi::ApplicationMailer.forwarder_notify( @message.id.to_s )
      Rails.env.production? ? out.deliver_later : out.deliver_now
    end

end