Skip to content

Commit 40b79f5

Browse files
Merge pull request #56795 from nextcloud/feat/noid/extend-entity-to-be-snoflake-aware
feat(snowflake): extend Entity class to support snowflakes
2 parents 351351a + 693a226 commit 40b79f5

37 files changed

Lines changed: 284 additions & 167 deletions

apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
use OCP\Share\IManager;
4545
use OCP\Share\IProviderFactory;
4646
use OCP\Share\IShare;
47-
use OCP\Snowflake\IGenerator;
4847
use OCP\Util;
4948
use Override;
5049
use Psr\Log\LoggerInterface;
@@ -70,7 +69,6 @@ public function __construct(
7069
private readonly IFilenameValidator $filenameValidator,
7170
private readonly IProviderFactory $shareProviderFactory,
7271
private readonly SetupManager $setupManager,
73-
private readonly IGenerator $snowflakeGenerator,
7472
private readonly ExternalShareMapper $externalShareMapper,
7573
) {
7674
}
@@ -145,7 +143,7 @@ public function shareReceived(ICloudFederationShare $share): string {
145143
}
146144

147145
$externalShare = new ExternalShare();
148-
$externalShare->setId($this->snowflakeGenerator->nextId());
146+
$externalShare->generateId();
149147
$externalShare->setRemote($remote);
150148
$externalShare->setRemoteId($remoteId);
151149
$externalShare->setShareToken($token);
@@ -177,9 +175,9 @@ public function shareReceived(ICloudFederationShare $share): string {
177175
->setType('remote_share')
178176
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/'), $ownerDisplayName])
179177
->setAffectedUser($shareWith)
180-
->setObject('remote_share', $externalShare->getId(), $name);
178+
->setObject('remote_share', (string)$externalShare->getId(), $name);
181179
Server::get(IActivityManager::class)->publish($event);
182-
$this->notifyAboutNewShare($shareWith, $externalShare->getId(), $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
180+
$this->notifyAboutNewShare($shareWith, (string)$externalShare->getId(), $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
183181

184182
// If auto-accept is enabled, accept the share
185183
if ($this->federatedShareProvider->isFederatedTrustedShareAutoAccept() && $trustedServers?->isTrustedServer($remote) === true) {
@@ -193,9 +191,9 @@ public function shareReceived(ICloudFederationShare $share): string {
193191
->setType('remote_share')
194192
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/'), $ownerDisplayName])
195193
->setAffectedUser($user->getUID())
196-
->setObject('remote_share', $externalShare->getId(), $name);
194+
->setObject('remote_share', (string)$externalShare->getId(), $name);
197195
Server::get(IActivityManager::class)->publish($event);
198-
$this->notifyAboutNewShare($user->getUID(), $externalShare->getId(), $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
196+
$this->notifyAboutNewShare($user->getUID(), (string)$externalShare->getId(), $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
199197

200198
// If auto-accept is enabled, accept the share
201199
if ($this->federatedShareProvider->isFederatedTrustedShareAutoAccept() && $trustedServers?->isTrustedServer($remote) === true) {
@@ -204,7 +202,7 @@ public function shareReceived(ICloudFederationShare $share): string {
204202
}
205203
}
206204

207-
return $externalShare->getId();
205+
return (string)$externalShare->getId();
208206
} catch (\Exception $e) {
209207
$this->logger->error('Server can not add remote share.', [
210208
'app' => 'files_sharing',
@@ -466,7 +464,7 @@ private function unshare(string $id, array $notification): array {
466464
$notification = $this->notificationManager->createNotification();
467465
$notification->setApp('files_sharing')
468466
->setUser($share->getUser())
469-
->setObject('remote_share', $share->getId());
467+
->setObject('remote_share', (string)$share->getId());
470468
$this->notificationManager->markProcessed($notification);
471469

472470
$event = $this->activityManager->generateEvent();

apps/files_sharing/lib/External/ExternalShare.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212

1313
use OC\Files\Filesystem;
1414
use OCA\Files_Sharing\ResponseDefinitions;
15-
use OCP\AppFramework\Db\Entity;
15+
use OCP\AppFramework\Db\SnowflakeAwareEntity;
1616
use OCP\DB\Types;
1717
use OCP\IGroup;
1818
use OCP\IUser;
1919
use OCP\Share\IShare;
2020

2121
/**
22-
* @method string getId()
23-
* @method void setId(string $id)
2422
* @method string getParent()
2523
* @method void setParent(string $parent)
2624
* @method int|null getShareType()
@@ -46,7 +44,7 @@
4644
*
4745
* @psalm-import-type Files_SharingRemoteShare from ResponseDefinitions
4846
*/
49-
class ExternalShare extends Entity implements \JsonSerializable {
47+
class ExternalShare extends SnowflakeAwareEntity implements \JsonSerializable {
5048
protected string $parent = '-1';
5149
protected ?int $shareType = null;
5250
protected ?string $remote = null;
@@ -96,7 +94,7 @@ public function setShareWith(IUser|IGroup $shareWith): void {
9694
public function jsonSerialize(): array {
9795
$parent = $this->getParent();
9896
return [
99-
'id' => $this->getId(),
97+
'id' => (string)$this->getId(),
10098
'parent' => $parent,
10199
'share_type' => $this->getShareType() ?? IShare::TYPE_USER, // unfortunately nullable on the DB level, but never null.
102100
'remote' => $this->getRemote(),

apps/files_sharing/lib/External/Manager.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
use OCP\Notification\IManager;
3535
use OCP\OCS\IDiscoveryService;
3636
use OCP\Share\IShare;
37-
use OCP\Snowflake\IGenerator;
3837
use Psr\Log\LoggerInterface;
3938

4039
class Manager {
@@ -57,7 +56,6 @@ public function __construct(
5756
private SetupManager $setupManager,
5857
private ICertificateManager $certificateManager,
5958
private ExternalShareMapper $externalShareMapper,
60-
private IGenerator $snowflakeGenerator,
6159
) {
6260
$this->user = $userSession->getUser();
6361
}
@@ -186,7 +184,7 @@ private function updateSubShare(ExternalShare $externalShare, IUser $user, ?stri
186184
$subShare = $this->externalShareMapper->getUserShare($externalShare, $user);
187185
} catch (DoesNotExistException) {
188186
$subShare = new ExternalShare();
189-
$subShare->setId($this->snowflakeGenerator->nextId());
187+
$subShare->generateId();
190188
$subShare->setRemote($externalShare->getRemote());
191189
$subShare->setPassword($externalShare->getPassword());
192190
$subShare->setName($externalShare->getName());
@@ -195,7 +193,7 @@ private function updateSubShare(ExternalShare $externalShare, IUser $user, ?stri
195193
$subShare->setMountpoint($mountPoint ?? $externalShare->getMountpoint());
196194
$subShare->setAccepted($accepted);
197195
$subShare->setRemoteId($externalShare->getRemoteId());
198-
$subShare->setParent($externalShare->getId());
196+
$subShare->setParent((string)$externalShare->getId());
199197
$subShare->setShareType($externalShare->getShareType());
200198
$subShare->setShareToken($externalShare->getShareToken());
201199
$this->externalShareMapper->insert($subShare);
@@ -317,7 +315,7 @@ public function processNotification(ExternalShare $remoteShare, ?IUser $user = n
317315
$filter = $this->notificationManager->createNotification();
318316
$filter->setApp('files_sharing')
319317
->setUser($user->getUID())
320-
->setObject('remote_share', $remoteShare->getId());
318+
->setObject('remote_share', (string)$remoteShare->getId());
321319
$this->notificationManager->markProcessed($filter);
322320
}
323321

apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use OCP\Federation\ICloudIdManager;
1515
use OCP\IDBConnection;
1616
use OCP\Server;
17-
use OCP\Snowflake\IGenerator;
1817
use PHPUnit\Framework\Attributes\Group;
1918
use PHPUnit\Framework\MockObject\MockObject;
2019
use Symfony\Component\Console\Input\InputInterface;
@@ -64,7 +63,7 @@ protected function setUp(): void {
6463

6564
if (isset($storage['share_token'])) {
6665
$externalShare = new ExternalShare();
67-
$externalShare->setId(Server::get(IGenerator::class)->nextId());
66+
$externalShare->generateId();
6867
$externalShare->setShareToken($storage['share_token']);
6968
$externalShare->setRemote($storage['remote']);
7069
$externalShare->setName('irrelevant');

apps/files_sharing/tests/External/ManagerTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use OCP\OCS\IDiscoveryService;
4141
use OCP\Server;
4242
use OCP\Share\IShare;
43-
use OCP\Snowflake\IGenerator;
4443
use PHPUnit\Framework\MockObject\MockObject;
4544
use Psr\Log\LoggerInterface;
4645
use Test\Traits\UserTrait;
@@ -170,7 +169,6 @@ private function createManagerForUser(IUser $user): Manager&MockObject {
170169
$this->setupManager,
171170
$this->certificateManager,
172171
$this->externalShareMapper,
173-
Server::get(IGenerator::class),
174172
]
175173
)->onlyMethods(['tryOCMEndPoint'])->getMock();
176174
}
@@ -190,7 +188,7 @@ private function clearMounts(): void {
190188

191189
public function testAddUserShare(): void {
192190
$userShare = new ExternalShare();
193-
$userShare->setId(Server::get(IGenerator::class)->nextId());
191+
$userShare->generateId();
194192
$userShare->setRemote('http://localhost');
195193
$userShare->setShareToken('token1');
196194
$userShare->setPassword('');
@@ -205,7 +203,7 @@ public function testAddUserShare(): void {
205203

206204
public function testAddGroupShare(): void {
207205
$groupShare = new ExternalShare();
208-
$groupShare->setId(Server::get(IGenerator::class)->nextId());
206+
$groupShare->generateId();
209207
$groupShare->setRemote('http://localhost');
210208
$groupShare->setOwner('foobar');
211209
$groupShare->setShareType(IShare::TYPE_GROUP);
@@ -237,10 +235,10 @@ public function doTestAddShare(ExternalShare $shareData1, IUser|IGroup $userOrGr
237235

238236
$shareData2 = $shareData1->clone();
239237
$shareData2->setShareToken('token2');
240-
$shareData2->setId(\OCP\Server::get(IGenerator::class)->nextId());
238+
$shareData2->generateId();
241239
$shareData3 = $shareData1->clone();
242240
$shareData3->setShareToken('token3');
243-
$shareData3->setId(\OCP\Server::get(IGenerator::class)->nextId());
241+
$shareData3->generateId();
244242

245243
$this->setupMounts();
246244
$this->assertNotMount('SharedFolder');
@@ -440,7 +438,7 @@ private function createTestUserShare(string $userId = 'user1'): ExternalShare {
440438
$user = $this->createMock(IUser::class);
441439
$user->expects($this->any())->method('getUID')->willReturn($userId);
442440
$share = new ExternalShare();
443-
$share->setId(Server::get(IGenerator::class)->nextId());
441+
$share->generateId();
444442
$share->setRemote('http://localhost');
445443
$share->setShareToken('token1');
446444
$share->setPassword('');
@@ -460,7 +458,7 @@ private function createTestUserShare(string $userId = 'user1'): ExternalShare {
460458
*/
461459
private function createTestGroupShare(string $groupId = 'group1'): array {
462460
$share = new ExternalShare();
463-
$share->setId(Server::get(IGenerator::class)->nextId());
461+
$share->generateId();
464462
$share->setRemote('http://localhost');
465463
$share->setShareToken('token1');
466464
$share->setPassword('');
@@ -646,7 +644,7 @@ public function testDeleteUserShares(): void {
646644
// user 2 shares
647645
$manager2 = $this->createManagerForUser($user2);
648646
$share = new ExternalShare();
649-
$share->setId(Server::get(IGenerator::class)->nextId());
647+
$share->generateId();
650648
$share->setRemote('http://localhost');
651649
$share->setShareToken('token1');
652650
$share->setPassword('');
@@ -696,7 +694,7 @@ public function testDeleteGroupShares(): void {
696694
$manager2 = $this->createManagerForUser($user);
697695

698696
$share = new ExternalShare();
699-
$share->setId(Server::get(IGenerator::class)->nextId());
697+
$share->generateId();
700698
$share->setRemote('http://localhost');
701699
$share->setShareToken('token1');
702700
$share->setPassword('');

apps/files_versions/lib/Db/VersionEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct() {
4444

4545
public function jsonSerialize(): array {
4646
return [
47-
'id' => $this->id,
47+
'id' => $this->getId(),
4848
'file_id' => $this->fileId,
4949
'timestamp' => $this->timestamp,
5050
'size' => $this->size,

core/BackgroundJobs/MovePreviewJob.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use OCP\IAppConfig;
2727
use OCP\IConfig;
2828
use OCP\IDBConnection;
29-
use OCP\Snowflake\IGenerator;
3029
use Override;
3130
use Psr\Log\LoggerInterface;
3231

@@ -45,7 +44,6 @@ public function __construct(
4544
private readonly IMimeTypeDetector $mimeTypeDetector,
4645
private readonly IMimeTypeLoader $mimeTypeLoader,
4746
private readonly LoggerInterface $logger,
48-
private readonly IGenerator $generator,
4947
IAppDataFactory $appDataFactory,
5048
) {
5149
parent::__construct($time);
@@ -138,7 +136,7 @@ private function processPreviews(int $fileId, bool $flatPath): void {
138136
$path = $fileId . '/' . $previewFile->getName();
139137
/** @var SimpleFile $previewFile */
140138
$preview = Preview::fromPath($path, $this->mimeTypeDetector);
141-
$preview->setId($this->generator->nextId());
139+
$preview->generateId();
142140
if (!$preview) {
143141
$this->logger->error('Unable to import old preview at path.');
144142
continue;
@@ -172,6 +170,7 @@ private function processPreviews(int $fileId, bool $flatPath): void {
172170
$preview->setStorageId($result[0]['storage']);
173171
$preview->setEtag($result[0]['etag']);
174172
$preview->setSourceMimeType($this->mimeTypeLoader->getMimetypeById((int)$result[0]['mimetype']));
173+
$preview->generateId();
175174
try {
176175
$preview = $this->previewMapper->insert($preview);
177176
} catch (Exception) {

core/Command/SnowflakeDecodeId.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
*/
99
namespace OC\Core\Command;
1010

11-
use OCP\Snowflake\IDecoder;
11+
use OCP\Snowflake\ISnowflakeDecoder;
1212
use Symfony\Component\Console\Helper\Table;
1313
use Symfony\Component\Console\Input\InputArgument;
1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616

1717
class SnowflakeDecodeId extends Base {
1818
public function __construct(
19-
private readonly IDecoder $decoder,
19+
private readonly ISnowflakeDecoder $decoder,
2020
) {
2121
parent::__construct();
2222
}
@@ -36,13 +36,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3636

3737
$rows = [
3838
['Snowflake ID', $snowflakeId],
39-
['Seconds', $data['seconds']],
40-
['Milliseconds', $data['milliseconds']],
41-
['Created from CLI', $data['isCli'] ? 'yes' : 'no'],
42-
['Server ID', $data['serverId']],
43-
['Sequence ID', $data['sequenceId']],
44-
['Creation timestamp', $data['createdAt']->format('U.v')],
45-
['Creation date', $data['createdAt']->format('Y-m-d H:i:s.v')],
39+
['Seconds', $data->getSeconds()],
40+
['Milliseconds', $data->getMilliseconds()],
41+
['Created from CLI', $data->isCli() ? 'yes' : 'no'],
42+
['Server ID', $data->getServerId()],
43+
['Sequence ID', $data->getSequenceId()],
44+
['Creation timestamp', $data->getCreatedAt()->format('U.v')],
45+
['Creation date', $data->getCreatedAt()->format('Y-m-d H:i:s.v')],
4646
];
4747

4848
$table = new Table($output);

lib/composer/composer/autoload_classmap.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
'OCP\\AppFramework\\Db\\IMapperException' => $baseDir . '/lib/public/AppFramework/Db/IMapperException.php',
7979
'OCP\\AppFramework\\Db\\MultipleObjectsReturnedException' => $baseDir . '/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php',
8080
'OCP\\AppFramework\\Db\\QBMapper' => $baseDir . '/lib/public/AppFramework/Db/QBMapper.php',
81+
'OCP\\AppFramework\\Db\\SnowflakeAwareEntity' => $baseDir . '/lib/public/AppFramework/Db/SnowflakeAwareEntity.php',
8182
'OCP\\AppFramework\\Db\\TTransactional' => $baseDir . '/lib/public/AppFramework/Db/TTransactional.php',
8283
'OCP\\AppFramework\\Http' => $baseDir . '/lib/public/AppFramework/Http.php',
8384
'OCP\\AppFramework\\Http\\Attribute\\ARateLimit' => $baseDir . '/lib/public/AppFramework/Http/Attribute/ARateLimit.php',
@@ -837,8 +838,9 @@
837838
'OCP\\Share_Backend' => $baseDir . '/lib/public/Share_Backend.php',
838839
'OCP\\Share_Backend_Collection' => $baseDir . '/lib/public/Share_Backend_Collection.php',
839840
'OCP\\Share_Backend_File_Dependent' => $baseDir . '/lib/public/Share_Backend_File_Dependent.php',
840-
'OCP\\Snowflake\\IDecoder' => $baseDir . '/lib/public/Snowflake/IDecoder.php',
841-
'OCP\\Snowflake\\IGenerator' => $baseDir . '/lib/public/Snowflake/IGenerator.php',
841+
'OCP\\Snowflake\\ISnowflakeDecoder' => $baseDir . '/lib/public/Snowflake/ISnowflakeDecoder.php',
842+
'OCP\\Snowflake\\ISnowflakeGenerator' => $baseDir . '/lib/public/Snowflake/ISnowflakeGenerator.php',
843+
'OCP\\Snowflake\\Snowflake' => $baseDir . '/lib/public/Snowflake/Snowflake.php',
842844
'OCP\\SpeechToText\\Events\\AbstractTranscriptionEvent' => $baseDir . '/lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php',
843845
'OCP\\SpeechToText\\Events\\TranscriptionFailedEvent' => $baseDir . '/lib/public/SpeechToText/Events/TranscriptionFailedEvent.php',
844846
'OCP\\SpeechToText\\Events\\TranscriptionSuccessfulEvent' => $baseDir . '/lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php',
@@ -2144,10 +2146,10 @@
21442146
'OC\\Share\\Helper' => $baseDir . '/lib/private/Share/Helper.php',
21452147
'OC\\Share\\Share' => $baseDir . '/lib/private/Share/Share.php',
21462148
'OC\\Snowflake\\APCuSequence' => $baseDir . '/lib/private/Snowflake/APCuSequence.php',
2147-
'OC\\Snowflake\\Decoder' => $baseDir . '/lib/private/Snowflake/Decoder.php',
21482149
'OC\\Snowflake\\FileSequence' => $baseDir . '/lib/private/Snowflake/FileSequence.php',
2149-
'OC\\Snowflake\\Generator' => $baseDir . '/lib/private/Snowflake/Generator.php',
21502150
'OC\\Snowflake\\ISequence' => $baseDir . '/lib/private/Snowflake/ISequence.php',
2151+
'OC\\Snowflake\\SnowflakeDecoder' => $baseDir . '/lib/private/Snowflake/SnowflakeDecoder.php',
2152+
'OC\\Snowflake\\SnowflakeGenerator' => $baseDir . '/lib/private/Snowflake/SnowflakeGenerator.php',
21512153
'OC\\SpeechToText\\SpeechToTextManager' => $baseDir . '/lib/private/SpeechToText/SpeechToTextManager.php',
21522154
'OC\\SpeechToText\\TranscriptionJob' => $baseDir . '/lib/private/SpeechToText/TranscriptionJob.php',
21532155
'OC\\StreamImage' => $baseDir . '/lib/private/StreamImage.php',

0 commit comments

Comments
 (0)