From 5af6c6adc172ac9cc2d24d0507adfeb45773373b Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 27 Mar 2020 10:45:43 +0100 Subject: plugins/base: wrap MailboxInfo and MailboxStatus This allows us to extend these and expose helpers for templates and plugins. --- plugins/base/imap.go | 30 +++++++++++++++++++++++++----- plugins/base/routes.go | 20 ++++++++++---------- 2 files changed, 35 insertions(+), 15 deletions(-) (limited to 'plugins') diff --git a/plugins/base/imap.go b/plugins/base/imap.go index ffa172f..760138b 100755 --- a/plugins/base/imap.go +++ b/plugins/base/imap.go @@ -17,16 +17,36 @@ import ( "github.com/emersion/go-message/textproto" ) -func listMailboxes(conn *imapclient.Client) ([]*imap.MailboxInfo, error) { +type MailboxInfo struct { + *imap.MailboxInfo +} + +func (mbox *MailboxInfo) URL() *url.URL { + return &url.URL{ + Path: fmt.Sprintf("/mailbox/%v", url.PathEscape(mbox.Name)), + } +} + +type MailboxStatus struct { + *imap.MailboxStatus +} + +func (mbox *MailboxStatus) URL() *url.URL { + return &url.URL{ + Path: fmt.Sprintf("/mailbox/%v", url.PathEscape(mbox.Name)), + } +} + +func listMailboxes(conn *imapclient.Client) ([]MailboxInfo, error) { ch := make(chan *imap.MailboxInfo, 10) done := make(chan error, 1) go func() { done <- conn.List("", "*", ch) }() - var mailboxes []*imap.MailboxInfo + var mailboxes []MailboxInfo for mbox := range ch { - mailboxes = append(mailboxes, mbox) + mailboxes = append(mailboxes, MailboxInfo{mbox}) } if err := <-done; err != nil { @@ -46,7 +66,7 @@ const ( mailboxDrafts ) -func getMailboxByType(conn *imapclient.Client, mboxType mailboxType) (*imap.MailboxInfo, error) { +func getMailboxByType(conn *imapclient.Client, mboxType mailboxType) (*MailboxInfo, error) { ch := make(chan *imap.MailboxInfo, 10) done := make(chan error, 1) go func() { @@ -91,7 +111,7 @@ func getMailboxByType(conn *imapclient.Client, mboxType mailboxType) (*imap.Mail return nil, fmt.Errorf("failed to get mailbox with attribute %q: %v", attr, err) } - return best, nil + return &MailboxInfo{best}, nil } func ensureMailboxSelected(conn *imapclient.Client, mboxName string) error { diff --git a/plugins/base/routes.go b/plugins/base/routes.go index 689fca4..e75573f 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -65,8 +65,8 @@ func registerRoutes(p *koushin.GoPlugin) { type MailboxRenderData struct { koushin.BaseRenderData - Mailbox *imap.MailboxStatus - Mailboxes []*imap.MailboxInfo + Mailbox *MailboxStatus + Mailboxes []MailboxInfo Messages []IMAPMessage PrevPage, NextPage int Query string @@ -94,9 +94,9 @@ func handleGetMailbox(ctx *koushin.Context) error { query := ctx.QueryParam("query") - var mailboxes []*imap.MailboxInfo + var mailboxes []MailboxInfo var msgs []IMAPMessage - var mbox *imap.MailboxStatus + var mbox *MailboxStatus var total int err = ctx.Session.DoIMAP(func(c *imapclient.Client) error { var err error @@ -111,7 +111,7 @@ func handleGetMailbox(ctx *koushin.Context) error { if err != nil { return err } - mbox = c.Mailbox() + mbox = &MailboxStatus{c.Mailbox()} return nil }) if err != nil { @@ -176,8 +176,8 @@ func handleLogout(ctx *koushin.Context) error { type MessageRenderData struct { koushin.BaseRenderData - Mailboxes []*imap.MailboxInfo - Mailbox *imap.MailboxStatus + Mailboxes []MailboxInfo + Mailbox *MailboxStatus Message *IMAPMessage Part *IMAPPartNode View interface{} @@ -201,10 +201,10 @@ func handleGetPart(ctx *koushin.Context, raw bool) error { } messagesPerPage := settings.MessagesPerPage - var mailboxes []*imap.MailboxInfo + var mailboxes []MailboxInfo var msg *IMAPMessage var part *message.Entity - var mbox *imap.MailboxStatus + var mbox *MailboxStatus err = ctx.Session.DoIMAP(func(c *imapclient.Client) error { var err error if mailboxes, err = listMailboxes(c); err != nil { @@ -213,7 +213,7 @@ func handleGetPart(ctx *koushin.Context, raw bool) error { if msg, part, err = getMessagePart(c, mboxName, uid, partPath); err != nil { return err } - mbox = c.Mailbox() + mbox = &MailboxStatus{c.Mailbox()} return nil }) if err != nil { -- cgit v1.2.3