Skip to content

Commit

Permalink
feat(GODT-2448): Support answered flag in message updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshoulahan authored and LBeernaertProton committed Mar 6, 2023
1 parent 9ea4c68 commit f706f0f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 75 deletions.
40 changes: 25 additions & 15 deletions connector/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ func (conn *Dummy) AddMessagesToMailbox(ctx context.Context, messageIDs []imap.M
conn.pushUpdate(imap.NewMessageMailboxesUpdated(
messageID,
conn.state.getMailboxIDs(messageID),
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))
}

Expand All @@ -213,9 +215,11 @@ func (conn *Dummy) RemoveMessagesFromMailbox(ctx context.Context, messageIDs []i
conn.pushUpdate(imap.NewMessageMailboxesUpdated(
messageID,
conn.state.getMailboxIDs(messageID),
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))
}

Expand All @@ -230,9 +234,11 @@ func (conn *Dummy) MoveMessages(ctx context.Context, messageIDs []imap.MessageID
conn.pushUpdate(imap.NewMessageMailboxesUpdated(
messageID,
conn.state.getMailboxIDs(messageID),
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))
}

Expand All @@ -245,9 +251,11 @@ func (conn *Dummy) MarkMessagesSeen(ctx context.Context, messageIDs []imap.Messa

conn.pushUpdate(imap.NewMessageFlagsUpdated(
messageID,
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))
}

Expand All @@ -260,9 +268,11 @@ func (conn *Dummy) MarkMessagesFlagged(ctx context.Context, messageIDs []imap.Me

conn.pushUpdate(imap.NewMessageFlagsUpdated(
messageID,
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))
}

Expand Down
32 changes: 20 additions & 12 deletions connector/dummy_simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ func (conn *Dummy) MessageAdded(messageID imap.MessageID, mboxID imap.MailboxID)
conn.pushUpdate(imap.NewMessageMailboxesUpdated(
messageID,
conn.state.getMailboxIDs(messageID),
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))

