123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- declare(strict_types=1);
- namespace OC\Core\Controller;
- use OCA\Core\ResponseDefinitions;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\Attribute\ApiRoute;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\Collaboration\Reference\IDiscoverableReferenceProvider;
- use OCP\Collaboration\Reference\IReferenceManager;
- use OCP\Collaboration\Reference\Reference;
- use OCP\IRequest;
- class ReferenceApiController extends \OCP\AppFramework\OCSController {
- public function __construct(
- string $appName,
- IRequest $request,
- private IReferenceManager $referenceManager,
- private ?string $userId,
- ) {
- parent::__construct($appName, $request);
- }
-
-
- public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse {
- $references = $this->referenceManager->extractReferences($text);
- $result = [];
- $index = 0;
- foreach ($references as $reference) {
- if ($index++ >= $limit) {
- break;
- }
- $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference)->jsonSerialize() : null;
- }
- return new DataResponse([
- 'references' => $result
- ]);
- }
-
-
- public function resolveOne(string $reference): DataResponse {
-
- $resolvedReference = $this->referenceManager->resolveReference(trim($reference))?->jsonSerialize();
- $response = new DataResponse(['references' => [$reference => $resolvedReference]]);
- $response->cacheFor(3600, false, true);
- return $response;
- }
-
-
- public function resolve(array $references, int $limit = 1): DataResponse {
- $result = [];
- $index = 0;
- foreach ($references as $reference) {
- if ($index++ >= $limit) {
- break;
- }
- $result[$reference] = $this->referenceManager->resolveReference($reference)?->jsonSerialize();
- }
- return new DataResponse([
- 'references' => $result
- ]);
- }
-
-
- public function getProvidersInfo(): DataResponse {
- $providers = $this->referenceManager->getDiscoverableProviders();
- $jsonProviders = array_map(static function (IDiscoverableReferenceProvider $provider) {
- return $provider->jsonSerialize();
- }, $providers);
- return new DataResponse($jsonProviders);
- }
-
-
- public function touchProvider(string $providerId, ?int $timestamp = null): DataResponse {
- if ($this->userId !== null) {
- $success = $this->referenceManager->touchProvider($this->userId, $providerId, $timestamp);
- return new DataResponse(['success' => $success]);
- }
- return new DataResponse(['success' => false]);
- }
- }
|