aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Veyssier <julien-nc@posteo.net>2023-01-12 15:47:20 +0100
committerJulius Härtl <jus@bitgrid.net>2023-01-27 11:10:56 +0100
commit946a1af9fd20d12a2ee6240ad2ae24827a22278c (patch)
treed7add9a58d1d7830d3c6948c167b32902a52b5ec
parent6431c5a559a1361ae9148adf22b21630b8a37431 (diff)
downloadnextcloud-server-946a1af9fd20d12a2ee6240ad2ae24827a22278c.tar.gz
nextcloud-server-946a1af9fd20d12a2ee6240ad2ae24827a22278c.zip
add 'last used timestamp' management for reference providers
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
-rw-r--r--core/Controller/ReferenceApiController.php20
-rw-r--r--core/routes.php1
-rw-r--r--lib/private/Collaboration/Reference/File/FileReferenceEventListener.php1
-rw-r--r--lib/private/Collaboration/Reference/ReferenceManager.php59
-rw-r--r--lib/private/Collaboration/Reference/RenderReferenceEventListener.php3
-rw-r--r--lib/public/Collaboration/Reference/IReferenceManager.php22
6 files changed, 99 insertions, 7 deletions
diff --git a/core/Controller/ReferenceApiController.php b/core/Controller/ReferenceApiController.php
index 098940187a8..9c3a212e8d7 100644
--- a/core/Controller/ReferenceApiController.php
+++ b/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]);
+ }
}
diff --git a/core/routes.php b/core/routes.php
index 4b67dee43f9..dcf8e4024af 100644
--- a/core/routes.php
+++ b/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'],
diff --git a/lib/private/Collaboration/Reference/File/FileReferenceEventListener.php b/lib/private/Collaboration/Reference/File/FileReferenceEventListener.php
index 6ccae9903dc..ae50f769665 100644
--- a/lib/private/Collaboration/Reference/File/FileReferenceEventListener.php
+++ b/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;
diff --git a/lib/private/Collaboration/Reference/ReferenceManager.php b/lib/private/Collaboration/Reference/ReferenceManager.php
index 1b00b9325ca..381f3aea8f4 100644
--- a/lib/private/Collaboration/Reference/ReferenceManager.php
+++ b/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;
+ }
}
diff --git a/lib/private/Collaboration/Reference/RenderReferenceEventListener.php b/lib/private/Collaboration/Reference/RenderReferenceEventListener.php
index 172dca3d7f2..f23aae92afa 100644
--- a/lib/private/Collaboration/Reference/RenderReferenceEventListener.php
+++ b/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);
}
}
diff --git a/lib/public/Collaboration/Reference/IReferenceManager.php b/lib/public/Collaboration/Reference/IReferenceManager.php
index 31977be4d74..8e6dee4aa2d 100644
--- a/lib/public/Collaboration/Reference/IReferenceManager.php
+++ b/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;
}