From b55939bf0e7db026a68eafa35a142fd9f06b3460 Mon Sep 17 00:00:00 2001 From: greta Date: Tue, 2 Apr 2024 14:37:46 +0200 Subject: [PATCH] fixup! Filter messages last 7 days Signed-off-by: greta --- lib/Db/MessageMapper.php | 22 -------------- lib/Service/Search/FilterStringParser.php | 10 +------ lib/Service/Search/SearchQuery.php | 36 +---------------------- src/components/SearchMessages.vue | 22 ++++++++++++++ 4 files changed, 24 insertions(+), 66 deletions(-) diff --git a/lib/Db/MessageMapper.php b/lib/Db/MessageMapper.php index e0514e48a0..22333cc33e 100644 --- a/lib/Db/MessageMapper.php +++ b/lib/Db/MessageMapper.php @@ -55,7 +55,6 @@ use function mb_convert_encoding; use function mb_strcut; use function OCA\Mail\array_flat_map; -use DateTime; /** * @template-extends QBMapper @@ -736,27 +735,6 @@ public function deleteByUid(Mailbox $mailbox, int ...$uids): void { } } - /** - * Find messages sent within the last 7 days. - * - * @return Message[] - */ - public function findMessagesSentWithinLast7Days(): array { - $sevenDaysAgo = new DateTime(); - $sevenDaysAgo->modify('-7 days'); - - $qb = $this->db->getQueryBuilder(); - - $select = $qb->select('*') - ->from($this->getTableName()) - ->where( - $qb->expr()->gte('sent_at', $qb->createNamedParameter($sevenDaysAgo->format('Y-m-d H:i:s'), IQueryBuilder::PARAM_STR)) - ); - - return $this->findEntities($select); - } - - /** * @param Account $account * @param string $threadRootId diff --git a/lib/Service/Search/FilterStringParser.php b/lib/Service/Search/FilterStringParser.php index 4f61b256e9..55242db6be 100644 --- a/lib/Service/Search/FilterStringParser.php +++ b/lib/Service/Search/FilterStringParser.php @@ -85,13 +85,8 @@ private function parseFilterToken(SearchQuery $query, string $token): bool { return true; } - if (str_starts_with($token, 'sent_last_7_days')) { - $query->setSentLast7Days(7); - return true; - } - - break; + break; case 'from': $query->addFrom($param); return true; @@ -110,9 +105,6 @@ private function parseFilterToken(SearchQuery $query, string $token): bool { case 'body': $query->addBody($param); return true; - case 'sent_last_7_days': - $query->setSentLast7Days(7); - return true; case 'tags': $tags = explode(',', $param); $query->setTags($tags); diff --git a/lib/Service/Search/SearchQuery.php b/lib/Service/Search/SearchQuery.php index 56e3f49bbc..2450950699 100644 --- a/lib/Service/Search/SearchQuery.php +++ b/lib/Service/Search/SearchQuery.php @@ -24,7 +24,7 @@ */ namespace OCA\Mail\Service\Search; -use DateTime; + class SearchQuery { /** @var int|null */ private $cursor; @@ -68,9 +68,6 @@ class SearchQuery { /** @var bool */ private $hasAttachments = false; - /** @var int|null */ - private $sentLast7Days = false; - /** * @return int|null * @psalm-mutation-free @@ -239,35 +236,4 @@ public function getHasAttachments(): ?bool { public function setHasAttachments(bool $hasAttachments): void { $this->hasAttachments = $hasAttachments; } - /** - * Get whether to filter messages sent in the last 7 days - * - * @return int|null - */ - public function getSentLast7Days(): ?int { - return $this->sentLast7Days; - } - - /** - * Set whether to filter messages sent in the last 7 days - * - * @param bool $sentLast7Days - * @return void - */ - public function setSentLast7Days(int $sentLast7Days): void { - $this->sentLast7Days = $sentLast7Days; - } - /** - * Get the start date to filter messages sent in the last 7 days - * - * @return string|null - */ - public function getSentLast7DaysStartDate(): ?string { - if ($this->sentLast7Days) { - $startDate = new DateTime(); - $startDate->modify('-7 days'); - return $startDate->format('Y-m-d'); - } - return null; - } } diff --git a/src/components/SearchMessages.vue b/src/components/SearchMessages.vue index 3c84f1f1bf..476d7b097e 100644 --- a/src/components/SearchMessages.vue +++ b/src/components/SearchMessages.vue @@ -248,6 +248,14 @@ {{ t('mail', 'Has attachments') }} + @@ -378,6 +386,8 @@ export default { start: this.prepareStart(), end: this.prepareEnd(), attachments: this.hasAttachments ? this.hasAttachments.toString() : '', + lastWeek: this.prepareLastWeekFilter(), + } }, searchQuery() { @@ -406,6 +416,18 @@ export default { }, }, methods: { + prepareLastWeekFilter() { + const endDate = moment().endOf('day') + const startDate = moment(endDate).subtract(6, 'days').startOf('day') + + this.startDate = startDate + this.endDate = endDate + + return { + start: startDate.unix().toString(), + end: endDate.unix().toString(), + } + }, prepareStart() { if (this.startDate !== null) { if (this.endDate !== null && this.startDate > this.endDate) {