ReferenceApiController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  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. namespace OC\Core\Controller;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\Collaboration\Reference\IDiscoverableReferenceProvider;
  26. use OCP\Collaboration\Reference\IReferenceManager;
  27. use OCP\IRequest;
  28. class ReferenceApiController extends \OCP\AppFramework\OCSController {
  29. private IReferenceManager $referenceManager;
  30. private ?string $userId;
  31. public function __construct(string $appName,
  32. IRequest $request,
  33. IReferenceManager $referenceManager,
  34. ?string $userId) {
  35. parent::__construct($appName, $request);
  36. $this->referenceManager = $referenceManager;
  37. $this->userId = $userId;
  38. }
  39. /**
  40. * @NoAdminRequired
  41. */
  42. public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse {
  43. $references = $this->referenceManager->extractReferences($text);
  44. $result = [];
  45. $index = 0;
  46. foreach ($references as $reference) {
  47. if ($index++ >= $limit) {
  48. break;
  49. }
  50. $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference) : null;
  51. }
  52. return new DataResponse([
  53. 'references' => $result
  54. ]);
  55. }
  56. /**
  57. * @NoAdminRequired
  58. */
  59. public function resolveOne(string $reference): DataResponse {
  60. $resolvedReference = $this->referenceManager->resolveReference(trim($reference));
  61. $response = new DataResponse(['references' => [ $reference => $resolvedReference ]]);
  62. $response->cacheFor(3600, false, true);
  63. return $response;
  64. }
  65. /**
  66. * @NoAdminRequired
  67. *
  68. * @param string[] $references
  69. */
  70. public function resolve(array $references, int $limit = 1): DataResponse {
  71. $result = [];
  72. $index = 0;
  73. foreach ($references as $reference) {
  74. if ($index++ >= $limit) {
  75. break;
  76. }
  77. $result[$reference] = $this->referenceManager->resolveReference($reference);
  78. }
  79. return new DataResponse([
  80. 'references' => array_filter($result)
  81. ]);
  82. }
  83. /**
  84. * @NoAdminRequired
  85. */
  86. public function getProvidersInfo(): DataResponse {
  87. $providers = $this->referenceManager->getDiscoverableProviders();
  88. $jsonProviders = array_map(static function (IDiscoverableReferenceProvider $provider) {
  89. return $provider->jsonSerialize();
  90. }, $providers);
  91. return new DataResponse($jsonProviders);
  92. }
  93. /**
  94. * @NoAdminRequired
  95. */
  96. public function touchProvider(string $providerId, ?int $timestamp = null): DataResponse {
  97. if ($this->userId !== null) {
  98. $success = $this->referenceManager->touchProvider($this->userId, $providerId, $timestamp);
  99. return new DataResponse(['success' => $success]);
  100. }
  101. return new DataResponse(['success' => false]);
  102. }
  103. }