Add basic message list

This commit is contained in:
Simon Ser 2019-12-02 18:21:45 +01:00
parent e9d31b0940
commit 39629b0740
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 64 additions and 3 deletions

48
imap.go
View file

@ -1,6 +1,8 @@
package koushin
import (
"sort"
"github.com/emersion/go-imap"
imapclient "github.com/emersion/go-imap/client"
)
@ -45,5 +47,51 @@ func listMailboxes(conn *imapclient.Client) ([]*imap.MailboxInfo, error) {
return nil, err
}
sort.Slice(mailboxes, func(i, j int) bool {
return mailboxes[i].Name < mailboxes[j].Name
})
return mailboxes, nil
}
func listMessages(conn *imapclient.Client, mboxName string) ([]*imap.Message, error) {
mbox := conn.Mailbox()
if mbox == nil || mbox.Name != mboxName {
var err error
mbox, err = conn.Select(mboxName, false)
if err != nil {
return nil, err
}
}
n := uint32(10)
from := uint32(1)
to := mbox.Messages
if mbox.Messages > n {
from = mbox.Messages - n
}
seqSet := new(imap.SeqSet)
seqSet.AddRange(from, to)
ch := make(chan *imap.Message, 10)
done := make(chan error, 1)
go func() {
done <- conn.Fetch(seqSet, []imap.FetchItem{imap.FetchEnvelope}, ch)
}()
msgs := make([]*imap.Message, 0, n)
for msg := range ch {
msgs = append(msgs, msg)
}
if err := <-done; err != nil {
return nil, err
}
// Reverse list of messages
for i := len(msgs)/2-1; i >= 0; i-- {
opp := len(msgs)-1-i
msgs[i], msgs[opp] = msgs[opp], msgs[i]
}
return msgs, nil
}

View file

@ -9,4 +9,11 @@
{{end}}
</ul>
<p>Messages:</p>
<ul>
{{range .Messages}}
<li>{{.Envelope.Subject}}</li>
{{end}}
</ul>
{{template "foot"}}

View file

@ -88,7 +88,7 @@ func handleLogin(ectx echo.Context) error {
}
ctx.setToken(token)
return ctx.Redirect(http.StatusFound, "/")
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
}
return ctx.Render(http.StatusOK, "login.html", nil)
@ -135,7 +135,7 @@ func New(imapURL string) *echo.Echo {
e.Logger.Fatal("Failed to load templates:", err)
}
e.GET("/", func(ectx echo.Context) error {
e.GET("/mailbox/:mbox", func(ectx echo.Context) error {
ctx := ectx.(*context)
mailboxes, err := listMailboxes(ctx.conn)
@ -143,8 +143,14 @@ func New(imapURL string) *echo.Echo {
return err
}
return ctx.Render(http.StatusOK, "index.html", map[string]interface{}{
msgs, err := listMessages(ctx.conn, ctx.Param("mbox"))
if err != nil {
return err
}
return ctx.Render(http.StatusOK, "mailbox.html", map[string]interface{}{
"Mailboxes": mailboxes,
"Messages": msgs,
})
})