Implement paging for search
This commit is contained in:
parent
6bccdd62ce
commit
80da410c3b
3 changed files with 30 additions and 16 deletions
|
@ -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) {
|
||||
|
|
|
@ -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}}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue