Skip to content

Commit 5f797eb

Browse files
miaulalalaCarlSchwan
authored andcommitted
refactor: move existing usages of snoflake IDs SnowflakeAwareEntity
Signed-off-by: Anna Larch <anna@nextcloud.com>
1 parent a100ede commit 5f797eb

File tree

19 files changed

+43
-78
lines changed

19 files changed

+43
-78
lines changed

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\ISnowflakeGenerator;
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 ISnowflakeGenerator $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->setId();
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 & 12 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(),
@@ -107,13 +105,6 @@ public function jsonSerialize(): array {
107105
'user' => $this->getUser(),
108106
'mountpoint' => $this->getMountpoint(),
109107
'accepted' => $this->getAccepted(),
110-
111-
// Added later on
112-
'file_id' => null,
113-
'mimetype' => null,
114-
'permissions' => null,
115-
'mtime' => null,
116-
'type' => null,
117108
];
118109
}
119110

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\ISnowflakeGenerator;
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 ISnowflakeGenerator $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->setId();
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/lib/ResponseDefinitions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@
8282
*
8383
* @psalm-type Files_SharingRemoteShare = array{
8484
* accepted: int,
85-
* file_id: int|null,
8685
* id: string,
87-
* mimetype: string|null,
8886
* mountpoint: string,
89-
* mtime: int|null,
9087
* name: string,
9188
* owner: string,
9289
* parent: string|null,
93-
* permissions: int|null,
9490
* remote: string,
9591
* remote_id: string,
9692
* share_token: string,
9793
* share_type: int,
98-
* type: string|null,
9994
* user: string,
95+
* file_id?: int,
96+
* mimetype?: string,
97+
* permissions?: int,
98+
* mtime?: int,
99+
* type?: string,
100100
* }
101101
*
102102
* @psalm-type Files_SharingSharee = array{

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\ISnowflakeGenerator;
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(ISnowflakeGenerator::class)->nextId());
66+
$externalShare->setId();
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\ISnowflakeGenerator;
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(ISnowflakeGenerator::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(ISnowflakeGenerator::class)->nextId());
191+
$userShare->setId();
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(ISnowflakeGenerator::class)->nextId());
206+
$groupShare->setId();
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(ISnowflakeGenerator::class)->nextId());
238+
$shareData2->setId();
241239
$shareData3 = $shareData1->clone();
242240
$shareData3->setShareToken('token3');
243-
$shareData3->setId(\OCP\Server::get(ISnowflakeGenerator::class)->nextId());
241+
$shareData3->setId();
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(ISnowflakeGenerator::class)->nextId());
441+
$share->setId();
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(ISnowflakeGenerator::class)->nextId());
461+
$share->setId();
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(ISnowflakeGenerator::class)->nextId());
647+
$share->setId();
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(ISnowflakeGenerator::class)->nextId());
697+
$share->setId();
700698
$share->setRemote('http://localhost');
701699
$share->setShareToken('token1');
702700
$share->setPassword('');

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\ISnowflakeGenerator;
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 ISnowflakeGenerator $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->setId();
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->setId();
175174
try {
176175
$preview = $this->previewMapper->insert($preview);
177176
} catch (Exception) {

lib/private/Authentication/Token/PublicKeyToken.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ public function __construct() {
101101
$this->addType('passwordInvalid', Types::BOOLEAN);
102102
}
103103

104-
public function getId(): int {
105-
return $this->id;
106-
}
107-
108104
public function getUID(): string {
109105
return $this->uid;
110106
}
@@ -127,7 +123,7 @@ public function getPassword(): ?string {
127123

128124
public function jsonSerialize(): array {
129125
return [
130-
'id' => $this->id,
126+
'id' => $this->getId(),
131127
'name' => $this->name,
132128
'lastActivity' => $this->lastActivity,
133129
'type' => $this->type,

lib/private/Preview/Db/Preview.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
namespace OC\Preview\Db;
1212

1313
use OCP\AppFramework\Db\Entity;
14+
use OCP\AppFramework\Db\SnowflakeAwareEntity;
1415
use OCP\DB\Types;
1516
use OCP\Files\IMimeTypeDetector;
1617

1718
/**
1819
* Preview entity mapped to the oc_previews and oc_preview_locations table.
1920
*
20-
* @method string getId()
21-
* @method void setId(string $id)
2221
* @method int getFileId() Get the file id of the original file.
2322
* @method void setFileId(int $fileId)
2423
* @method int getStorageId() Get the storage id of the original file.
@@ -55,7 +54,7 @@
5554
*
5655
* @see PreviewMapper
5756
*/
58-
class Preview extends Entity {
57+
class Preview extends SnowflakeAwareEntity {
5958
protected ?int $fileId = null;
6059
protected ?int $oldFileId = null;
6160
protected ?int $storageId = null;

lib/private/Preview/Db/PreviewMapper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ public function insert(Entity $entity): Entity {
5252

5353
if ($preview->getVersion() !== null && $preview->getVersion() !== '') {
5454
$qb = $this->db->getQueryBuilder();
55-
$id = $this->snowflake->nextId();
5655
$qb->insert(self::VERSION_TABLE_NAME)
5756
->values([
58-
'id' => $qb->createNamedParameter($id),
57+
'id' => $qb->createNamedParameter($preview->getId()),
5958
'version' => $qb->createNamedParameter($preview->getVersion(), IQueryBuilder::PARAM_STR),
6059
'file_id' => $qb->createNamedParameter($preview->getFileId()),
6160
])
6261
->executeStatement();
63-
$entity->setVersionId($id);
62+
$entity->setVersionId((string)$preview->getId());
6463
}
6564
return parent::insert($preview);
6665
}

0 commit comments

Comments
 (0)