@@ -204,6 +204,9 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
204
204
messageForMBox := make (map [imap.InternalMailboxID ][]db.MessageIDPair )
205
205
mboxInternalIDMap := make (map [imap.MailboxID ]imap.InternalMailboxID )
206
206
207
+ // for existing messages - we fully update them
208
+ var messagesToUpdate []* imap.MessageCreated
209
+
207
210
err := userDBWrite (ctx , user , func (ctx context.Context , tx db.Transaction ) ([]state.Update , error ) {
208
211
for _ , message := range update .Messages {
209
212
if slices .Contains (message .MailboxIDs , ids .GluonInternalRecoveryMailboxRemoteID ) {
@@ -213,7 +216,7 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
213
216
214
217
internalID , ok := messagesToCreateFilter [message .Message .ID ]
215
218
if ! ok {
216
- messageID , err := tx .GetMessageIDFromRemoteID (ctx , message .Message .ID )
219
+ _ , err := tx .GetMessageIDFromRemoteID (ctx , message .Message .ID )
217
220
if db .IsErrNotFound (err ) {
218
221
internalID = imap .NewInternalMessageID ()
219
222
@@ -237,7 +240,10 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
237
240
messagesToCreate = append (messagesToCreate , request )
238
241
messagesToCreateFilter [message .Message .ID ] = internalID
239
242
} else if err == nil {
240
- internalID = messageID
243
+ user .log .WithField ("MessageID" , message .Message .ID .ShortID ()).
244
+ Info ("Found existing message in create event, will update instead" )
245
+ messagesToUpdate = append (messagesToUpdate , message )
246
+ continue
241
247
} else {
242
248
return nil , err
243
249
}
@@ -339,9 +345,23 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
339
345
}
340
346
}
341
347
}
348
+ return err
342
349
}
343
350
344
- return err
351
+ for _ , message := range messagesToUpdate {
352
+ if err := user .applyMessageUpdated (ctx , imap .NewMessageUpdated (
353
+ message .Message ,
354
+ message .Literal ,
355
+ message .MailboxIDs ,
356
+ message .ParsedMessage ,
357
+ false ,
358
+ update .IgnoreUnknownMailboxIDs ,
359
+ )); err != nil {
360
+ return fmt .Errorf ("failed to update previously existing message during creation: %v" , err )
361
+ }
362
+ }
363
+
364
+ return nil
345
365
}
346
366
347
367
// applyMessageMailboxesUpdated applies a MessageMailboxesUpdated update.
@@ -624,6 +644,12 @@ func (user *user) applyMessageUpdated(ctx context.Context, update *imap.MessageU
624
644
for _ , mbox := range update .MailboxIDs {
625
645
internalMBoxID , err := tx .GetMailboxIDFromRemoteID (ctx , mbox )
626
646
if err != nil {
647
+ if update .IgnoreUnknownMailboxIDs {
648
+ user .log .WithField ("MailboxID" , mbox .ShortID ()).
649
+ WithField ("MessageID" , update .Message .ID .ShortID ()).
650
+ Warn ("Unknown Mailbox ID during message update, skipping add to mailbox" )
651
+ continue
652
+ }
627
653
return nil , err
628
654
}
629
655
@@ -702,6 +728,12 @@ func (user *user) applyMessageUpdated(ctx context.Context, update *imap.MessageU
702
728
for _ , mbox := range update .MailboxIDs {
703
729
internalMBoxID , err := tx .GetMailboxIDFromRemoteID (ctx , mbox )
704
730
if err != nil {
731
+ if update .IgnoreUnknownMailboxIDs {
732
+ user .log .WithField ("MailboxID" , mbox .ShortID ()).
733
+ WithField ("MessageID" , update .Message .ID .ShortID ()).
734
+ Warn ("Unknown Mailbox ID during message update, skipping add to mailbox" )
735
+ continue
736
+ }
705
737
return nil , err
706
738
}
707
739
0 commit comments