ReferenceApiController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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|null>}, array{}>
  54. *
  55. * 200: References returned
  56. */
  57. public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse {
  58. $references = $this->referenceManager->extractReferences($text);
  59. $result = [];
  60. $index = 0;
  61. foreach ($references as $reference) {
  62. if ($index++ >= $limit) {
  63. break;
  64. }
  65. $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference)->jsonSerialize() : null;
  66. }
  67. return new DataResponse([
  68. 'references' => $result
  69. ]);
  70. }
  71. /**
  72. * @NoAdminRequired
  73. *
  74. * Resolve a reference
  75. *
  76. * @param string $reference Reference to resolve
  77. * @return DataResponse<Http::STATUS_OK, array{references: array<string, ?CoreReference>}, array{}>
  78. *
  79. * 200: Reference returned
  80. */
  81. public function resolveOne(string $reference): DataResponse {
  82. /** @var ?CoreReference $resolvedReference */
  83. $resolvedReference = $this->referenceManager->resolveReference(trim($reference))?->jsonSerialize();
  84. $response = new DataResponse(['references' => [$reference => $resolvedReference]]);
  85. $response->cacheFor(3600, false, true);
  86. return $response;
  87. }
  88. /**
  89. * @NoAdminRequired
  90. *
  91. * Resolve multiple references
  92. *
  93. * @param string[] $references References to resolve
  94. * @param int $limit Maximum amount of references to resolve
  95. * @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|null>}, array{}>
  96. *
  97. * 200: References returned
  98. */
  99. public function resolve(array $references, int $limit = 1): DataResponse {
  100. $result = [];
  101. $index = 0;
  102. foreach ($references as $reference) {
  103. if ($index++ >= $limit) {
  104. break;
  105. }
  106. $result[$reference] = $this->referenceManager->resolveReference($reference)?->jsonSerialize();
  107. }
  108. return new DataResponse([
  109. 'references' => $result
  110. ]);
  111. }
  112. /**
  113. * @NoAdminRequired
  114. *
  115. * Get the providers
  116. *
  117. * @return DataResponse<Http::STATUS_OK, CoreReferenceProvider[], array{}>
  118. *
  119. * 200: Providers returned
  120. */
  121. public function getProvidersInfo(): DataResponse {
  122. $providers = $this->referenceManager->getDiscoverableProviders();
  123. $jsonProviders = array_map(static function (IDiscoverableReferenceProvider $provider) {
  124. return $provider->jsonSerialize();
  125. }, $providers);
  126. return new DataResponse($jsonProviders);
  127. }
  128. /**
  129. * @NoAdminRequired
  130. *
  131. * Touch a provider
  132. *
  133. * @param string $providerId ID of the provider
  134. * @param int|null $timestamp Timestamp of the last usage
  135. * @return DataResponse<Http::STATUS_OK, array{success: bool}, array{}>
  136. *
  137. * 200: Provider touched
  138. */
  139. public function touchProvider(string $providerId, ?int $timestamp = null): DataResponse {
  140. if ($this->userId !== null) {
  141. $success = $this->referenceManager->touchProvider($this->userId, $providerId, $timestamp);
  142. return new DataResponse(['success' => $success]);
  143. }
  144. return new DataResponse(['success' => false]);
  145. }
  146. }