diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-08-11 12:20:58 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-08-25 14:44:04 +0300 |
commit | c6f8bc732e95236a6ba7447e7ced6d947ca8f0d8 (patch) | |
tree | 29a328a4e01b021d471c36387cfcfacf923bbabd /lib/Search | |
parent | 98f476bc57e4594f78284eb08c84c9844d0271df (diff) |
Add a unified search provider
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/Search')
-rw-r--r-- | lib/Search/Provider.php | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/lib/Search/Provider.php b/lib/Search/Provider.php new file mode 100644 index 000000000..3506f4665 --- /dev/null +++ b/lib/Search/Provider.php @@ -0,0 +1,109 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 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\Search; + +use OCA\Mail\AppInfo\Application; +use OCA\Mail\Contracts\IMailSearch; +use OCA\Mail\Db\Message; +use OCP\IDateTimeFormatter; +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\IUser; +use OCP\Search\IProvider; +use OCP\Search\ISearchQuery; +use OCP\Search\SearchResult; +use OCP\Search\SearchResultEntry; +use function array_map; + +class Provider implements IProvider { + + /** @var IMailSearch */ + private $mailSearch; + + /** @var IL10N */ + private $l10n; + + /** @var IDateTimeFormatter */ + private $dateTimeFormatter; + + /** @var IURLGenerator */ + private $urlGenerator; + + public function __construct(IMailSearch $mailSearch, + IL10N $l10n, + IDateTimeFormatter $dateTimeFormatter, + IURLGenerator $urlGenerator) { + $this->mailSearch = $mailSearch; + $this->l10n = $l10n; + $this->dateTimeFormatter = $dateTimeFormatter; + $this->urlGenerator = $urlGenerator; + } + + public function getId(): string { + return Application::APP_ID; + } + + public function getName(): string { + return $this->l10n->t('Mails'); + } + + public function getOrder(string $route, array $routeParameters): int { + return 20; + } + + public function search(IUser $user, ISearchQuery $query): SearchResult { + $messages = $this->mailSearch->findMessagesGlobally( + $user, + $query->getTerm(), + $query->getCursor(), + $query->getLimit() + ); + + return SearchResult::complete( + $this->getName(), + array_map(function (Message $message) { + $formattedDate = $this->dateTimeFormatter->formatDateTimeRelativeDay($message->getSentAt(), 'short'); + $sender = $message->getFrom()->first(); + if ($sender !== null && $sender->getLabel() !== null) { + $subline = $sender->getLabel() . ' – ' . $formattedDate; + } else { + $subline = $formattedDate; + } + + return new SearchResultEntry( + '', + $message->getSubject(), + $subline, + $this->urlGenerator->linkToRoute('mail.page.thread', [ + 'mailboxId' => $message->getMailboxId(), + 'id' => $message->getId(), + ]), // TODO: deep URL + 'icon-mail' + ); + }, $messages) + ); + } +} |