diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-01-15 17:14:50 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-01-15 18:17:01 +0300 |
commit | bf9e58106a73e7ed0591c6b2216e5263247e1360 (patch) | |
tree | fa30af7d173bb10b94d4e029c67e4a89aa90fd7e /lib/Service | |
parent | 13a11c6be1c63531ac29329bbf822eace26ea88d (diff) |
Allow complex expressions in message search queries and enhance the PI
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/Search/FilterStringParser.php | 49 | ||||
-rw-r--r-- | lib/Service/Search/FlagExpression.php | 80 | ||||
-rw-r--r-- | lib/Service/Search/SearchQuery.php | 16 |
3 files changed, 145 insertions, 0 deletions
diff --git a/lib/Service/Search/FilterStringParser.php b/lib/Service/Search/FilterStringParser.php index 763f314b0..87c8d362a 100644 --- a/lib/Service/Search/FilterStringParser.php +++ b/lib/Service/Search/FilterStringParser.php @@ -68,6 +68,55 @@ class FilterStringParser { $query->addFlag($type === 'is' ? $flag : $flag->invert()); return true; } + if ($param === 'pi-important') { + // We assume this is about 'is' and not 'not' + // imp && ~read + $query->addFlagExpression( + FlagExpression::and( + Flag::is(Flag::IMPORTANT), + Flag::not(Flag::SEEN) + ) + ); + + return true; + } + if ($param === 'pi-starred') { + // We assume this is about 'is' and not 'not' + // fav /\ (~imp \/ (imp /\ read)) + $query->addFlagExpression( + FlagExpression::and( + Flag::is(Flag::FLAGGED), + FlagExpression::or( + Flag::not(Flag::IMPORTANT), + FlagExpression::or( + Flag::is(Flag::IMPORTANT), + Flag::is(Flag::SEEN) + ) + ) + ) + ); + + return true; + } + if ($param === 'pi-other') { + // We assume this is about 'is' and not 'not' + // ~fav && (~imp || (imp && read)) + $query->addFlagExpression( + FlagExpression::and( + Flag::not(Flag::FLAGGED), + FlagExpression::or( + Flag::not(Flag::IMPORTANT), + FlagExpression::and( + Flag::is(Flag::IMPORTANT), + Flag::is(Flag::SEEN) + ) + ) + ) + ); + + return true; + } + break; case 'from': $query->addFrom($param); diff --git a/lib/Service/Search/FlagExpression.php b/lib/Service/Search/FlagExpression.php new file mode 100644 index 000000000..a219b8c8b --- /dev/null +++ b/lib/Service/Search/FlagExpression.php @@ -0,0 +1,80 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\Mail\Service\Search; + +class FlagExpression { + + /** + * @var string + * @psalm-var "and"|"or" + */ + private $operator; + + /** + * @var array + * @psalm-var (Flag|FlagExpression)[] + */ + private $operands; + + /** + * @psalm-param "and"|"or" $operator + * @param array $operands + */ + private function __construct(string $operator, array $operands) { + $this->operator = $operator; + $this->operands = $operands; + } + + /** + * @param Flag|FlagExpression ...$operands + * + * @return static + */ + public static function and(...$operands): self { + return new self("and", $operands); + } + + /** + * @param Flag|FlagExpression ...$operands + * + * @return static + */ + public static function or(...$operands): self { + return new self("or", $operands); + } + + public function getOperator(): string { + return $this->operator; + } + + /** + * @return array + * @psalm-return (Flag|FlagExpression)[] + */ + public function getOperands(): array { + return $this->operands; + } +} diff --git a/lib/Service/Search/SearchQuery.php b/lib/Service/Search/SearchQuery.php index 10be01e69..57af668a4 100644 --- a/lib/Service/Search/SearchQuery.php +++ b/lib/Service/Search/SearchQuery.php @@ -33,6 +33,9 @@ class SearchQuery { /** @var Flag[] */ private $flags = []; + /** @var FlagExpression[] */ + private $flagExpressions = []; + /** @var string[] */ private $to = []; @@ -53,6 +56,7 @@ class SearchQuery { /** * @return int|null + * @psalm-mutation-free */ public function getCursor(): ?int { return $this->cursor; @@ -67,6 +71,7 @@ class SearchQuery { /** * @return Flag[] + * @psalm-mutation-free */ public function getFlags(): array { return $this->flags; @@ -77,6 +82,17 @@ class SearchQuery { } /** + * @return FlagExpression[] + */ + public function getFlagExpressions(): array { + return $this->flagExpressions; + } + + public function addFlagExpression(FlagExpression $expression) { + $this->flagExpressions[] = $expression; + } + + /** * @return string[] */ public function getTo(): array { |