Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BRIDGE-288): handle previously existing message in create events… #423

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connector/dummy_simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (conn *Dummy) MessageUpdated(message imap.Message, literal []byte, mboxIDs
mboxIDs: mboxIDMap,
}

conn.pushUpdate(imap.NewMessageUpdated(message, literal, mboxIDs, parsedMessage, false))
conn.pushUpdate(imap.NewMessageUpdated(message, literal, mboxIDs, parsedMessage, false, false))

return nil
}
Expand Down
29 changes: 16 additions & 13 deletions imap/update_message_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,40 @@ type MessageUpdated struct {
updateBase
*updateWaiter

Message Message
Literal []byte
MailboxIDs []MailboxID
ParsedMessage *ParsedMessage
AllowCreate bool
Message Message
Literal []byte
MailboxIDs []MailboxID
ParsedMessage *ParsedMessage
AllowCreate bool
IgnoreUnknownMailboxIDs bool
}

func NewMessageUpdated(
message Message,
literal []byte,
mailboxIDs []MailboxID,
parsedMessage *ParsedMessage,
allowCreate bool,
allowCreate, ignoreUnkownMailboxIDs bool,
) *MessageUpdated {
return &MessageUpdated{
updateWaiter: newUpdateWaiter(),
Message: message,
Literal: literal,
MailboxIDs: mailboxIDs,
ParsedMessage: parsedMessage,
AllowCreate: allowCreate,
updateWaiter: newUpdateWaiter(),
Message: message,
Literal: literal,
MailboxIDs: mailboxIDs,
ParsedMessage: parsedMessage,
AllowCreate: allowCreate,
IgnoreUnknownMailboxIDs: ignoreUnkownMailboxIDs,
}
}

func (u *MessageUpdated) String() string {
return fmt.Sprintf("MessageUpdated: ID:%v Mailboxes:%v Flags:%s AllowCreate:%v",
return fmt.Sprintf("MessageUpdated: ID:%v Mailboxes:%v Flags:%s AllowCreate:%v IgnoreUnkownMailboxIDs:%v",
u.Message.ID.ShortID(),
xslices.Map(u.MailboxIDs, func(mboxID MailboxID) string {
return mboxID.ShortID()
}),
u.Message.Flags.ToSlice(),
u.AllowCreate,
u.IgnoreUnknownMailboxIDs,
)
}
38 changes: 35 additions & 3 deletions internal/backend/connector_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
messageForMBox := make(map[imap.InternalMailboxID][]db.MessageIDPair)
mboxInternalIDMap := make(map[imap.MailboxID]imap.InternalMailboxID)

// for existing messages - we fully update them
var messagesToUpdate []*imap.MessageCreated

err := userDBWrite(ctx, user, func(ctx context.Context, tx db.Transaction) ([]state.Update, error) {
for _, message := range update.Messages {
if slices.Contains(message.MailboxIDs, ids.GluonInternalRecoveryMailboxRemoteID) {
Expand All @@ -213,7 +216,7 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message

internalID, ok := messagesToCreateFilter[message.Message.ID]
if !ok {
messageID, err := tx.GetMessageIDFromRemoteID(ctx, message.Message.ID)
_, err := tx.GetMessageIDFromRemoteID(ctx, message.Message.ID)
if db.IsErrNotFound(err) {
internalID = imap.NewInternalMessageID()

Expand All @@ -237,7 +240,10 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
messagesToCreate = append(messagesToCreate, request)
messagesToCreateFilter[message.Message.ID] = internalID
} else if err == nil {
internalID = messageID
user.log.WithField("MessageID", message.Message.ID.ShortID()).
Info("Found existing message in create event, will update instead")
messagesToUpdate = append(messagesToUpdate, message)
continue
} else {
return nil, err
}
Expand Down Expand Up @@ -339,9 +345,23 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
}
}
}
return err
}

return err
for _, message := range messagesToUpdate {
LBeernaertProton marked this conversation as resolved.
Show resolved Hide resolved
if err := user.applyMessageUpdated(ctx, imap.NewMessageUpdated(
message.Message,
message.Literal,
message.MailboxIDs,
message.ParsedMessage,
false,
update.IgnoreUnknownMailboxIDs,
)); err != nil {
return fmt.Errorf("failed to update previously existing message during creation: %v", err)
}
}

return nil
}

// applyMessageMailboxesUpdated applies a MessageMailboxesUpdated update.
Expand Down Expand Up @@ -624,6 +644,12 @@ func (user *user) applyMessageUpdated(ctx context.Context, update *imap.MessageU
for _, mbox := range update.MailboxIDs {
internalMBoxID, err := tx.GetMailboxIDFromRemoteID(ctx, mbox)
if err != nil {
if update.IgnoreUnknownMailboxIDs {
user.log.WithField("MailboxID", mbox.ShortID()).
WithField("MessageID", update.Message.ID.ShortID()).
Warn("Unknown Mailbox ID during message update, skipping add to mailbox")
continue
}
return nil, err
}

Expand Down Expand Up @@ -702,6 +728,12 @@ func (user *user) applyMessageUpdated(ctx context.Context, update *imap.MessageU
for _, mbox := range update.MailboxIDs {
internalMBoxID, err := tx.GetMailboxIDFromRemoteID(ctx, mbox)
if err != nil {
if update.IgnoreUnknownMailboxIDs {
user.log.WithField("MailboxID", mbox.ShortID()).
WithField("MessageID", update.Message.ID.ShortID()).
Warn("Unknown Mailbox ID during message update, skipping add to mailbox")
continue
}
return nil, err
}

Expand Down
Loading