Skip to content

Commit

Permalink
feat: Add AllowCreate to imap.MessageUpdated
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshoulahan authored and LBeernaertProton committed Feb 21, 2023
1 parent 9fe1c15 commit a4e1a32
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion connector/dummy_simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (conn *Dummy) MessageUpdated(message imap.Message, literal []byte, mboxIDs
mboxIDs: mboxIDMap,
}

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

return nil
}
Expand Down
19 changes: 15 additions & 4 deletions imap/update_message_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"github.com/bradenaw/juniper/xslices"
)

// MessageUpdated replaces the previous behavior of MessageDelete followed by MessageCreate. Furthermore, it guarantees
// that the operation is executed atomically.
// MessageUpdated replaces the previous behavior of MessageDelete followed by MessageCreate.
// If the message does exist, it is updated.
// If the message does not exist, it can optionally be created.
// Furthermore, it guarantees that the operation is executed atomically.
type MessageUpdated struct {
updateBase
*updateWaiter
Expand All @@ -16,24 +18,33 @@ type MessageUpdated struct {
Literal []byte
MailboxIDs []MailboxID
ParsedMessage *ParsedMessage
AllowCreate bool
}

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

func (u *MessageUpdated) String() string {
return fmt.Sprintf("MessageUpdate: ID:%v Mailboxes:%v Flags:%s",
return fmt.Sprintf("MessageUpdated: ID:%v Mailboxes:%v Flags:%s AllowCreate:%v",
u.Message.ID.ShortID(),
xslices.Map(u.MailboxIDs, func(mboxID MailboxID) string {
return mboxID.ShortID()
}),
u.Message.Flags.ToSlice(),
u.AllowCreate,
)
}
19 changes: 13 additions & 6 deletions internal/backend/connector_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,19 @@ func (user *user) applyMessageUpdated(ctx context.Context, update *imap.MessageU
return db.GetMessageIDFromRemoteID(ctx, client, update.Message.ID)
})
if ent.IsNotFound(err) {
return user.applyMessagesCreated(ctx, imap.NewMessagesCreated(true, &imap.MessageCreated{
Message: update.Message,
Literal: update.Literal,
MailboxIDs: update.MailboxIDs,
ParsedMessage: update.ParsedMessage,
}))
if update.AllowCreate {
log.Warn("Message not found, creating it instead")

return user.applyMessagesCreated(ctx, imap.NewMessagesCreated(true, &imap.MessageCreated{
Message: update.Message,
Literal: update.Literal,
MailboxIDs: update.MailboxIDs,
ParsedMessage: update.ParsedMessage,
}))
} else {
log.Warn("Message not found, skipping update")
return nil
}
} else if err != nil {
return err
}
Expand Down

0 comments on commit a4e1a32

Please sign in to comment.