Provider.php 2.6 KB

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