CommentsSearchProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. private IUserManager $userManager;
  39. private IL10N $l10n;
  40. private IURLGenerator $urlGenerator;
  41. private LegacyProvider $legacyProvider;
  42. public function __construct(IUserManager $userManager,
  43. IL10N $l10n,
  44. IURLGenerator $urlGenerator,
  45. LegacyProvider $legacyProvider) {
  46. $this->userManager = $userManager;
  47. $this->l10n = $l10n;
  48. $this->urlGenerator = $urlGenerator;
  49. $this->legacyProvider = $legacyProvider;
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. public function getId(): string {
  55. return 'comments';
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. public function getName(): string {
  61. return $this->l10n->t('Comments');
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. public function getOrder(string $route, array $routeParameters): int {
  67. if ($route === 'files.View.index') {
  68. // Files first
  69. return 0;
  70. }
  71. return 10;
  72. }
  73. /**
  74. * @inheritDoc
  75. */
  76. public function search(IUser $user, ISearchQuery $query): SearchResult {
  77. return SearchResult::complete(
  78. $this->l10n->t('Comments'),
  79. array_map(function (Result $result) {
  80. $path = $result->path;
  81. $pathInfo = pathinfo($path);
  82. $isUser = $this->userManager->userExists($result->authorId);
  83. $avatarUrl = $isUser
  84. ? $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $result->authorId, 'size' => 42])
  85. : $this->urlGenerator->linkToRouteAbsolute('core.GuestAvatar.getAvatar', ['guestName' => $result->authorId, 'size' => 42]);
  86. return new SearchResultEntry(
  87. $avatarUrl,
  88. $result->name,
  89. $path,
  90. $this->urlGenerator->linkToRouteAbsolute('files.view.index',[
  91. 'dir' => $pathInfo['dirname'],
  92. 'scrollto' => $pathInfo['basename'],
  93. ]),
  94. '',
  95. true
  96. );
  97. }, $this->legacyProvider->search($query->getTerm()))
  98. );
  99. }
  100. }