diff options
author | Simon Ser <contact@emersion.fr> | 2019-12-17 16:23:51 +0100 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2019-12-17 16:23:51 +0100 |
commit | 80da410c3bb1c0d8d53c02867afc0cddd2f185bd (patch) | |
tree | 1dac77ab38b6818de3acd6941a4514d2812ebe23 | |
parent | 6bccdd62cec3ce20f50da12c8b4a23678746aea5 (diff) | |
download | alps-80da410c3bb1c0d8d53c02867afc0cddd2f185bd.tar.gz alps-80da410c3bb1c0d8d53c02867afc0cddd2f185bd.zip |
Implement paging for search
-rw-r--r-- | plugins/base/imap.go | 25 | ||||
-rw-r--r-- | plugins/base/public/mailbox.html | 6 | ||||
-rw-r--r-- | plugins/base/routes.go | 15 |
3 files changed, 30 insertions, 16 deletions
diff --git a/plugins/base/imap.go b/plugins/base/imap.go index 433a63e..3e268db 100644 --- a/plugins/base/imap.go +++ b/plugins/base/imap.go @@ -227,26 +227,33 @@ func listMessages(conn *imapclient.Client, mboxName string, page int) ([]IMAPMes return msgs, nil } -func searchMessages(conn *imapclient.Client, mboxName, query string) ([]IMAPMessage, error) { +func searchMessages(conn *imapclient.Client, mboxName, query string, page int) (msgs []IMAPMessage, total int, err error) { if err := ensureMailboxSelected(conn, mboxName); err != nil { - return nil, err + return nil, 0, err } criteria := imap.SearchCriteria{Text: []string{query}} nums, err := conn.Search(&criteria) if err != nil { - return nil, fmt.Errorf("UID SEARCH failed: %v", err) + return nil, 0, fmt.Errorf("UID SEARCH failed: %v", err) } - if len(nums) == 0 { - return nil, nil + total = len(nums) + + from := page*messagesPerPage + to := from + messagesPerPage + if from >= len(nums) { + return nil, total, nil } + if to > len(nums) { + to = len(nums) + } + nums = nums[from:to] indexes := make(map[uint32]int) for i, num := range nums { indexes[num] = i } - // TODO: paging var seqSet imap.SeqSet seqSet.AddNum(nums...) @@ -258,7 +265,7 @@ func searchMessages(conn *imapclient.Client, mboxName, query string) ([]IMAPMess done <- conn.Fetch(&seqSet, fetch, ch) }() - msgs := make([]IMAPMessage, len(nums)) + msgs = make([]IMAPMessage, len(nums)) for msg := range ch { i, ok := indexes[msg.SeqNum] if !ok { @@ -268,10 +275,10 @@ func searchMessages(conn *imapclient.Client, mboxName, query string) ([]IMAPMess } if err := <-done; err != nil { - return nil, fmt.Errorf("failed to fetch message list: %v", err) + return nil, 0, fmt.Errorf("failed to fetch message list: %v", err) } - return msgs, nil + return msgs, total, nil } func getMessagePart(conn *imapclient.Client, mboxName string, uid uint32, partPath []int) (*IMAPMessage, *message.Entity, error) { diff --git a/plugins/base/public/mailbox.html b/plugins/base/public/mailbox.html index 6deaa38..d480440 100644 --- a/plugins/base/public/mailbox.html +++ b/plugins/base/public/mailbox.html @@ -8,7 +8,7 @@ <h2>{{.Mailbox.Name}}</h2> -<form method="post" action=""> +<form method="get" action=""> <input type="search" name="query" value="{{.Query}}"> <input type="submit" value="Search"> </form> @@ -36,11 +36,11 @@ <p> {{if ge .PrevPage 0}} - <a href="?page={{.PrevPage}}">Prev</a> + <a href="?page={{.PrevPage}}&query={{.Query}}">Prev</a> {{end}} {{if and (ge .PrevPage 0) (ge .NextPage 0)}}ยท{{end}} {{if ge .NextPage 0}} - <a href="?page={{.NextPage}}">Next</a> + <a href="?page={{.NextPage}}&query={{.Query}}">Next</a> {{end}} </p> {{else}} diff --git a/plugins/base/routes.go b/plugins/base/routes.go index 35e72de..c7b6609 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -74,18 +74,19 @@ func handleGetMailbox(ctx *koushin.Context) error { } } - query := ctx.FormValue("query") + query := ctx.QueryParam("query") var mailboxes []*imap.MailboxInfo var msgs []IMAPMessage var mbox *imap.MailboxStatus + var total int err = ctx.Session.DoIMAP(func(c *imapclient.Client) error { var err error if mailboxes, err = listMailboxes(c); err != nil { return err } if query != "" { - msgs, err = searchMessages(c, mboxName, query) + msgs, total, err = searchMessages(c, mboxName, query, page) } else { msgs, err = listMessages(c, mboxName, page) } @@ -100,8 +101,14 @@ func handleGetMailbox(ctx *koushin.Context) error { } prevPage, nextPage := -1, -1 - if query == "" { - // TODO: paging for search + if query != "" { + if page > 0 { + prevPage = page - 1 + } + if (page+1)*messagesPerPage <= total { + nextPage = page + 1 + } + } else { if page > 0 { prevPage = page - 1 } |