Skip to content

Commit

Permalink
refactor(GODT-2522): Cleanup old db types
Browse files Browse the repository at this point in the history
Refactor old db types to remove unnecessary fields and create new types
where required to avoid accidental misuse.
  • Loading branch information
LBeernaertProton committed Jun 27, 2023
1 parent fecbe0c commit ccfbcb7
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 213 deletions.
2 changes: 1 addition & 1 deletion db/ops_mailbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MailboxReadOps interface {

GetMailboxMessageIDPairs(ctx context.Context, mboxID imap.InternalMailboxID) ([]MessageIDPair, error)

GetAllMailboxesWithAttr(ctx context.Context) ([]*Mailbox, error)
GetAllMailboxesWithAttr(ctx context.Context) ([]*MailboxWithAttr, error)

GetAllMailboxesAsRemoteIDs(ctx context.Context) ([]imap.MailboxID, error)

Expand Down
21 changes: 2 additions & 19 deletions db/ops_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/ProtonMail/gluon/imap"
"github.com/bradenaw/juniper/xslices"
)

type MessageReadOps interface {
Expand All @@ -19,7 +18,7 @@ type MessageReadOps interface {

GetMessageRemoteID(ctx context.Context, id imap.InternalMessageID) (imap.MessageID, error)

GetImportedMessageData(ctx context.Context, id imap.InternalMessageID) (*Message, error)
GetImportedMessageData(ctx context.Context, id imap.InternalMessageID) (*MessageWithFlags, error)

GetMessageDateAndSize(ctx context.Context, id imap.InternalMessageID) (time.Time, int, error)

Expand All @@ -39,7 +38,7 @@ type MessageReadOps interface {
type MessageWriteOps interface {
MessageReadOps

CreateMessages(ctx context.Context, reqs ...*CreateMessageReq) ([]*Message, error)
CreateMessages(ctx context.Context, reqs ...*CreateMessageReq) error

CreateMessageAndAddToMailbox(ctx context.Context, mbox imap.InternalMailboxID, req *CreateMessageReq) (imap.UID, imap.FlagSet, error)

Expand Down Expand Up @@ -74,19 +73,3 @@ type MessageFlagSet struct {
RemoteID imap.MessageID
FlagSet imap.FlagSet
}

func NewFlagSet(msgUID *UID, flags []*MessageFlag) imap.FlagSet {
flagSet := imap.NewFlagSetFromSlice(xslices.Map(flags, func(flag *MessageFlag) string {
return flag.Value
}))

if msgUID.Deleted {
flagSet.AddToSelf(imap.FlagDeleted)
}

if msgUID.Recent {
flagSet.AddToSelf(imap.FlagRecent)
}

return flagSet
}
35 changes: 13 additions & 22 deletions db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/ProtonMail/gluon/imap"
"github.com/bradenaw/juniper/xslices"
)

type MailboxIDPair struct {
Expand Down Expand Up @@ -86,27 +85,16 @@ type MailboxAttr struct {
}

type Mailbox struct {
ID imap.InternalMailboxID
RemoteID imap.MailboxID
Name string
UIDValidity imap.UID
Subscribed bool
Flags []*MailboxFlag
PermanentFlags []*MailboxFlag
Attributes []*MailboxAttr
ID imap.InternalMailboxID
RemoteID imap.MailboxID
Name string
UIDValidity imap.UID
Subscribed bool
}

type MessageFlag struct {
ID int
Value string
}

func MessageFlagsFromFlagSet(set imap.FlagSet) []*MessageFlag {
return xslices.Map(set.ToSlice(), func(t string) *MessageFlag {
return &MessageFlag{
Value: t,
}
})
type MailboxWithAttr struct {
Mailbox
Attributes imap.FlagSet
}

type Message struct {
Expand All @@ -118,8 +106,11 @@ type Message struct {
BodyStructure string
Envelope string
Deleted bool
Flags []*MessageFlag
UIDs []*UID
}

type MessageWithFlags struct {
Message
Flags imap.FlagSet
}

type UID struct {
Expand Down
6 changes: 2 additions & 4 deletions internal/backend/connector_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
}

// Create message in the database
if _, err := tx.CreateMessages(ctx, xslices.Map(chunk, func(req *DBRequestWithLiteral) *db.CreateMessageReq {
if err := tx.CreateMessages(ctx, xslices.Map(chunk, func(req *DBRequestWithLiteral) *db.CreateMessageReq {
return &req.CreateMessageReq
})...); err != nil {
return err
Expand Down Expand Up @@ -686,10 +686,8 @@ func (user *user) applyMessageUpdated(ctx context.Context, update *imap.MessageU
InternalID: newInternalID,
}

if m, err := tx.CreateMessages(ctx, request); err != nil {
if err := tx.CreateMessages(ctx, request); err != nil {
return err
} else if len(m) == 0 {
return fmt.Errorf("no messages were inserted")
}

if err := user.store.Set(newInternalID, literalReader); err != nil {
Expand Down
Loading

0 comments on commit ccfbcb7

Please sign in to comment.