Add pagination to the email list (was hardcoded to newest 50, no way to see older mail)
listMessages() only ever fetched everything then kept the newest
limit (default 50) messages, with no offset parameter and no way for
the client to know how many messages existed beyond what it was
shown - older mail was completely unreachable through the webmail UI.
__imap::listMessages() now takes an offset (counted back from the
newest message) and returns {"total":N,"messages":[...]} instead of
a bare array, so the client can page backward/forward and know when
it has reached either end. __imaplist tag and /email/list both pass
offset through; /email/search is unchanged (search results are not
paginated). email.js tracks currentOffset (reset alongside
currentFolder/currentUid at every existing reset site), and renders
Prev/Next controls next to the unread count showing the current
1-based range and total.
This commit is contained in:
1 parent
dcfe2a667c
commit
41fbf0b7d9
5 files changed
+103
-19
No files matched your search
+20
-12
@@ -832,7 +832,7 @@ namespace jet {
|
||||
return extractLiteralAt(fetchLine, braceEnd);
|
||||
}
|
||||
|
||||
coreutils::MString __imap::listMessages(const std::string &folder, int limit) {
|
||||
coreutils::MString __imap::listMessages(const std::string &folder, int limit, int offset) {
|
||||
if (!selectFolderIfNeeded(folder))
|
||||
throw coreutils::Exception("imap: could not select folder " + folder, __FILE__, __LINE__, 502);
|
||||
|
||||
@@ -843,22 +843,30 @@ namespace jet {
|
||||
throw coreutils::Exception("imap: FETCH failed", __FILE__, __LINE__, 502);
|
||||
|
||||
std::vector<std::string> items = parseSummaryLines(lines);
|
||||
int total = (int)items.size();
|
||||
|
||||
// Keep only the newest `limit` - UIDs are assigned in strictly
|
||||
// ascending delivery order (see MailFolder.h, BARANTMail, built
|
||||
// earlier this session), so the tail of this UID-ascending list
|
||||
// is the newest mail.
|
||||
if ((int)items.size() > limit)
|
||||
items.erase(items.begin(), items.end() - limit);
|
||||
std::reverse(items.begin(), items.end());
|
||||
// items is UID-ascending (oldest-first) - UIDs are assigned in
|
||||
// strictly ascending delivery order (see MailFolder.h, BARANTMail,
|
||||
// built earlier this session), so the tail is the newest mail.
|
||||
// `offset` counts back from the newest message (0 = the very
|
||||
// newest), so the page is the `limit`-wide slice ending `offset`
|
||||
// messages from the end.
|
||||
int end = total - offset;
|
||||
if (end < 0)
|
||||
end = 0;
|
||||
int start = end - limit;
|
||||
if (start < 0)
|
||||
start = 0;
|
||||
std::vector<std::string> page(items.begin() + start, items.begin() + end);
|
||||
std::reverse(page.begin(), page.end());
|
||||
|
||||
std::string json = "[";
|
||||
for (size_t i = 0; i < items.size(); ++i) {
|
||||
std::string json = "{\"total\":" + std::to_string(total) + ",\"messages\":[";
|
||||
for (size_t i = 0; i < page.size(); ++i) {
|
||||
if (i)
|
||||
json += ",";
|
||||
json += items[i];
|
||||
json += page[i];
|
||||
}
|
||||
json += "]";
|
||||
json += "]}";
|
||||
return coreutils::MString(json);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user