diff --git a/connector/dummy.go b/connector/dummy.go index d991f2b1..3cd5a0d1 100644 --- a/connector/dummy.go +++ b/connector/dummy.go @@ -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), + }, )) } @@ -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), + }, )) } @@ -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), + }, )) } @@ -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), + }, )) } @@ -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), + }, )) } diff --git a/connector/dummy_simulate.go b/connector/dummy_simulate.go index 6325758b..6ffda7e6 100644 --- a/connector/dummy_simulate.go +++ b/connector/dummy_simulate.go @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/imap/update_message_flags_updated.go b/imap/update_message_flags_updated.go index 1e0886db..27af8b3d 100644 --- a/imap/update_message_flags_updated.go +++ b/imap/update_message_flags_updated.go @@ -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, ) } diff --git a/imap/update_message_labels_updated.go b/imap/update_message_labels_updated.go index ae110af0..7a0e1e7d 100644 --- a/imap/update_message_labels_updated.go +++ b/imap/update_message_labels_updated.go @@ -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, ) } diff --git a/internal/backend/connector_updates.go b/internal/backend/connector_updates.go index faa80b7b..82097bbd 100644 --- a/internal/backend/connector_updates.go +++ b/internal/backend/connector_updates.go @@ -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 } @@ -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 } @@ -465,7 +465,7 @@ 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 @@ -473,34 +473,34 @@ func (user *user) setMessageFlags(ctx context.Context, tx *ent.Tx, messageID ima 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 diff --git a/tests/state_test.go b/tests/state_test.go index d6e89dc9..4f0f894e 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -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