ReferenceApiController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Core\ResponseDefinitions;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  13. use OCP\AppFramework\Http\Attribute\PublicPage;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\Collaboration\Reference\IDiscoverableReferenceProvider;
  16. use OCP\Collaboration\Reference\IReferenceManager;
  17. use OCP\Collaboration\Reference\Reference;
  18. use OCP\IRequest;
  19. /**
  20. * @psalm-import-type CoreReference from ResponseDefinitions
  21. * @psalm-import-type CoreReferenceProvider from ResponseDefinitions
  22. */
  23. class ReferenceApiController extends \OCP\AppFramework\OCSController {
  24. private const LIMIT_MAX = 15;
  25. public function __construct(
  26. string $appName,
  27. IRequest $request,
  28. private IReferenceManager $referenceManager,
  29. private ?string $userId,
  30. ) {
  31. parent::__construct($appName, $request);
  32. }
  33. /**
  34. * Extract references from a text
  35. *
  36. * @param string $text Text to extract from
  37. * @param bool $resolve Resolve the references
  38. * @param int $limit Maximum amount of references to extract
  39. * @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|null>}, array{}>
  40. *
  41. * 200: References returned
  42. */
  43. #[NoAdminRequired]
  44. #[ApiRoute(verb: 'POST', url: '/extract', root: '/references')]
  45. public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse {
  46. $references = $this->referenceManager->extractReferences($text);
  47. $result = [];
  48. $index = 0;
  49. foreach ($references as $reference) {
  50. if ($index++ >= $limit) {
  51. break;
  52. }
  53. $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference)->jsonSerialize() : null;
  54. }
  55. return new DataResponse([
  56. 'references' => $result
  57. ]);
  58. }
  59. /**
  60. * Extract references from a text
  61. *
  62. * @param string $text Text to extract from
  63. * @param string $sharingToken Token of the public share
  64. * @param bool $resolve Resolve the references
  65. * @param int $limit Maximum amount of references to extract, limited to 15
  66. * @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|null>}, array{}>
  67. *
  68. * 200: References returned
  69. */
  70. #[ApiRoute(verb: 'POST', url: '/extractPublic', root: '/references')]
  71. #[PublicPage]
  72. #[AnonRateLimit(limit: 10, period: 120)]
  73. public function extractPublic(string $text, string $sharingToken, bool $resolve = false, int $limit = 1): DataResponse {
  74. $references = $this->referenceManager->extractReferences($text);
  75. $result = [];
  76. $index = 0;
  77. foreach ($references as $reference) {
  78. if ($index++ >= min($limit, self::LIMIT_MAX)) {
  79. break;
  80. }
  81. $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference, true, $sharingToken)?->jsonSerialize() : null;
  82. }
  83. return new DataResponse([
  84. 'references' => $result
  85. ]);
  86. }
  87. /**
  88. * Resolve a reference
  89. *
  90. * @param string $reference Reference to resolve
  91. * @return DataResponse<Http::STATUS_OK, array{references: array<string, ?CoreReference>}, array{}>
  92. *
  93. * 200: Reference returned
  94. */
  95. #[NoAdminRequired]
  96. #[ApiRoute(verb: 'GET', url: '/resolve', root: '/references')]
  97. public function resolveOne(string $reference): DataResponse {
  98. /** @var ?CoreReference $resolvedReference */
  99. $resolvedReference = $this->referenceManager->resolveReference(trim($reference))?->jsonSerialize();
  100. $response = new DataResponse(['references' => [$reference => $resolvedReference]]);
  101. $response->cacheFor(3600, false, true);
  102. return $response;
  103. }
  104. /**
  105. * Resolve from a public page
  106. *
  107. * @param string $reference Reference to resolve
  108. * @param string $sharingToken Token of the public share
  109. * @return DataResponse<Http::STATUS_OK, array{references: array<string, ?CoreReference>}, array{}>
  110. *
  111. * 200: Reference returned
  112. */
  113. #[ApiRoute(verb: 'GET', url: '/resolvePublic', root: '/references')]
  114. #[PublicPage]
  115. #[AnonRateLimit(limit: 10, period: 120)]
  116. public function resolveOnePublic(string $reference, string $sharingToken): DataResponse {
  117. /** @var ?CoreReference $resolvedReference */
  118. $resolvedReference = $this->referenceManager->resolveReference(trim($reference), true, trim($sharingToken))?->jsonSerialize();
  119. $response = new DataResponse(['references' => [$reference => $resolvedReference]]);
  120. $response->cacheFor(3600, false, true);
  121. return $response;
  122. }
  123. /**
  124. * Resolve multiple references
  125. *
  126. * @param list<string> $references References to resolve
  127. * @param int $limit Maximum amount of references to resolve
  128. * @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|null>}, array{}>
  129. *
  130. * 200: References returned
  131. */
  132. #[NoAdminRequired]
  133. #[ApiRoute(verb: 'POST', url: '/resolve', root: '/references')]
  134. public function resolve(array $references, int $limit = 1): DataResponse {
  135. $result = [];
  136. $index = 0;
  137. foreach ($references as $reference) {
  138. if ($index++ >= $limit) {
  139. break;
  140. }
  141. $result[$reference] = $this->referenceManager->resolveReference($reference)?->jsonSerialize();
  142. }
  143. return new DataResponse([
  144. 'references' => $result
  145. ]);
  146. }
  147. /**
  148. * Resolve multiple references from a public page
  149. *
  150. * @param list<string> $references References to resolve
  151. * @param string $sharingToken Token of the public share
  152. * @param int $limit Maximum amount of references to resolve, limited to 15
  153. * @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|null>}, array{}>
  154. *
  155. * 200: References returned
  156. */
  157. #[ApiRoute(verb: 'POST', url: '/resolvePublic', root: '/references')]
  158. #[PublicPage]
  159. #[AnonRateLimit(limit: 10, period: 120)]
  160. public function resolvePublic(array $references, string $sharingToken, int $limit = 1): DataResponse {
  161. $result = [];
  162. $index = 0;
  163. foreach ($references as $reference) {
  164. if ($index++ >= min($limit, self::LIMIT_MAX)) {
  165. break;
  166. }
  167. $result[$reference] = $this->referenceManager->resolveReference($reference, true, $sharingToken)?->jsonSerialize();
  168. }
  169. return new DataResponse([
  170. 'references' => $result
  171. ]);
  172. }
  173. /**
  174. * Get the providers
  175. *
  176. * @return DataResponse<Http::STATUS_OK, list<CoreReferenceProvider>, array{}>
  177. *
  178. * 200: Providers returned
  179. */
  180. #[NoAdminRequired]
  181. #[ApiRoute(verb: 'GET', url: '/providers', root: '/references')]
  182. public function getProvidersInfo(): DataResponse {
  183. $providers = $this->referenceManager->getDiscoverableProviders();
  184. $jsonProviders = array_values(array_map(static function (IDiscoverableReferenceProvider $provider) {
  185. return $provider->jsonSerialize();
  186. }, $providers));
  187. return new DataResponse($jsonProviders);
  188. }
  189. /**
  190. * Touch a provider
  191. *
  192. * @param string $providerId ID of the provider
  193. * @param int|null $timestamp Timestamp of the last usage
  194. * @return DataResponse<Http::STATUS_OK, array{success: bool}, array{}>
  195. *
  196. * 200: Provider touched
  197. */
  198. #[NoAdminRequired]
  199. #[ApiRoute(verb: 'PUT', url: '/provider/{providerId}', root: '/references')]
  200. public function touchProvider(string $providerId, ?int $timestamp = null): DataResponse {
  201. if ($this->userId !== null) {
  202. $success = $this->referenceManager->touchProvider($this->userId, $providerId, $timestamp);
  203. return new DataResponse(['success' => $success]);
  204. }
  205. return new DataResponse(['success' => false]);
  206. }
  207. }