Skip to content

Commit

Permalink
feat: Return mailbox counts when user added
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshoulahan authored and LBeernaertProton committed Nov 10, 2022
1 parent 43598c5 commit c99ec61
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions events/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package events

import "github.com/ProtonMail/gluon/imap"

type UserAdded struct {
eventBase

UserID string

Counts map[imap.MailboxID]int
}

type UserRemoved struct {
Expand Down
33 changes: 33 additions & 0 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"time"

"github.com/ProtonMail/gluon/connector"
"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/gluon/internal/db"
"github.com/ProtonMail/gluon/internal/db/ent"
"github.com/ProtonMail/gluon/internal/db/ent/mailbox"
"github.com/ProtonMail/gluon/internal/state"
"github.com/ProtonMail/gluon/reporter"
"github.com/ProtonMail/gluon/store"
Expand Down Expand Up @@ -122,6 +125,36 @@ func (b *Backend) RemoveUser(ctx context.Context, userID string, removeFiles boo
return nil
}

func (b *Backend) GetMailboxMessageCounts(ctx context.Context, userID string) (map[imap.MailboxID]int, error) {
b.usersLock.Lock()
defer b.usersLock.Unlock()

user, ok := b.users[userID]
if !ok {
return nil, ErrNoSuchUser
}

return db.ReadResult(ctx, user.db, func(ctx context.Context, c *ent.Client) (map[imap.MailboxID]int, error) {
counts := make(map[imap.MailboxID]int)

mailboxes, err := c.Mailbox.Query().Select(mailbox.FieldRemoteID).All(ctx)
if err != nil {
return nil, err
}

for _, mailbox := range mailboxes {
messageCount, err := mailbox.QueryUIDs().Count(ctx)
if err != nil {
return nil, err
}

counts[mailbox.RemoteID] = messageCount
}

return counts, nil
})
}

func (b *Backend) GetState(ctx context.Context, username string, password []byte, sessionID int) (*state.State, error) {
b.usersLock.Lock()
defer b.usersLock.Unlock()
Expand Down
8 changes: 7 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ func (s *Server) LoadUser(ctx context.Context, conn connector.Connector, userID
ctx = reporter.NewContextWithReporter(ctx, s.reporter)

if err := s.backend.AddUser(ctx, userID, conn, passphrase); err != nil {
return err
return fmt.Errorf("failed to add user: %w", err)
}

counts, err := s.backend.GetMailboxMessageCounts(ctx, userID)
if err != nil {
return fmt.Errorf("failed to get counts: %w", err)
}

s.publish(events.UserAdded{
UserID: userID,
Counts: counts,
})

return nil
Expand Down

0 comments on commit c99ec61

Please sign in to comment.