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.

LegacyProvider.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Comments\Search;
  25. use OCP\Comments\IComment;
  26. use OCP\Files\Folder;
  27. use OCP\Files\Node;
  28. use OCP\Files\NotFoundException;
  29. use OCP\IUser;
  30. use OCP\Search\Provider;
  31. use function count;
  32. class LegacyProvider extends Provider {
  33. /**
  34. * Search for $query
  35. *
  36. * @param string $query
  37. * @return array An array of OCP\Search\Result's
  38. * @since 7.0.0
  39. */
  40. public function search($query): array {
  41. $cm = \OC::$server->getCommentsManager();
  42. $us = \OC::$server->getUserSession();
  43. $user = $us->getUser();
  44. if (!$user instanceof IUser) {
  45. return [];
  46. }
  47. $uf = \OC::$server->getUserFolder($user->getUID());
  48. if ($uf === null) {
  49. return [];
  50. }
  51. $result = [];
  52. $numComments = 50;
  53. $offset = 0;
  54. while (count($result) < $numComments) {
  55. /** @var IComment[] $comments */
  56. $comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments);
  57. foreach ($comments as $comment) {
  58. if ($comment->getActorType() !== 'users') {
  59. continue;
  60. }
  61. $displayName = $cm->resolveDisplayName('user', $comment->getActorId());
  62. try {
  63. $file = $this->getFileForComment($uf, $comment);
  64. $result[] = new Result($query,
  65. $comment,
  66. $displayName,
  67. $file->getPath()
  68. );
  69. } catch (NotFoundException $e) {
  70. continue;
  71. }
  72. }
  73. if (count($comments) < $numComments) {
  74. // Didn't find more comments when we tried to get, so there are no more comments.
  75. return $result;
  76. }
  77. $offset += $numComments;
  78. $numComments = 50 - count($result);
  79. }
  80. return $result;
  81. }
  82. /**
  83. * @param Folder $userFolder
  84. * @param IComment $comment
  85. * @return Node
  86. * @throws NotFoundException
  87. */
  88. protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
  89. $nodes = $userFolder->getById((int) $comment->getObjectId());
  90. if (empty($nodes)) {
  91. throw new NotFoundException('File not found');
  92. }
  93. return array_shift($nodes);
  94. }
  95. }