Class: Ishapi::EmailMessageIntakeJob
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- Ishapi::EmailMessageIntakeJob
- Defined in:
- app/jobs/ishapi/email_message_intake_job.rb
Overview
2023-02-26 vp let’s go 2023-03-02 vp Continue 2023-03-07 vp Continue
Instance Method Summary collapse
-
#churn_subpart(message, part) ⇒ Object
For recursive parts of type ‘related`.
- #perform(id) ⇒ Object
Instance Method Details
#churn_subpart(message, part) ⇒ Object
For recursive parts of type ‘related`. Content Types: “text/html; charset=utf-8” “application/pdf; name="Securities Forward Agreement – HaulHub Inc – Victor Pudeyev – 2021-10-26.docx.pdf"” “image/jpeg; name=TX_DL_2.jpg” “text/plain; charset=UTF-8”
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/jobs/ishapi/email_message_intake_job.rb', line 29 def churn_subpart , part if part.content_type.include?("multipart/related") part.parts.each do |subpart| churn_subpart( , subpart ) end elsif part.content_type.include?('text/html') .part_html = part.decoded elsif part.content_type.include?("text/plain") .part_txt = part.decoded else ## @TODO: attachments ! puts! part.content_type, '444 No action for a part with this content_type' end return nil end |
#perform(id) ⇒ Object
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 |
# File 'app/jobs/ishapi/email_message_intake_job.rb', line 50 def perform id stub = ::Office::EmailMessageStub.find id puts "Performing EmailMessageIntakeJob for message_id #{stub.object_key}" 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], access_key_id: ::S3_CREDENTIALS[:access_key_id], secret_access_key: ::S3_CREDENTIALS[:secret_access_key] }) _mail = client.get_object( bucket: ::S3_CREDENTIALS[:bucket_ses], key: stub.object_key ).body.read the_mail = Mail.new(_mail) = the_mail.header['message-id'].decoded ## == stub.object_key in_reply_to_id = the_mail.header['in-reply-to']&.to_s = ::Office::EmailMessage.where( message_id: ).first ||= ::Office::EmailMessage.new .assign_attributes({ raw: _mail, message_id: , in_reply_to_id: in_reply_to_id, object_key: stub.object_key, object_path: stub.object_path, subject: the_mail.subject, date: the_mail.date, from: the_mail.from[0], froms: the_mail.from, to: the_mail.to[0], tos: the_mail.to, ccs: the_mail.cc, bccs: the_mail.bcc, }) the_mail.parts.each do |part| churn_subpart( , part ) 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.create!({ subject: the_mail.subject, latest_at: the_mail.date, }) in_reply_to_msg = ::Office::EmailMessage.create!({ message_id: in_reply_to_id, email_conversation_id: conv.id, }) end conv = in_reply_to_msg.email_conversation else conv = ::Office::EmailConversation.create!({ subject: the_mail.subject, latest_at: the_mail.date, }) end .email_conversation_id = conv.id conv.update_attributes({ state: Conv::STATE_UNREAD, latest_at: the_mail.date, term_ids: (conv.term_ids + stub.term_ids).uniq, }) ## Leadset domain = .from.split('@')[1] leadset = Leadset.where( company_url: domain ).first if !leadset leadset = Leadset.create!( company_url: domain, name: domain ) end ## Lead lead = Lead.where( email: .from ).first if !lead lead = Lead.new( email: .from ) lead.leadsets.push( leadset ) lead.save! end conv.lead_ids = conv.lead_ids.push( lead.id ).uniq ## Actions & Filters inbox_tag = WpTag.email_inbox_tag .add_tag( inbox_tag ) conv.add_tag( inbox_tag ) email_filters = Office::EmailFilter.active email_filters.each do |filter| if .from.match( filter.from_regex ) # || @message.part_html.match( filter.body_regex ) ) # || MiaTagger.analyze( @message.part_html, :is_spammy_recruite ).score > .5 .apply_filter( filter ) end end ## Save to exit flag = .save if flag puts! ., 'Saved this message' else puts! .errors..join(', '), 'Cannot save email_message' end conv.save stub.update_attributes({ state: ::Office::EmailMessageStub::STATE_PROCESSED }) ## Notification if .wp_term_ids.include?( inbox_tag.id ) # @TODO: send android notification _vp_ 2023-03-01 ::Ishapi::ApplicationMailer.forwarder_notify( .id.to_s ).deliver_later end end |