CommentsSearchProvider.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  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\Comments\Search;
  27. use OCP\IL10N;
  28. use OCP\IURLGenerator;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use OCP\Search\IProvider;
  32. use OCP\Search\ISearchQuery;
  33. use OCP\Search\SearchResult;
  34. use OCP\Search\SearchResultEntry;
  35. use function array_map;
  36. use function pathinfo;
  37. class CommentsSearchProvider implements IProvider {
  38. public function __construct(
  39. private IUserManager $userManager,
  40. private IL10N $l10n,
  41. private IURLGenerator $urlGenerator,
  42. private LegacyProvider $legacyProvider,
  43. ) {
  44. }
  45. public function getId(): string {
  46. return 'comments';
  47. }
  48. public function getName(): string {
  49. return $this->l10n->t('Comments');
  50. }
  51. public function getOrder(string $route, array $routeParameters): int {
  52. if ($route === 'files.View.index') {
  53. // Files first
  54. return 0;
  55. }
  56. return 10;
  57. }
  58. public function search(IUser $user, ISearchQuery $query): SearchResult {
  59. return SearchResult::complete(
  60. $this->l10n->t('Comments'),
  61. array_map(function (Result $result) {
  62. $path = $result->path;
  63. $pathInfo = pathinfo($path);
  64. $isUser = $this->userManager->userExists($result->authorId);
  65. $avatarUrl = $isUser
  66. ? $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $result->authorId, 'size' => 42])
  67. : $this->urlGenerator->linkToRouteAbsolute('core.GuestAvatar.getAvatar', ['guestName' => $result->authorId, 'size' => 42]);
  68. return new SearchResultEntry(
  69. $avatarUrl,
  70. $result->name,
  71. $path,
  72. $this->urlGenerator->linkToRouteAbsolute('files.view.index', [
  73. 'dir' => $pathInfo['dirname'],
  74. 'scrollto' => $pathInfo['basename'],
  75. ]),
  76. '',
  77. true
  78. );
  79. }, $this->legacyProvider->search($query->getTerm()))
  80. );
  81. }
  82. }