ContactsSearchProvider.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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) <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\IProvider;
  33. use OCP\Search\ISearchQuery;
  34. use OCP\Search\SearchResult;
  35. use OCP\Search\SearchResultEntry;
  36. use Sabre\VObject\Component\VCard;
  37. use Sabre\VObject\Reader;
  38. class ContactsSearchProvider implements IProvider {
  39. /** @var IAppManager */
  40. private $appManager;
  41. /** @var IL10N */
  42. private $l10n;
  43. /** @var IURLGenerator */
  44. private $urlGenerator;
  45. /** @var CardDavBackend */
  46. private $backend;
  47. /**
  48. * @var string[]
  49. */
  50. private static $searchProperties = [
  51. 'N',
  52. 'FN',
  53. 'NICKNAME',
  54. 'EMAIL',
  55. 'TEL',
  56. 'ADR',
  57. 'TITLE',
  58. 'ORG',
  59. 'NOTE',
  60. ];
  61. /**
  62. * ContactsSearchProvider constructor.
  63. *
  64. * @param IAppManager $appManager
  65. * @param IL10N $l10n
  66. * @param IURLGenerator $urlGenerator
  67. * @param CardDavBackend $backend
  68. */
  69. public function __construct(IAppManager $appManager,
  70. IL10N $l10n,
  71. IURLGenerator $urlGenerator,
  72. CardDavBackend $backend) {
  73. $this->appManager = $appManager;
  74. $this->l10n = $l10n;
  75. $this->urlGenerator = $urlGenerator;
  76. $this->backend = $backend;
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. public function getId(): string {
  82. return 'contacts';
  83. }
  84. /**
  85. * @inheritDoc
  86. */
  87. public function getName(): string {
  88. return $this->l10n->t('Contacts');
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. public function getOrder(string $route, array $routeParameters): int {
  94. if ($route === 'contacts.Page.index') {
  95. return -1;
  96. }
  97. return 25;
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function search(IUser $user, ISearchQuery $query): SearchResult {
  103. if (!$this->appManager->isEnabledForUser('contacts', $user)) {
  104. return SearchResult::complete($this->getName(), []);
  105. }
  106. $principalUri = 'principals/users/' . $user->getUID();
  107. $addressBooks = $this->backend->getAddressBooksForUser($principalUri);
  108. $addressBooksById = [];
  109. foreach ($addressBooks as $addressBook) {
  110. $addressBooksById[(int) $addressBook['id']] = $addressBook;
  111. }
  112. $searchResults = $this->backend->searchPrincipalUri(
  113. $principalUri,
  114. $query->getTerm(),
  115. self::$searchProperties,
  116. [
  117. 'limit' => $query->getLimit(),
  118. 'offset' => $query->getCursor(),
  119. ]
  120. );
  121. $formattedResults = \array_map(function (array $contactRow) use ($addressBooksById):SearchResultEntry {
  122. $addressBook = $addressBooksById[$contactRow['addressbookid']];
  123. /** @var VCard $vCard */
  124. $vCard = Reader::read($contactRow['carddata']);
  125. $thumbnailUrl = '';
  126. if ($vCard->PHOTO) {
  127. $thumbnailUrl = $this->getDavUrlForContact($addressBook['principaluri'], $addressBook['uri'], $contactRow['uri']) . '?photo';
  128. }
  129. $title = (string)$vCard->FN;
  130. $subline = $this->generateSubline($vCard);
  131. $resourceUrl = $this->getDeepLinkToContactsApp($addressBook['uri'], (string) $vCard->UID);
  132. return new SearchResultEntry($thumbnailUrl, $title, $subline, $resourceUrl, 'icon-contacts-dark', true);
  133. }, $searchResults);
  134. return SearchResult::paginated(
  135. $this->getName(),
  136. $formattedResults,
  137. $query->getCursor() + count($formattedResults)
  138. );
  139. }
  140. /**
  141. * @param string $principalUri
  142. * @param string $addressBookUri
  143. * @param string $contactsUri
  144. * @return string
  145. */
  146. protected function getDavUrlForContact(string $principalUri,
  147. string $addressBookUri,
  148. string $contactsUri): string {
  149. [, $principalType, $principalId] = explode('/', $principalUri, 3);
  150. return $this->urlGenerator->getAbsoluteURL(
  151. $this->urlGenerator->linkTo('', 'remote.php') . '/dav/addressbooks/'
  152. . $principalType . '/'
  153. . $principalId . '/'
  154. . $addressBookUri . '/'
  155. . $contactsUri
  156. );
  157. }
  158. /**
  159. * @param string $addressBookUri
  160. * @param string $contactUid
  161. * @return string
  162. */
  163. protected function getDeepLinkToContactsApp(string $addressBookUri,
  164. string $contactUid): string {
  165. return $this->urlGenerator->getAbsoluteURL(
  166. $this->urlGenerator->linkToRoute('contacts.contacts.direct', [
  167. 'contact' => $contactUid . '~' . $addressBookUri
  168. ])
  169. );
  170. }
  171. /**
  172. * @param VCard $vCard
  173. * @return string
  174. */
  175. protected function generateSubline(VCard $vCard): string {
  176. $emailAddresses = $vCard->select('EMAIL');
  177. if (!is_array($emailAddresses) || empty($emailAddresses)) {
  178. return '';
  179. }
  180. return (string)$emailAddresses[0];
  181. }
  182. }