1
0

LegacyProvider.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 OCP\Comments\ICommentsManager;
  32. use function count;
  33. class LegacyProvider extends Provider {
  34. /**
  35. * Search for $query
  36. *
  37. * @param string $query
  38. * @return array An array of OCP\Search\Result's
  39. * @since 7.0.0
  40. */
  41. public function search($query): array {
  42. $cm = \OC::$server->get(ICommentsManager::class);
  43. $us = \OC::$server->getUserSession();
  44. $user = $us->getUser();
  45. if (!$user instanceof IUser) {
  46. return [];
  47. }
  48. $uf = \OC::$server->getUserFolder($user->getUID());
  49. if ($uf === null) {
  50. return [];
  51. }
  52. $result = [];
  53. $numComments = 50;
  54. $offset = 0;
  55. while (count($result) < $numComments) {
  56. /** @var IComment[] $comments */
  57. $comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments);
  58. foreach ($comments as $comment) {
  59. if ($comment->getActorType() !== 'users') {
  60. continue;
  61. }
  62. $displayName = $cm->resolveDisplayName('user', $comment->getActorId());
  63. try {
  64. $file = $this->getFileForComment($uf, $comment);
  65. $result[] = new Result($query,
  66. $comment,
  67. $displayName,
  68. $file->getPath()
  69. );
  70. } catch (NotFoundException $e) {
  71. continue;
  72. }
  73. }
  74. if (count($comments) < $numComments) {
  75. // Didn't find more comments when we tried to get, so there are no more comments.
  76. return $result;
  77. }
  78. $offset += $numComments;
  79. $numComments = 50 - count($result);
  80. }
  81. return $result;
  82. }
  83. /**
  84. * @param Folder $userFolder
  85. * @param IComment $comment
  86. * @return Node
  87. * @throws NotFoundException
  88. */
  89. protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
  90. $nodes = $userFolder->getById((int) $comment->getObjectId());
  91. if (empty($nodes)) {
  92. throw new NotFoundException('File not found');
  93. }
  94. return array_shift($nodes);
  95. }
  96. }