ReferenceApiController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. namespace OC\Core\Controller;
  25. use OCA\Core\ResponseDefinitions;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\Collaboration\Reference\IDiscoverableReferenceProvider;
  29. use OCP\Collaboration\Reference\IReferenceManager;
  30. use OCP\Collaboration\Reference\Reference;
  31. use OCP\IRequest;
  32. /**
  33. * @psalm-import-type CoreReference from ResponseDefinitions
  34. * @psalm-import-type CoreReferenceProvider from ResponseDefinitions
  35. */
  36. class ReferenceApiController extends \OCP\AppFramework\OCSController {
  37. public function __construct(
  38. string $appName,
  39. IRequest $request,
  40. private IReferenceManager $referenceManager,
  41. private ?string $userId,
  42. ) {
  43. parent::__construct($appName, $request);
  44. }
  45. /**
  46. * @NoAdminRequired
  47. *
  48. * Extract references from a text
  49. *
  50. * @param string $text Text to extract from
  51. * @param bool $resolve Resolve the references
  52. * @param int $limit Maximum amount of references to extract
  53. * @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|mixed|null>}, array{}>
  54. */
  55. public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse {
  56. $references = $this->referenceManager->extractReferences($text);
  57. $result = [];
  58. $index = 0;
  59. foreach ($references as $reference) {
  60. if ($index++ >= $limit) {
  61. break;
  62. }
  63. $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference)->jsonSerialize() : null;
  64. }
  65. return new DataResponse([
  66. 'references' => $result
  67. ]);
  68. }
  69. /**
  70. * @NoAdminRequired
  71. *
  72. * Resolve a reference
  73. *
  74. * @param string $reference Reference to resolve
  75. * @return DataResponse<Http::STATUS_OK, array{references: array<string, ?CoreReference>}, array{}>
  76. */
  77. public function resolveOne(string $reference): DataResponse {
  78. /** @var ?CoreReference $resolvedReference */
  79. $resolvedReference = $this->referenceManager->resolveReference(trim($reference))?->jsonSerialize();
  80. $response = new DataResponse(['references' => [$reference => $resolvedReference]]);
  81. $response->cacheFor(3600, false, true);
  82. return $response;
  83. }
  84. /**
  85. * @NoAdminRequired
  86. *
  87. * Resolve multiple references
  88. *
  89. * @param string[] $references References to resolve
  90. * @param int $limit Maximum amount of references to resolve
  91. * @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|mixed|null>}, array{}>
  92. */
  93. public function resolve(array $references, int $limit = 1): DataResponse {
  94. $result = [];
  95. $index = 0;
  96. foreach ($references as $reference) {
  97. if ($index++ >= $limit) {
  98. break;
  99. }
  100. $result[$reference] = $this->referenceManager->resolveReference($reference)?->jsonSerialize();
  101. }
  102. return new DataResponse([
  103. 'references' => $result
  104. ]);
  105. }
  106. /**
  107. * @NoAdminRequired
  108. *
  109. * Get the providers
  110. *
  111. * @return DataResponse<Http::STATUS_OK, CoreReferenceProvider[], array{}>
  112. */
  113. public function getProvidersInfo(): DataResponse {
  114. $providers = $this->referenceManager->getDiscoverableProviders();
  115. $jsonProviders = array_map(static function (IDiscoverableReferenceProvider $provider) {
  116. return $provider->jsonSerialize();
  117. }, $providers);
  118. return new DataResponse($jsonProviders);
  119. }
  120. /**
  121. * @NoAdminRequired
  122. *
  123. * Touch a provider
  124. *
  125. * @param string $providerId ID of the provider
  126. * @param int|null $timestamp Timestamp of the last usage
  127. * @return DataResponse<Http::STATUS_OK, array{success: bool}, array{}>
  128. */
  129. public function touchProvider(string $providerId, ?int $timestamp = null): DataResponse {
  130. if ($this->userId !== null) {
  131. $success = $this->referenceManager->touchProvider($this->userId, $providerId, $timestamp);
  132. return new DataResponse(['success' => $success]);
  133. }
  134. return new DataResponse(['success' => false]);
  135. }
  136. }