ReferenceApiController.php 7.3 KB

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