diff options
author | 2023-05-01 16:34:56 +0200 | |
---|---|---|
committer | 2023-05-01 16:34:56 +0200 | |
commit | ab8b5f7678da886eb55216832ef47a730f5a4830 (patch) | |
tree | be2356aff20edfdbb6bf35afcd2e08bbce4d81b0 | |
parent | f1d00df4dda7a35651359b76bad34a9d1262994d (diff) | |
download | alps-ab8b5f7678da886eb55216832ef47a730f5a4830.tar.gz alps-ab8b5f7678da886eb55216832ef47a730f5a4830.tar.bz2 alps-ab8b5f7678da886eb55216832ef47a730f5a4830.zip |
Use LIST-STATUS to fetch unread message count for all mailboxes
-rw-r--r-- | plugins/base/imap.go | 22 | ||||
-rw-r--r-- | plugins/base/routes.go | 12 |
2 files changed, 30 insertions, 4 deletions
diff --git a/plugins/base/imap.go b/plugins/base/imap.go index e3720d0..bce1dc1 100644 --- a/plugins/base/imap.go +++ b/plugins/base/imap.go @@ -46,14 +46,28 @@ func (mbox *MailboxInfo) HasAttr(flag string) bool { } func listMailboxes(conn *imapclient.Client) ([]MailboxInfo, error) { + var options imap.ListOptions + if conn.Caps().Has(imap.CapListStatus) { + options.ReturnStatus = &imap.StatusOptions{ + NumMessages: true, + UIDValidity: true, + NumUnseen: true, + } + } + var mailboxes []MailboxInfo - list := conn.List("", "*", nil) + list := conn.List("", "*", &options) for { - mbox := list.Next() - if mbox == nil { + data := list.Next() + if data == nil { break } - mailboxes = append(mailboxes, MailboxInfo{mbox, false, -1, -1}) + mbox := MailboxInfo{data, false, -1, -1} + if mbox.Status != nil { + mbox.Unseen = int(*mbox.Status.NumUnseen) + mbox.Total = int(*mbox.Status.NumMessages) + } + mailboxes = append(mailboxes, mbox) } if err := list.Close(); err != nil { return nil, fmt.Errorf("failed to list mailboxes: %v", err) diff --git a/plugins/base/routes.go b/plugins/base/routes.go index 7c3748d..c04b9ef 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -150,6 +150,18 @@ func newIMAPBaseRenderData(ctx *alps.Context, return err } + if c.Caps().Has(imap.CapListStatus) { + for _, mbox := range mailboxes { + if mbox.Status == nil { + continue + } + statuses[mbox.Name()] = &MailboxStatus{mbox.Status} + } + inbox = statuses["INBOX"] + active = statuses[mboxName] + return nil + } + if mboxName != "" { if active, err = getMailboxStatus(c, mboxName); err != nil { return echo.NewHTTPError(http.StatusNotFound, err) |