You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContactsSearchProvider.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Search;
  27. use OCA\DAV\CardDAV\CardDavBackend;
  28. use OCP\App\IAppManager;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. use OCP\IUser;
  32. use OCP\Search\FilterDefinition;
  33. use OCP\Search\IFilter;
  34. use OCP\Search\IFilteringProvider;
  35. use OCP\Search\ISearchQuery;
  36. use OCP\Search\SearchResult;
  37. use OCP\Search\SearchResultEntry;
  38. use Sabre\VObject\Component\VCard;
  39. use Sabre\VObject\Reader;
  40. class ContactsSearchProvider implements IFilteringProvider {
  41. private static array $searchPropertiesRestricted = [
  42. 'N',
  43. 'FN',
  44. 'NICKNAME',
  45. 'EMAIL',
  46. ];
  47. private static array $searchProperties = [
  48. 'N',
  49. 'FN',
  50. 'NICKNAME',
  51. 'EMAIL',
  52. 'TEL',
  53. 'ADR',
  54. 'TITLE',
  55. 'ORG',
  56. 'NOTE',
  57. ];
  58. public function __construct(
  59. private IAppManager $appManager,
  60. private IL10N $l10n,
  61. private IURLGenerator $urlGenerator,
  62. private CardDavBackend $backend,
  63. ) {
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. public function getId(): string {
  69. return 'contacts';
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function getName(): string {
  75. return $this->l10n->t('Contacts');
  76. }
  77. public function getOrder(string $route, array $routeParameters): ?int {
  78. if ($this->appManager->isEnabledForUser('contacts')) {
  79. return $route === 'contacts.Page.index' ? -1 : 25;
  80. }
  81. return null;
  82. }
  83. public function search(IUser $user, ISearchQuery $query): SearchResult {
  84. if (!$this->appManager->isEnabledForUser('contacts', $user)) {
  85. return SearchResult::complete($this->getName(), []);
  86. }
  87. $principalUri = 'principals/users/' . $user->getUID();
  88. $addressBooks = $this->backend->getAddressBooksForUser($principalUri);
  89. $addressBooksById = [];
  90. foreach ($addressBooks as $addressBook) {
  91. $addressBooksById[(int) $addressBook['id']] = $addressBook;
  92. }
  93. $searchResults = $this->backend->searchPrincipalUri(
  94. $principalUri,
  95. $query->getFilter('term')?->get() ?? '',
  96. $query->getFilter('title-only')?->get() ? self::$searchPropertiesRestricted : self::$searchProperties,
  97. [
  98. 'limit' => $query->getLimit(),
  99. 'offset' => $query->getCursor(),
  100. 'since' => $query->getFilter('since'),
  101. 'until' => $query->getFilter('until'),
  102. 'person' => $this->getPersonDisplayName($query->getFilter('person')),
  103. 'company' => $query->getFilter('company'),
  104. ],
  105. );
  106. $formattedResults = \array_map(function (array $contactRow) use ($addressBooksById):SearchResultEntry {
  107. $addressBook = $addressBooksById[$contactRow['addressbookid']];
  108. /** @var VCard $vCard */
  109. $vCard = Reader::read($contactRow['carddata']);
  110. $thumbnailUrl = '';
  111. if ($vCard->PHOTO) {
  112. $thumbnailUrl = $this->getDavUrlForContact($addressBook['principaluri'], $addressBook['uri'], $contactRow['uri']) . '?photo';
  113. }
  114. $title = (string)$vCard->FN;
  115. $subline = $this->generateSubline($vCard);
  116. $resourceUrl = $this->getDeepLinkToContactsApp($addressBook['uri'], (string) $vCard->UID);
  117. return new SearchResultEntry($thumbnailUrl, $title, $subline, $resourceUrl, 'icon-contacts-dark', true);
  118. }, $searchResults);
  119. return SearchResult::paginated(
  120. $this->getName(),
  121. $formattedResults,
  122. $query->getCursor() + count($formattedResults)
  123. );
  124. }
  125. private function getPersonDisplayName(?IFilter $person): ?string {
  126. $user = $person?->get();
  127. if ($user instanceof IUser) {
  128. return $user->getDisplayName();
  129. }
  130. return null;
  131. }
  132. protected function getDavUrlForContact(
  133. string $principalUri,
  134. string $addressBookUri,
  135. string $contactsUri,
  136. ): string {
  137. [, $principalType, $principalId] = explode('/', $principalUri, 3);
  138. return $this->urlGenerator->getAbsoluteURL(
  139. $this->urlGenerator->linkTo('', 'remote.php') . '/dav/addressbooks/'
  140. . $principalType . '/'
  141. . $principalId . '/'
  142. . $addressBookUri . '/'
  143. . $contactsUri
  144. );
  145. }
  146. protected function getDeepLinkToContactsApp(
  147. string $addressBookUri,
  148. string $contactUid,
  149. ): string {
  150. return $this->urlGenerator->getAbsoluteURL(
  151. $this->urlGenerator->linkToRoute('contacts.contacts.direct', [
  152. 'contact' => $contactUid . '~' . $addressBookUri
  153. ])
  154. );
  155. }
  156. protected function generateSubline(VCard $vCard): string {
  157. $emailAddresses = $vCard->select('EMAIL');
  158. if (!is_array($emailAddresses) || empty($emailAddresses)) {
  159. return '';
  160. }
  161. return (string)$emailAddresses[0];
  162. }
  163. public function getSupportedFilters(): array {
  164. return [
  165. 'term',
  166. 'since',
  167. 'until',
  168. 'person',
  169. 'title-only',
  170. ];
  171. }
  172. public function getAlternateIds(): array {
  173. return [];
  174. }
  175. public function getCustomFilters(): array {
  176. return [
  177. new FilterDefinition('company'),
  178. ];
  179. }
  180. }