Class: WcoEmail::MessageStub

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

Overview

Only object_key, no validations. 2023-12-28 vp Continue. 2024-01-05 LFG 2026-07-10 LFG

Constant Summary collapse

PAGE_PARAM_NAME =
'stubs_page'
STATUS_PENDING =
'status_pending'
STATUS_PROCESSED =
'status_processed'
STATUS_FAILED =
'status_failed'
STATUSES =
[ STATUS_PENDING, STATUS_PROCESSED, STATUS_FAILED ]

Instance Method Summary collapse

Instance Method Details

#configObject

skip_notification process_images



45
46
47
# File 'app/models/wco_email/message_stub.rb', line 45

field :config, type: Object, default: <<~AOL
  {}
AOL

#do_process_jsonObject



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

def do_process_json
  stub = self
  @client ||= Aws::S3::Client.new(::SES_S3_CREDENTIALS)

  json = JSON.parse( @client.get_object( bucket: stub.bucket, key: stub.object_key ).body.read )
  subject = json['subject']
  message_id = json['message_id']

  ## Conversation
  if json['in_reply_to']
    in_reply_to_msg = WcoEmail::Message.where({ message_id: json['in_reply_to'] }).first
    if !in_reply_to_msg
      @conv = WcoEmail::Conversation.find_or_create_by({
        subject: subject,
      })
      in_reply_to_msg = WcoEmail::Message.find_or_create_by({
        message_id: json['in_reply_to'],
        conversation: @conv,
      })
    end
    @conv = in_reply_to_msg.conversation
  else
    @conv = WcoEmail::Conversation.unscoped.find_or_create_by({
      subject: subject,
    })
    @conv.deleted_at = nil
  end

  ## Leadset, Lead
  # from       = json['mail_from'] || "nobody@unknown-doma.in"
  from       = json['from'][/<([^>]+)>/, 1].downcase rescue json['mail_from']
  @lead      = Wco::Lead.find_or_create_by_email( from )
  @conv.leads.push @lead
  @leadset   = Wco::Leadset.from_email from
  @conv.leadsets.push @leadset

  ## message
  old_message   = WcoEmail::Message.unscoped.where( message_id: message_id ).first
  old_message.destroy! if old_message
  @message = WcoEmail::Message.create!({
    stub:         stub,
    conversation: @conv,
    lead:         @lead,

    message_id:     message_id,
    in_reply_to_id: json['in_reply_to'],
    object_key:     stub.object_key,

    subject: subject,
    date:    json['date'].to_s,

    from: from,
    to:   json['to'],
    cc:   json['cc'],

    part_html: json['html_body'],
    part_txt:  json['plain_body'],
  })

  ## Attachments
  json['attachments'].each do |att|
    @message.save_attachment_postal( att )
  end

  ## _TODO
  # the_mail.cc&.each do |cc|
  #   Wco::Lead.find_or_create_by_email( cc )
  # end

  @conv.update_attributes({
    status:      WcoEmail::Conversation::STATUS_UNREAD,
    latest_at:   json['date'].to_time.to_s || Time.now.to_datetime,
    from_emails: ( @conv.from_emails + [ from ]).uniq,
    preview:     @message.preview_str,
  })

  ## tags
  @conv.tags.push Wco::Tag.inbox
  @conv.tags.push stub.tags
  @conv.save


  ## Actions & Filters
  email_filters = WcoEmail::EmailFilter.all.active
  email_filters.each do |filter|
    reason = nil

    filter.conditions.each do |cond|
      reason ||= cond.apply(leadset: @leadset, message: @message )
    end

    if reason
      puts! "Applying2 filter #{filter} to conv #{@message.conversation} for matching #{reason}" if DEBUG

      ## skip
      skip_reason = nil

      filter.skip_conditions.each do |scond|
        skip_reason ||= scond.apply(leadset: @leadset, message: @message )
      end

      if skip_reason
        puts! "NOT Applying2 filter #{filter} to conv #{@message.conversation} for matching #{skip_reason}" if DEBUG
      else
        @conv.filters << filter; @conv.save
        filter.actions.each do |action|
          @message.apply_filter_action( action )
        end
      end
    end
  end

  stub.update_attributes({ status: WcoEmail::MessageStub::STATUS_PROCESSED })

  ## Notification
  ## _TODO
end