From: Konstantinos Koukas <contact@koukas.org>
Implement server-side sorting of search results using the SORT
extension to IMAP, if the server supports it. Otherwise, fall back to
the unordered SEARCH command.
Sort messages by sent date, in descending order.
---
plugins/base/imap.go | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/plugins/base/imap.go b/plugins/base/imap.go
index 8ea88e9..efa1de3 100644
--- a/plugins/base/imap.go
+++ b/plugins/base/imap.go
@@ -471,12 +471,28 @@ func searchMessages(conn *imapclient.Client, mboxName, query string, page, messa
return nil, 0, err
}
- criteria := PrepareSearch(query)
- data, err := conn.Search(criteria, nil).Wait()
- if err != nil {
- return nil, 0, fmt.Errorf("UID SEARCH failed: %v", err)
+ searchCriteria := PrepareSearch(query)
+ var nums []uint32
+
+ if !conn.Caps().Has(imap.CapSort) {
+ data, err := conn.Search(searchCriteria, nil).Wait()
+ if err != nil {
+ return nil, 0, fmt.Errorf("UID SEARCH failed: %v", err)
+ }
+ nums = data.AllNums()
+ } else {
+ sortOptions := &imapclient.SortOptions{
+ SearchCriteria: searchCriteria,
+ SortCriteria: []imapclient.SortCriterion{
+ {Key: imapclient.SortKeyDate, Reverse: true},
+ },
+ }
+ nums, err = conn.Sort(sortOptions).Wait()
+ if err != nil {
+ return nil, 0, fmt.Errorf("SORT failed: %v", err)
+ }
}
- nums := data.AllNums()
+
total = len(nums)
from := page * messagesPerPage
--
2.38.5
On Wednesday, August 9th, 2023 at 23:15, Simon Ser <contact@emersion.fr> wrote:
> Shouldn't we use UIDSort instead of Sort?
Ah, no, my bad. I got confused due to the error message.
Pushed, thanks!