Browse Source

add 'last used timestamp' management for reference providers

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
Julien Veyssier 1 year ago
parent
commit
946a1af9fd

+ 19 - 1
core/Controller/ReferenceApiController.php

@@ -31,10 +31,15 @@ use OCP\IRequest;
 
 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;
 	}
 
 	/**
@@ -102,4 +107,17 @@ class ReferenceApiController extends \OCP\AppFramework\OCSController {
 		}, $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 - 0
core/routes.php

@@ -135,6 +135,7 @@ $application->registerRoutes($this, [
 		['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'],
 

+ 1 - 0
lib/private/Collaboration/Reference/File/FileReferenceEventListener.php

@@ -31,6 +31,7 @@ use OCP\Files\Events\Node\NodeDeletedEvent;
 use OCP\Share\Events\ShareCreatedEvent;
 use OCP\Share\Events\ShareDeletedEvent;
 
+/** @psalm-implements IEventDispatcher<Event|NodeDeletedEvent|ShareDeletedEvent|ShareCreatedEvent> */
 class FileReferenceEventListener implements \OCP\EventDispatcher\IEventListener {
 	private IReferenceManager $manager;
 

+ 54 - 5
lib/private/Collaboration/Reference/ReferenceManager.php

@@ -33,7 +33,9 @@ use OCP\Collaboration\Reference\IReferenceProvider;
 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;
@@ -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;
 	}
 
 	/**
@@ -216,10 +228,7 @@ class ReferenceManager implements IReferenceManager {
 	}
 
 	/**
-	 * 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
@@ -229,4 +238,44 @@ class ReferenceManager implements IReferenceManager {
 			})
 		);
 	}
+
+	/**
+	 * @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;
+	}
 }

+ 3 - 0
lib/private/Collaboration/Reference/RenderReferenceEventListener.php

@@ -58,5 +58,8 @@ class RenderReferenceEventListener implements IEventListener {
 			return $provider->jsonSerialize();
 		}, $providers);
 		$this->initialStateService->provideInitialState('core', 'reference-provider-list', $jsonProviders);
+
+		$timestamps = $this->manager->getUserProviderTimestamps();
+		$this->initialStateService->provideInitialState('core', 'reference-provider-timestamps', $timestamps);
 	}
 }

+ 21 - 1
lib/public/Collaboration/Reference/IReferenceManager.php

@@ -69,10 +69,30 @@ interface IReferenceManager {
 	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;
 }