Skip to content

Commit

Permalink
fixup! Filter messages last 7 days
Browse files Browse the repository at this point in the history
Signed-off-by: greta <[email protected]>
  • Loading branch information
GretaD committed Apr 2, 2024
1 parent b859f84 commit b55939b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 66 deletions.
22 changes: 0 additions & 22 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message>
Expand Down Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions lib/Service/Search/FilterStringParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
36 changes: 1 addition & 35 deletions lib/Service/Search/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

namespace OCA\Mail\Service\Search;
use DateTime;

class SearchQuery {
/** @var int|null */
private $cursor;
Expand Down Expand Up @@ -68,9 +68,6 @@ class SearchQuery {
/** @var bool */
private $hasAttachments = false;

/** @var int|null */
private $sentLast7Days = false;

/**
* @return int|null
* @psalm-mutation-free
Expand Down Expand Up @@ -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;
}
}
22 changes: 22 additions & 0 deletions src/components/SearchMessages.vue
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@
{{ t('mail', 'Has attachments') }}
</NcCheckboxRadioSwitch>
</div>
<div class="modal-inner-inline">
<NcCheckboxRadioSwitch :checked.sync="searchFlags"
value="lastWeek"
name="flags[]"
type="checkbox">
{{ t('mail', 'Sent in the last 7 days') }}
</NcCheckboxRadioSwitch>
</div>
</div>
</div>

Expand Down Expand Up @@ -378,6 +386,8 @@ export default {
start: this.prepareStart(),
end: this.prepareEnd(),
attachments: this.hasAttachments ? this.hasAttachments.toString() : '',
lastWeek: this.prepareLastWeekFilter(),
}
},
searchQuery() {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b55939b

Please sign in to comment.