@@ -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?
0 commit comments