return nil
Expand All @@ -180,9 +182,11 @@ func (conn *Dummy) MessageRemoved(messageID imap.MessageID, mboxID imap.MailboxI
conn.pushUpdate(imap.NewMessageMailboxesUpdated(
messageID,
conn.state.getMailboxIDs(messageID),
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))

return nil
Expand All @@ -193,9 +197,11 @@ func (conn *Dummy) MessageSeen(messageID imap.MessageID, seen bool) error {

conn.pushUpdate(imap.NewMessageFlagsUpdated(
messageID,
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))

return nil
Expand All @@ -206,9 +212,11 @@ func (conn *Dummy) MessageFlagged(messageID imap.MessageID, flagged bool) error

conn.pushUpdate(imap.NewMessageFlagsUpdated(
messageID,
conn.state.isSeen(messageID),
conn.state.isFlagged(messageID),
conn.state.isDraft(messageID),
imap.MessageCustomFlags{
Seen: conn.state.isSeen(messageID),
Flagged: conn.state.isFlagged(messageID),
Draft: conn.state.isDraft(messageID),
},
))

return nil
Expand Down
16 changes: 6 additions & 10 deletions imap/update_message_flags_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@ type MessageFlagsUpdated struct {

*updateWaiter

MessageID MessageID
Seen, Flagged, Draft bool
MessageID MessageID
CustomFlags MessageCustomFlags
}

func NewMessageFlagsUpdated(messageID MessageID, seen, flagged, draft bool) *MessageFlagsUpdated {
func NewMessageFlagsUpdated(messageID MessageID, flags MessageCustomFlags) *MessageFlagsUpdated {
return &MessageFlagsUpdated{
updateWaiter: newUpdateWaiter(),
MessageID: messageID,
Seen: seen,
Flagged: flagged,
Draft: draft,
CustomFlags: flags,
}
}

func (u *MessageFlagsUpdated) String() string {
return fmt.Sprintf(
"MessageFlagsUpdated: MessageID = %v, seen = %v, flagged = %v, draft = %v",
"MessageFlagsUpdated: MessageID = %v, CustomFlags = %v",
u.MessageID.ShortID(),
u.Seen,
u.Flagged,
u.Draft,
u.CustomFlags,
)
}
26 changes: 15 additions & 11 deletions imap/update_message_labels_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,33 @@ type MessageMailboxesUpdated struct {

*updateWaiter

MessageID MessageID
MailboxIDs []MailboxID
Seen, Flagged, Draft bool
MessageID MessageID
MailboxIDs []MailboxID
CustomFlags MessageCustomFlags
}

func NewMessageMailboxesUpdated(messageID MessageID, mailboxIDs []MailboxID, seen, flagged, draft bool) *MessageMailboxesUpdated {
type MessageCustomFlags struct {
Seen, Flagged, Draft, Answered bool
}

func (f MessageCustomFlags) String() string {
return fmt.Sprintf("seen=%v, flagged=%v, draft=%v, answered=%v", f.Seen, f.Flagged, f.Draft, f.Answered)
}

func NewMessageMailboxesUpdated(messageID MessageID, mailboxIDs []MailboxID, flags MessageCustomFlags) *MessageMailboxesUpdated {
return &MessageMailboxesUpdated{
updateWaiter: newUpdateWaiter(),
MessageID: messageID,
MailboxIDs: mailboxIDs,
Seen: seen,
Flagged: flagged,
Draft: draft,
CustomFlags: flags,
}
}

func (u *MessageMailboxesUpdated) String() string {
return fmt.Sprintf(
"MessageMailboxesUpdated: MessageID = %v, MailboxIDs = %v, seen = %v, flagged = %v, draft = %v",
"MessageMailboxesUpdated: MessageID = %v, MailboxIDs = %v, CustomFlags = %v",
u.MessageID.ShortID(),
xslices.Map(u.MailboxIDs, func(id MailboxID) string { return id.ShortID() }),
u.Seen,
u.Flagged,
u.Draft,
u.CustomFlags,
)
}
52 changes: 26 additions & 26 deletions internal/backend/connector_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (user *user) applyMessageMailboxesUpdated(ctx context.Context, update *imap
return err
}

if err := user.setMessageFlags(ctx, tx, internalMsgID, update.Seen, update.Flagged, update.Draft); err != nil {
if err := user.setMessageFlags(ctx, tx, internalMsgID, update.CustomFlags); err != nil {
return err
}

Expand Down Expand Up @@ -393,7 +393,7 @@ func (user *user) applyMessageFlagsUpdated(ctx context.Context, update *imap.Mes
return err
}

if err := user.setMessageFlags(ctx, tx, internalMsgID, update.Seen, update.Flagged, update.Draft); err != nil {
if err := user.setMessageFlags(ctx, tx, internalMsgID, update.CustomFlags); err != nil {
return err
}

Expand Down Expand Up @@ -465,42 +465,42 @@ func (user *user) applyMessagesRemovedFromMailbox(ctx context.Context, tx *ent.T
return nil
}

func (user *user) setMessageFlags(ctx context.Context, tx *ent.Tx, messageID imap.InternalMessageID, seen, flagged, draft bool) error {
func (user *user) setMessageFlags(ctx context.Context, tx *ent.Tx, messageID imap.InternalMessageID, flags imap.MessageCustomFlags) error {
curFlags, err := db.GetMessageFlags(ctx, tx.Client(), []imap.InternalMessageID{messageID})
if err != nil {
return err
}

flagSet := curFlags[0].FlagSet

if seen && !flagSet.ContainsUnchecked(imap.FlagSeenLowerCase) {
if err := user.addMessageFlags(ctx, tx, messageID, imap.FlagSeen); err != nil {
return err
}
} else if !seen && flagSet.ContainsUnchecked(imap.FlagSeenLowerCase) {
if err := user.removeMessageFlags(ctx, tx, messageID, imap.FlagSeen); err != nil {
return err
setFlag := func(flag string, wantFlag bool) error {
if wantFlag && !flagSet.Contains(flag) {
if err := user.addMessageFlags(ctx, tx, messageID, flag); err != nil {
return err
}
} else if !wantFlag && flagSet.Contains(flag) {
if err := user.removeMessageFlags(ctx, tx, messageID, flag); err != nil {
return err
}
}

return nil
}

if flagged && !flagSet.ContainsUnchecked(imap.FlagFlaggedLowerCase) {
if err := user.addMessageFlags(ctx, tx, messageID, imap.FlagFlagged); err != nil {
return err
}
} else if !flagged && flagSet.ContainsUnchecked(imap.FlagFlaggedLowerCase) {
if err := user.removeMessageFlags(ctx, tx, messageID, imap.FlagFlagged); err != nil {
return err
}
if err := setFlag(imap.FlagSeen, flags.Seen); err != nil {
return err
}

if draft && !flagSet.ContainsUnchecked(imap.FlagDraftLowerCase) {
if err := user.addMessageFlags(ctx, tx, messageID, imap.FlagDraft); err != nil {
return err
}
} else if !draft && flagSet.ContainsUnchecked(imap.FlagDraftLowerCase) {
if err := user.removeMessageFlags(ctx, tx, messageID, imap.FlagDraft); err != nil {
return err
}
if err := setFlag(imap.FlagFlagged, flags.Flagged); err != nil {
return err
}

if err := setFlag(imap.FlagDraft, flags.Draft); err != nil {
return err
}

if err := setFlag(imap.FlagAnswered, flags.Answered); err != nil {
return err
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion tests/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestErrNoSuchMessage(t *testing.T) {
}

runOneToOneTestWithAuth(t, defaultServerOptions(t, withConnectorBuilder(connBuilder)), func(c *testConnection, s *testSession) {
update := imap.NewMessageMailboxesUpdated("this is not the message you are looking for", []imap.MailboxID{}, false, false, false)
update := imap.NewMessageMailboxesUpdated("this is not the message you are looking for", []imap.MailboxID{}, imap.MessageCustomFlags{})

connBuilder.updateCh <- update

Expand Down

0 comments on commit f706f0f

Please sign in to comment.