Skip to content

Commit e67630e

Browse files
committed
Store original reply-to message ids in our database
Sometimes messages to the mailing list appear out of order (replies appear to messages that weren't approved by the list yet by spam filters), causing lookup/mapping issues. While we can't fix the problem immediately, we can save enough information to reconciele the issue later - that part is not implemented yet in this commit, only the information gathering part, as we'll also have to update all previously imported mbox records.
1 parent 3975371 commit e67630e

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

app/services/email_ingestor.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ def ingest_raw(raw_message, fallback_threading: false, trust_date: false, update
1010
sent_at = trust_date ? m.date : sanitize_email_date(m.date, m[:date], message_id)
1111

1212
body = normalize_body(extract_body(m))
13+
reply_to_message_id = m.in_reply_to ? clean_reference(m.in_reply_to) : nil
14+
1315
existing_message = Message.find_by_message_id(message_id)
1416
if existing_message
15-
update_existing_message(existing_message, body: body, sent_at: sent_at, update_existing: update_existing)
17+
update_existing_message(existing_message, body: body, sent_at: sent_at,
18+
reply_to_message_id: reply_to_message_id, update_existing: update_existing)
1619
return existing_message
1720
end
1821

@@ -24,7 +27,7 @@ def ingest_raw(raw_message, fallback_threading: false, trust_date: false, update
2427

2528
subject = m.subject || 'No title'
2629

27-
reply_to_msg, import_log = resolve_threading(m, import_log)
30+
reply_to_msg, import_log = resolve_threading(m, reply_to_message_id, import_log)
2831
if fallback_threading && reply_to_msg.nil? && subject.present? && subject.match?(/\A\s*(re|aw|fwd):/i)
2932
reply_to_msg = fallback_thread_lookup(subject, message_id: message_id, references: m.references, sent_at: sent_at)
3033
import_log = [import_log, "Resolved by subject fallback"].reject(&:blank?).join(" | ") if reply_to_msg
@@ -43,6 +46,7 @@ def ingest_raw(raw_message, fallback_threading: false, trust_date: false, update
4346
sender: from[0],
4447
sender_person_id: from[0].person_id,
4548
reply_to: reply_to_msg,
49+
reply_to_message_id: reply_to_message_id,
4650
subject: subject,
4751
body: body,
4852
created_at: sent_at,
@@ -64,7 +68,7 @@ def ingest_raw(raw_message, fallback_threading: false, trust_date: false, update
6468

6569
private
6670

67-
def update_existing_message(message, body:, sent_at:, update_existing:)
71+
def update_existing_message(message, body:, sent_at:, reply_to_message_id:, update_existing:)
6872
return if update_existing.empty?
6973

7074
updates = {}
@@ -80,6 +84,10 @@ def update_existing_message(message, body:, sent_at:, update_existing:)
8084
end
8185
end
8286

87+
if update_existing.include?(:reply_to_message_id) && reply_to_message_id
88+
updates[:reply_to_message_id] = reply_to_message_id
89+
end
90+
8391
message.update_columns(updates) if updates.any?
8492
end
8593

@@ -127,12 +135,12 @@ def handle_attachments(m, msg)
127135
end
128136
end
129137

130-
def resolve_threading(m, import_log)
138+
def resolve_threading(m, reply_to_message_id, import_log)
131139
reply_to_msg = nil
132140

133-
if m.in_reply_to
134-
reply_to_msg = Message.find_by_message_id(clean_reference(m.in_reply_to))
135-
import_log += "Reply to msg id not found: #{clean_reference(m.in_reply_to)}" unless reply_to_msg
141+
if reply_to_message_id
142+
reply_to_msg = Message.find_by_message_id(reply_to_message_id)
143+
import_log += "Reply to msg id not found: #{reply_to_message_id}" unless reply_to_msg
136144
end
137145

138146
if m.references && reply_to_msg.nil?
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddReplyToMessageIdToMessages < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :messages, :reply_to_message_id, :string
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/import_options.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def self.parse!(argv = ARGV)
1515
opts.on('--update-date', 'Update date of existing messages') do
1616
options[:update_existing] |= [:date]
1717
end
18+
opts.on('--update-reply-to-message-id', 'Update reply_to_message_id of existing messages') do
19+
options[:update_existing] |= [:reply_to_message_id]
20+
end
1821
end.parse!(argv)
1922

2023
options

0 commit comments

Comments
 (0)