CommentsSearchProvider.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Comments\Search;
  8. use OCP\IL10N;
  9. use OCP\IURLGenerator;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use OCP\Search\IProvider;
  13. use OCP\Search\ISearchQuery;
  14. use OCP\Search\SearchResult;
  15. use OCP\Search\SearchResultEntry;
  16. use function array_map;
  17. use function pathinfo;
  18. class CommentsSearchProvider implements IProvider {
  19. public function __construct(
  20. private IUserManager $userManager,
  21. private IL10N $l10n,
  22. private IURLGenerator $urlGenerator,
  23. private LegacyProvider $legacyProvider,
  24. ) {
  25. }
  26. public function getId(): string {
  27. return 'comments';
  28. }
  29. public function getName(): string {
  30. return $this->l10n->t('Comments');
  31. }
  32. public function getOrder(string $route, array $routeParameters): int {
  33. if ($route === 'files.View.index') {
  34. // Files first
  35. return 0;
  36. }
  37. return 10;
  38. }
  39. public function search(IUser $user, ISearchQuery $query): SearchResult {
  40. return SearchResult::complete(
  41. $this->l10n->t('Comments'),
  42. array_map(function (Result $result) {
  43. $path = $result->path;
  44. $pathInfo = pathinfo($path);
  45. $isUser = $this->userManager->userExists($result->authorId);
  46. $avatarUrl = $isUser
  47. ? $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $result->authorId, 'size' => 42])
  48. : $this->urlGenerator->linkToRouteAbsolute('core.GuestAvatar.getAvatar', ['guestName' => $result->authorId, 'size' => 42]);
  49. return new SearchResultEntry(
  50. $avatarUrl,
  51. $result->name,
  52. $path,
  53. $this->urlGenerator->linkToRouteAbsolute('files.view.index', [
  54. 'dir' => $pathInfo['dirname'],
  55. 'scrollto' => $pathInfo['basename'],
  56. ]),
  57. '',
  58. true
  59. );
  60. }, $this->legacyProvider->search($query->getTerm()))
  61. );
  62. }
  63. }