Skip to content

Commit

Permalink
fix: Add missing state update for connector mailbox delete update
Browse files Browse the repository at this point in the history
States were not notified that the mailbox was removed from the
connector.
  • Loading branch information
LBeernaertProton committed Oct 28, 2022
1 parent d8206f6 commit b3dcaa4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
23 changes: 16 additions & 7 deletions internal/backend/connector_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,18 @@ func (user *user) applyMailboxCreated(ctx context.Context, update *imap.MailboxC

// applyMailboxDeleted applies a MailboxDeleted update.
func (user *user) applyMailboxDeleted(ctx context.Context, update *imap.MailboxDeleted) error {
if exists, err := db.ReadResult(ctx, user.db, func(ctx context.Context, client *ent.Client) (bool, error) {
return db.MailboxExistsWithRemoteID(ctx, client, update.MailboxID)
}); err != nil {
internalMailboxID, err := db.ReadResult(ctx, user.db, func(ctx context.Context, client *ent.Client) (imap.InternalMailboxID, error) {
return db.GetMailboxIDFromRemoteID(ctx, client, update.MailboxID)
})
if err != nil {
if ent.IsNotFound(err) {
return nil
}

return err
} else if !exists {
return nil
}

return user.db.Write(ctx, func(ctx context.Context, tx *ent.Tx) error {
if err := user.db.Write(ctx, func(ctx context.Context, tx *ent.Tx) error {
uidValidity, increased, err := db.DeleteMailboxWithRemoteID(ctx, tx, update.MailboxID)
if err != nil {
return err
Expand All @@ -108,7 +111,13 @@ func (user *user) applyMailboxDeleted(ctx context.Context, update *imap.MailboxD
}

return nil
})
}); err != nil {
return err
}

user.queueStateUpdate(state.NewMailboxDeletedStateUpdate(internalMailboxID))

return nil
}

// applyMailboxUpdated applies a MailboxUpdated update.
Expand Down
9 changes: 9 additions & 0 deletions internal/db/mailbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func MailboxExistsWithRemoteID(ctx context.Context, client *ent.Client, mboxID i
return client.Mailbox.Query().Where(mailbox.RemoteID(mboxID)).Exist(ctx)
}

func GetMailboxIDFromRemoteID(ctx context.Context, client *ent.Client, mboxID imap.MailboxID) (imap.InternalMailboxID, error) {
mbox, err := client.Mailbox.Query().Where(mailbox.RemoteID(mboxID)).Select(mailbox.FieldID).Only(ctx)
if err != nil {
return 0, err
}

return mbox.ID, nil
}

func MailboxExistsWithName(ctx context.Context, client *ent.Client, name string) (bool, error) {
return client.Mailbox.Query().Where(mailbox.Name(name)).Exist(ctx)
}
Expand Down
15 changes: 15 additions & 0 deletions tests/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,18 @@ func TestDeleteExaminedMailboxCausesDisconnectOnOtherClients(t *testing.T) {
c[2].S(response.Bye().WithInconsistentState().String())
})
}

func TestDeleteSelectedMailboxWithRemoteUpdateCausesDisconnect(t *testing.T) {
runOneToOneTestWithAuth(t, defaultServerOptions(t), func(c *testConnection, s *testSession) {
mailboxID := s.mailboxCreated("user", []string{"mbox1"})
s.flush("user")

c.C("b002 SELECT mbox1").OK("b002")

s.mailboxDeleted("user", mailboxID)
s.flush("user")

c.C("b003 NOOP")
c.S(response.Bye().WithInconsistentState().String())
})
}
4 changes: 4 additions & 0 deletions tests/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (s *testSession) mailboxCreated(user string, name []string, withData ...str
return s.mailboxCreatedWithAttributes(user, name, defaultAttributes, withData...)
}

func (s *testSession) mailboxDeleted(user string, id imap.MailboxID) {
require.NoError(s.tb, s.conns[s.userIDs[user]].MailboxDeleted(id))
}

func (s *testSession) mailboxCreatedWithAttributes(user string, name []string, attributes imap.FlagSet, withData ...string) imap.MailboxID {
mboxID := imap.MailboxID(utils.NewRandomMailboxID())

Expand Down

0 comments on commit b3dcaa4

Please sign in to comment.