Skip to content

Commit

Permalink
add 'last used timestamp' management for reference providers
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
  • Loading branch information
julien-nc committed Jan 18, 2023
1 parent 7c2780c commit 39ac533
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
20 changes: 19 additions & 1 deletion core/Controller/ReferenceApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@

class ReferenceApiController extends \OCP\AppFramework\OCSController {
private IReferenceManager $referenceManager;
private ?string $userId;

public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager) {
public function __construct(string $appName,
IRequest $request,
IReferenceManager $referenceManager,
?string $userId) {
parent::__construct($appName, $request);
$this->referenceManager = $referenceManager;
$this->userId = $userId;
}

/**
Expand Down Expand Up @@ -102,4 +107,17 @@ public function getProvidersInfo(): DataResponse {
}, $providers);
return new DataResponse($jsonProviders);
}

/**
* @NoAdminRequired
*
* @param string $providerId
* @return DataResponse
*/
public function touchProvider(string $providerId, ?int $timestamp = null): DataResponse {
if ($this->userId !== null) {
$this->referenceManager->touchProvider($this->userId, $providerId, $timestamp);
}
return new DataResponse(['success' => true]);
}
}
1 change: 1 addition & 0 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
['root' => '/references', 'name' => 'ReferenceApi#extract', 'url' => '/extract', 'verb' => 'POST'],
['root' => '/references', 'name' => 'ReferenceApi#resolve', 'url' => '/resolve', 'verb' => 'POST'],
['root' => '/references', 'name' => 'ReferenceApi#getProvidersInfo', 'url' => '/providers', 'verb' => 'GET'],
['root' => '/references', 'name' => 'ReferenceApi#touchProvider', 'url' => '/provider/{providerId}', 'verb' => 'PUT'],

['root' => '/profile', 'name' => 'ProfileApi#setVisibility', 'url' => '/{targetUserId}', 'verb' => 'PUT'],

Expand Down
59 changes: 54 additions & 5 deletions lib/private/Collaboration/Reference/ReferenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
use OCP\Collaboration\Reference\Reference;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Throwable;
Expand All @@ -48,13 +50,23 @@ class ReferenceManager implements IReferenceManager {
private ContainerInterface $container;
private LinkReferenceProvider $linkReferenceProvider;
private LoggerInterface $logger;
private IConfig $config;
private IUserSession $userSession;

public function __construct(LinkReferenceProvider $linkReferenceProvider, ICacheFactory $cacheFactory, Coordinator $coordinator, ContainerInterface $container, LoggerInterface $logger) {
public function __construct(LinkReferenceProvider $linkReferenceProvider,
ICacheFactory $cacheFactory,
Coordinator $coordinator,
ContainerInterface $container,
LoggerInterface $logger,
IConfig $config,
IUserSession $userSession) {
$this->linkReferenceProvider = $linkReferenceProvider;
$this->cache = $cacheFactory->createDistributed('reference');
$this->coordinator = $coordinator;
$this->container = $container;
$this->logger = $logger;
$this->config = $config;
$this->userSession = $userSession;
}

/**
Expand Down Expand Up @@ -216,10 +228,7 @@ public function getProviders(): array {
}

/**
* Get information on discoverable reference providers (id, title, icon and order)
* If the provider is searchable, also get the list of supported unified search providers
*
* @return IDiscoverableReferenceProvider[]
* @inheritDoc
*/
public function getDiscoverableProviders(): array {
// preserve 0 based index to avoid returning an object in data responses
Expand All @@ -229,4 +238,44 @@ public function getDiscoverableProviders(): array {
})
);
}

/**
* @inheritDoc
*/
public function touchProvider(string $userId, string $providerId, ?int $timestamp = null): void {
$providers = $this->getDiscoverableProviders();
$providerIds = array_map(static function (IDiscoverableReferenceProvider $provider) {
return $provider->getId();
}, $providers);
if (array_search($providerId, $providerIds, true) !== false) {
$configKey = 'provider-last-use_' . $providerId;
if ($timestamp === null) {
$timestamp = time();
}

$this->config->setUserValue($userId, 'references', $configKey, (string) $timestamp);
}
}

/**
* @inheritDoc
*/
public function getUserProviderTimestamps(): array {
$user = $this->userSession->getUser();
if ($user === null) {
return [];
}
$userId = $user->getUID();
$keys = $this->config->getUserKeys($userId, 'references');
$keys = array_filter($keys, static function (string $key) {
return preg_match('/^provider-last-use_/', $key) !== false;
});
$timestamps = [];
foreach ($keys as $key) {
$providerId = preg_replace('/^provider-last-use_/', '', $key);
$timestamp = (int) $this->config->getUserValue($userId, 'references', $key);
$timestamps[$providerId] = $timestamp;
}
return $timestamps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ public function handle(Event $event): void {
return $provider->jsonSerialize();
}, $providers);
$this->initialStateService->provideInitialState('core', 'reference-provider-list', $jsonProviders);

$timestamps = $this->manager->getUserProviderTimestamps();
$this->initialStateService->provideInitialState('core', 'reference-provider-timestamps', $timestamps);
}
}
22 changes: 21 additions & 1 deletion lib/public/Collaboration/Reference/IReferenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,30 @@ public function getReferenceFromCache(string $referenceId): ?IReference;
public function invalidateCache(string $cachePrefix, ?string $cacheKey = null): void;

/**
* Get list of providers that implement IDiscoverableReferenceProvider
* Get information on discoverable reference providers (id, title, icon and order)
* If the provider is searchable, also get the list of supported unified search providers
*
* @return IDiscoverableReferenceProvider[]
* @since 26.0.0
*/
public function getDiscoverableProviders(): array;

/**
* Update or set the last used timestamp for a provider
*
* @param string $userId
* @param string $providerId
* @param int|null $timestamp use current timestamp if null
* @return void
* @since 26.0.0
*/
public function touchProvider(string $userId, string $providerId, ?int $timestamp = null): void;

/**
* Get all known last used timestamps for reference providers
*
* @return int[]
* @since 26.0.0
*/
public function getUserProviderTimestamps(): array;
}

0 comments on commit 39ac533

Please sign in to comment.