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.

Provider.php 2.7KB

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