plugins/base: wrap MailboxInfo and MailboxStatus
This allows us to extend these and expose helpers for templates and plugins.
This commit is contained in:
parent
f6959346ee
commit
5af6c6adc1
2 changed files with 35 additions and 15 deletions
|
@ -17,16 +17,36 @@ import (
|
||||||
"github.com/emersion/go-message/textproto"
|
"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)
|
ch := make(chan *imap.MailboxInfo, 10)
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
done <- conn.List("", "*", ch)
|
done <- conn.List("", "*", ch)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var mailboxes []*imap.MailboxInfo
|
var mailboxes []MailboxInfo
|
||||||
for mbox := range ch {
|
for mbox := range ch {
|
||||||
mailboxes = append(mailboxes, mbox)
|
mailboxes = append(mailboxes, MailboxInfo{mbox})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := <-done; err != nil {
|
if err := <-done; err != nil {
|
||||||
|
@ -46,7 +66,7 @@ const (
|
||||||
mailboxDrafts
|
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)
|
ch := make(chan *imap.MailboxInfo, 10)
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
go func() {
|
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 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 {
|
func ensureMailboxSelected(conn *imapclient.Client, mboxName string) error {
|
||||||
|
|
|
@ -65,8 +65,8 @@ func registerRoutes(p *koushin.GoPlugin) {
|
||||||
|
|
||||||
type MailboxRenderData struct {
|
type MailboxRenderData struct {
|
||||||
koushin.BaseRenderData
|
koushin.BaseRenderData
|
||||||
Mailbox *imap.MailboxStatus
|
Mailbox *MailboxStatus
|
||||||
Mailboxes []*imap.MailboxInfo
|
Mailboxes []MailboxInfo
|
||||||
Messages []IMAPMessage
|
Messages []IMAPMessage
|
||||||
PrevPage, NextPage int
|
PrevPage, NextPage int
|
||||||
Query string
|
Query string
|
||||||
|
@ -94,9 +94,9 @@ func handleGetMailbox(ctx *koushin.Context) error {
|
||||||
|
|
||||||
query := ctx.QueryParam("query")
|
query := ctx.QueryParam("query")
|
||||||
|
|
||||||
var mailboxes []*imap.MailboxInfo
|
var mailboxes []MailboxInfo
|
||||||
var msgs []IMAPMessage
|
var msgs []IMAPMessage
|
||||||
var mbox *imap.MailboxStatus
|
var mbox *MailboxStatus
|
||||||
var total int
|
var total int
|
||||||
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
||||||
var err error
|
var err error
|
||||||
|
@ -111,7 +111,7 @@ func handleGetMailbox(ctx *koushin.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mbox = c.Mailbox()
|
mbox = &MailboxStatus{c.Mailbox()}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -176,8 +176,8 @@ func handleLogout(ctx *koushin.Context) error {
|
||||||
|
|
||||||
type MessageRenderData struct {
|
type MessageRenderData struct {
|
||||||
koushin.BaseRenderData
|
koushin.BaseRenderData
|
||||||
Mailboxes []*imap.MailboxInfo
|
Mailboxes []MailboxInfo
|
||||||
Mailbox *imap.MailboxStatus
|
Mailbox *MailboxStatus
|
||||||
Message *IMAPMessage
|
Message *IMAPMessage
|
||||||
Part *IMAPPartNode
|
Part *IMAPPartNode
|
||||||
View interface{}
|
View interface{}
|
||||||
|
@ -201,10 +201,10 @@ func handleGetPart(ctx *koushin.Context, raw bool) error {
|
||||||
}
|
}
|
||||||
messagesPerPage := settings.MessagesPerPage
|
messagesPerPage := settings.MessagesPerPage
|
||||||
|
|
||||||
var mailboxes []*imap.MailboxInfo
|
var mailboxes []MailboxInfo
|
||||||
var msg *IMAPMessage
|
var msg *IMAPMessage
|
||||||
var part *message.Entity
|
var part *message.Entity
|
||||||
var mbox *imap.MailboxStatus
|
var mbox *MailboxStatus
|
||||||
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
||||||
var err error
|
var err error
|
||||||
if mailboxes, err = listMailboxes(c); err != nil {
|
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 {
|
if msg, part, err = getMessagePart(c, mboxName, uid, partPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mbox = c.Mailbox()
|
mbox = &MailboxStatus{c.Mailbox()}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue