4242use OCP \Security \ISecureRandom ;
4343use OCP \Snowflake \ISnowflakeGenerator ;
4444use OCP \User \Events \BeforeUserDeletedEvent ;
45+ use Psr \Clock \ClockInterface ;
4546use Random \Randomizer ;
4647use RuntimeException ;
4748
@@ -70,6 +71,7 @@ public function __construct(
7071 private IDBConnection $ dbConnection ,
7172 private ISharingRegistry $ registry ,
7273 IAppConfig $ appConfig ,
74+ private ClockInterface $ clock ,
7375 ) {
7476 $ this ->randomizer = new Randomizer ();
7577 $ this ->l10n = $ l10nFactory ->get ('sharing ' );
@@ -140,13 +142,8 @@ public function generateSecret(): string {
140142 }
141143
142144 #[\Override]
143- public function generateTimestamp (): int {
144- $ time = (int )(microtime (true ) * 1000.0 );
145- if ($ time < 0 ) {
146- throw new RuntimeException ('Have you invented time travel? ' );
147- }
148-
149- return $ time ;
145+ public function getTime (): \DateTimeImmutable {
146+ return $ this ->clock ->now ();
150147 }
151148
152149 #[\Override]
@@ -158,7 +155,7 @@ public function createShare(ShareAccessContext $accessContext): string {
158155 $ this ->assertInTransaction ();
159156
160157 $ id = $ this ->snowflakeGenerator ->nextId ();
161- $ lastUpdated = $ this ->generateTimestamp ();
158+ $ lastUpdated = $ this ->getTime ();
162159 $ this ->backend ->createShare ($ id , new ShareUser ($ currentUser ->getUID (), null ), $ lastUpdated );
163160
164161 $ this ->processShareUpdates ([$ id ]);
@@ -190,7 +187,7 @@ public function onOwnerDeleted(ShareAccessContext $accessContext, ShareUser $own
190187 public function updateShareState (ShareAccessContext $ accessContext , string $ id , ShareState $ state ): void {
191188 $ this ->assertInTransaction ();
192189
193- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
190+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
194191
195192 $ owner = $ this ->backend ->getShareOwner ($ id );
196193 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -209,7 +206,7 @@ public function updateShareState(ShareAccessContext $accessContext, string $id,
209206 public function addShareSource (ShareAccessContext $ accessContext , string $ id , ShareSource $ source ): void {
210207 $ this ->assertInTransaction ();
211208
212- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
209+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
213210
214211 $ owner = $ this ->backend ->getShareOwner ($ id );
215212 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -250,7 +247,7 @@ public function addShareSource(ShareAccessContext $accessContext, string $id, Sh
250247 public function removeShareSource (ShareAccessContext $ accessContext , string $ id , ShareSource $ source ): void {
251248 $ this ->assertInTransaction ();
252249
253- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
250+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
254251
255252 $ owner = $ this ->backend ->getShareOwner ($ id );
256253 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -268,7 +265,7 @@ public function onSourceDeleted(ShareAccessContext $accessContext, ShareSource $
268265
269266 $ this ->assertInTransaction ();
270267
271- $ timestamp = $ this ->generateTimestamp ();
268+ $ timestamp = $ this ->getTime ();
272269
273270 $ updatedIds = $ this ->backend ->onSourceDeleted ($ source );
274271 if ($ updatedIds === []) {
@@ -288,7 +285,7 @@ public function addShareRecipient(ShareAccessContext $accessContext, string $id,
288285
289286 $ this ->assertInTransaction ();
290287
291- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
288+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
292289
293290 $ owner = $ this ->backend ->getShareOwner ($ id );
294291
@@ -351,7 +348,7 @@ public function removeShareRecipient(ShareAccessContext $accessContext, string $
351348
352349 $ this ->assertInTransaction ();
353350
354- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
351+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
355352
356353 $ owner = $ this ->backend ->getShareOwner ($ id );
357354
@@ -376,7 +373,7 @@ public function onRecipientDeleted(ShareAccessContext $accessContext, ShareRecip
376373
377374 $ this ->assertInTransaction ();
378375
379- $ timestamp = $ this ->generateTimestamp ();
376+ $ timestamp = $ this ->getTime ();
380377
381378 $ updatedIds = $ this ->backend ->onRecipientDeleted ($ recipient );
382379 if ($ updatedIds === []) {
@@ -396,7 +393,7 @@ public function onInitiatorDeleted(ShareAccessContext $accessContext, ShareUser
396393
397394 $ this ->assertInTransaction ();
398395
399- $ timestamp = $ this ->generateTimestamp ();
396+ $ timestamp = $ this ->getTime ();
400397
401398 $ updatedIds = $ this ->backend ->onInitiatorDeleted ($ initiator );
402399 if ($ updatedIds === []) {
@@ -412,7 +409,7 @@ public function onInitiatorDeleted(ShareAccessContext $accessContext, ShareUser
412409 public function updateShareRecipientSecret (ShareAccessContext $ accessContext , string $ id , ShareRecipient $ recipient , string $ secret ): void {
413410 $ this ->assertInTransaction ();
414411
415- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
412+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
416413
417414 $ owner = $ this ->backend ->getShareOwner ($ id );
418415
@@ -444,7 +441,7 @@ public function updateShareRecipientSecret(ShareAccessContext $accessContext, st
444441 public function createSharePropertyDefaultValue (Share $ share , string $ propertyTypeClass ): Share {
445442 $ this ->assertInTransaction ();
446443
447- $ timestamp = $ this ->generateTimestamp ();
444+ $ timestamp = $ this ->getTime ();
448445 $ this ->backend ->setLastUpdated ([$ share ->id ], $ timestamp );
449446
450447 if (($ propertyType = $ this ->registry ->getPropertyTypes ()[$ propertyTypeClass ] ?? null ) === null ) {
@@ -478,7 +475,7 @@ public function createSharePropertyDefaultValue(Share $share, string $propertyTy
478475 public function updateShareProperty (ShareAccessContext $ accessContext , string $ id , ShareProperty $ property ): void {
479476 $ this ->assertInTransaction ();
480477
481- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
478+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
482479
483480 $ owner = $ this ->backend ->getShareOwner ($ id );
484481 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -503,7 +500,7 @@ public function updateShareProperty(ShareAccessContext $accessContext, string $i
503500 public function createSharePermissionDefaultValue (Share $ share , string $ permissionTypeClass ): Share {
504501 $ this ->assertInTransaction ();
505502
506- $ timestamp = $ this ->generateTimestamp ();
503+ $ timestamp = $ this ->getTime ();
507504 $ this ->backend ->setLastUpdated ([$ share ->id ], $ timestamp );
508505
509506 if (($ permissionType = $ this ->registry ->getPermissionTypes ()[$ permissionTypeClass ] ?? null ) === null ) {
@@ -537,7 +534,7 @@ public function createSharePermissionDefaultValue(Share $share, string $permissi
537534 public function updateSharePermission (ShareAccessContext $ accessContext , string $ id , SharePermission $ permission ): void {
538535 $ this ->assertInTransaction ();
539536
540- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
537+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
541538
542539 $ owner = $ this ->backend ->getShareOwner ($ id );
543540 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -574,7 +571,7 @@ public function updateSharePermission(ShareAccessContext $accessContext, string
574571 public function selectSharePermissionPreset (ShareAccessContext $ accessContext , string $ id , string $ permissionPresetClass ): void {
575572 $ this ->assertInTransaction ();
576573
577- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
574+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
578575
579576 $ owner = $ this ->backend ->getShareOwner ($ id );
580577 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -826,4 +823,23 @@ private function processShareUpdates(array $sharesOrIds): array {
826823
827824 return $ shares ;
828825 }
826+
827+ /**
828+ * @return non-negative-int
829+ */
830+ public static function timeToMs (\DateTimeImmutable $ time ): int {
831+ if (method_exists ($ time , 'getMicrosecond ' )) {
832+ /** @var int $micros */
833+ $ micros = $ time ->getMicrosecond ();
834+ } else {
835+ $ micros = (int )$ time ->format ('u ' );
836+ }
837+
838+ $ time = $ time ->getTimestamp () * 1000 + (int )floor ($ micros / 1000 );
839+ if ($ time > 0 ) {
840+ return $ time ;
841+ }
842+
843+ throw new \RuntimeException ('invalid date-time ' );
844+ }
829845}
0 commit comments