diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-03-02 23:28:49 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-04-25 22:36:58 +0300 |
commit | 7a576bc9300cedf7cd9210e8bfddd7e97adafe3e (patch) | |
tree | 7e4a225bb6006bf9b0a46cf6244b53d863639dcb /lib/Mailbox.php | |
parent | 55ff9146da707ef702521df23eb63764c4c9d21d (diff) |
Implement date- and cursor-based pagination
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/Mailbox.php')
-rw-r--r-- | lib/Mailbox.php | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/lib/Mailbox.php b/lib/Mailbox.php index 8ba5058fa..4dde6514d 100644 --- a/lib/Mailbox.php +++ b/lib/Mailbox.php @@ -93,8 +93,9 @@ class Mailbox implements IMailBox { /** * @param string $filter + * @param int $cursor */ - private function getSearchIds($filter) { + private function getSearchIds($filter, $cursor = null) { if ($filter instanceof Horde_Imap_Client_Search_Query) { $query = $filter; } else { @@ -106,6 +107,7 @@ class Mailbox implements IMailBox { if ($this->getSpecialRole() !== 'trash') { $query->flag(Horde_Imap_Client::FLAG_DELETED, false); } + $query->dateSearch($cursor, Horde_Imap_Client_Search_Query::DATE_SINCE); try { $result = $this->conn->search($this->mailBox, $query, [ @@ -123,7 +125,11 @@ class Mailbox implements IMailBox { return array_reverse($result['match']->ids); } - private function getFetchIds() { + /** + * @param int $cursor + * @return type + */ + private function getFetchIds($cursor = null) { $q = new Horde_Imap_Client_Fetch_Query(); $q->uid(); $q->imapDate(); @@ -131,7 +137,10 @@ class Mailbox implements IMailBox { $result = $this->conn->fetch($this->mailBox, $q); $uidMap = []; foreach ($result as $r) { - $uidMap[$r->getUid()] = $r->getImapDate()->getTimeStamp(); + $ts = $r->getImapDate()->getTimeStamp(); + if (is_null($cursor) || $ts < $cursor) { + $uidMap[$r->getUid()] = $ts; + } } // sort by time uasort($uidMap, function($a, $b) { @@ -150,19 +159,14 @@ class Mailbox implements IMailBox { * the cursorId, if given. The size of the page is limited to 20. * * @param string|Horde_Imap_Client_Search_Query $filter - * @param int $cursorId last known ID on the client + * @param int $cursor time stamp of the oldest message on the client * @return array */ - public function getMessages($filter = null, $cursorId = null) { + public function getMessages($filter = null, $cursor = null) { if (!$this->conn->capability->query('SORT') && (is_null($filter) || $filter === '')) { - $ids = $this->getFetchIds($cursorId); + $ids = $this->getFetchIds($cursor); } else { - $ids = $this->getSearchIds($cursorId, $filter); - } - if ($cursorId) { - $ids = array_filter($ids, function($id) use ($cursorId) { - return $id > $cursorId; - }); + $ids = $this->getSearchIds($filter, $cursor); } $page = new Horde_Imap_Client_Ids(array_slice($ids, 0, 20, true)); @@ -199,6 +203,11 @@ class Mailbox implements IMailBox { } ob_get_clean(); + // sort by time + usort($messages, function($a, $b) { + return $a['dateInt'] < $b['dateInt']; + }); + return $messages; } |