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.

90 lines
2.4 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  5. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.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\Spreed\Collaboration\Collaborators;
  24. use OCA\Spreed\Manager;
  25. use OCA\Spreed\Room;
  26. use OCP\Collaboration\Collaborators\ISearchPlugin;
  27. use OCP\Collaboration\Collaborators\ISearchResult;
  28. use OCP\Collaboration\Collaborators\SearchResultType;
  29. use OCP\IUserSession;
  30. use OCP\Share;
  31. class RoomPlugin implements ISearchPlugin {
  32. /** @var Manager */
  33. private $manager;
  34. /** @var IUserSession */
  35. private $userSession;
  36. public function __construct(Manager $manager,
  37. IUserSession $userSession) {
  38. $this->manager = $manager;
  39. $this->userSession = $userSession;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  45. if (empty($search)) {
  46. return false;
  47. }
  48. $userId = $this->userSession->getUser()->getUID();
  49. $result = ['wide' => [], 'exact' => []];
  50. $rooms = $this->manager->getRoomsForParticipant($userId);
  51. foreach ($rooms as $room) {
  52. if (stripos($room->getName(), $search) !== false) {
  53. $item = $this->roomToSearchResultItem($room, $userId);
  54. if (strtolower($item['label']) === strtolower($search)) {
  55. $result['exact'][] = $item;
  56. } else {
  57. $result['wide'][] = $item;
  58. }
  59. }
  60. }
  61. $type = new SearchResultType('rooms');
  62. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  63. return false;
  64. }
  65. private function roomToSearchResultItem(Room $room, string $userId): array {
  66. return
  67. [
  68. 'label' => $room->getDisplayName($userId),
  69. 'value' => [
  70. 'shareType' => Share::SHARE_TYPE_ROOM,
  71. 'shareWith' => $room->getToken()
  72. ]
  73. ];
  74. }
  75. }