Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Reference): Add public API endpoints to get references #46378

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions core/Controller/ReferenceApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OC\Core\ResponseDefinitions;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\DataResponse;
use OCP\Collaboration\Reference\IDiscoverableReferenceProvider;
Expand All @@ -22,6 +23,8 @@
* @psalm-import-type CoreReferenceProvider from ResponseDefinitions
*/
class ReferenceApiController extends \OCP\AppFramework\OCSController {
private const LIMIT_MAX = 15;

public function __construct(
string $appName,
IRequest $request,
Expand Down Expand Up @@ -62,6 +65,39 @@ public function extract(string $text, bool $resolve = false, int $limit = 1): Da
]);
}

/**
* @PublicPage
*
* Extract references from a text
*
* @param string $text Text to extract from
* @param string $sharingToken Token of the public share
* @param bool $resolve Resolve the references
* @param int $limit Maximum amount of references to extract, limited to 15
* @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|null>}, array{}>
*
* 200: References returned
*/
#[ApiRoute(verb: 'POST', url: '/extractPublic', root: '/references')]
mejo- marked this conversation as resolved.
Show resolved Hide resolved
#[AnonRateLimit(limit: 10, period: 120)]
public function extractPublic(string $text, string $sharingToken, bool $resolve = false, int $limit = 1): DataResponse {
mejo- marked this conversation as resolved.
Show resolved Hide resolved
$references = $this->referenceManager->extractReferences($text);

$result = [];
$index = 0;
foreach ($references as $reference) {
if ($index++ >= min($limit, self::LIMIT_MAX)) {
break;
}

$result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference, true, $sharingToken)?->jsonSerialize() : null;
}

return new DataResponse([
'references' => $result
]);
}

/**
* @NoAdminRequired
*
Expand All @@ -73,6 +109,7 @@ public function extract(string $text, bool $resolve = false, int $limit = 1): Da
* 200: Reference returned
*/
#[ApiRoute(verb: 'GET', url: '/resolve', root: '/references')]
#[AnonRateLimit(limit: 10, period: 120)]
public function resolveOne(string $reference): DataResponse {
/** @var ?CoreReference $resolvedReference */
$resolvedReference = $this->referenceManager->resolveReference(trim($reference))?->jsonSerialize();
Expand All @@ -82,6 +119,28 @@ public function resolveOne(string $reference): DataResponse {
return $response;
}

/**
* @PublicPage
*
* Resolve from a public page
*
* @param string $reference Reference to resolve
* @param string $sharingToken Token of the public share
* @return DataResponse<Http::STATUS_OK, array{references: array<string, ?CoreReference>}, array{}>
*
* 200: Reference returned
*/
#[ApiRoute(verb: 'GET', url: '/resolvePublic', root: '/references')]
#[AnonRateLimit(limit: 10, period: 120)]
public function resolveOnePublic(string $reference, string $sharingToken): DataResponse {
/** @var ?CoreReference $resolvedReference */
$resolvedReference = $this->referenceManager->resolveReference(trim($reference), true, trim($sharingToken))?->jsonSerialize();

$response = new DataResponse(['references' => [$reference => $resolvedReference]]);
$response->cacheFor(3600, false, true);
return $response;
}

/**
* @NoAdminRequired
*
Expand Down Expand Up @@ -110,6 +169,36 @@ public function resolve(array $references, int $limit = 1): DataResponse {
]);
}

/**
* @PublicPage
*
* Resolve multiple references from a public page
*
* @param string[] $references References to resolve
* @param string $sharingToken Token of the public share
* @param int $limit Maximum amount of references to resolve, limited to 15
* @return DataResponse<Http::STATUS_OK, array{references: array<string, CoreReference|null>}, array{}>
*
* 200: References returned
*/
#[ApiRoute(verb: 'POST', url: '/resolvePublic', root: '/references')]
#[AnonRateLimit(limit: 10, period: 120)]
public function resolvePublic(array $references, string $sharingToken, int $limit = 1): DataResponse {
mejo- marked this conversation as resolved.
Show resolved Hide resolved
$result = [];
$index = 0;
foreach ($references as $reference) {
if ($index++ >= min($limit, self::LIMIT_MAX)) {
break;
}

$result[$reference] = $this->referenceManager->resolveReference($reference, true, $sharingToken)?->jsonSerialize();
}

return new DataResponse([
'references' => $result
]);
}

/**
* @NoAdminRequired
*
Expand Down
Loading
Loading