5050use OCP \Share \IManager ;
5151use OCP \Snowflake \ISnowflakeGenerator ;
5252use OCP \User \Events \BeforeUserDeletedEvent ;
53+ use Psr \Clock \ClockInterface ;
5354use Random \Randomizer ;
5455use RuntimeException ;
5556
@@ -81,6 +82,7 @@ public function __construct(
8182 private readonly IDBConnection $ dbConnection ,
8283 private readonly ISharingRegistry $ registry ,
8384 private readonly IManager $ legacyManager ,
85+ private readonly ClockInterface $ clock ,
8486 IAppConfig $ appConfig ,
8587 ) {
8688 $ this ->randomizer = new Randomizer ();
@@ -160,13 +162,8 @@ public function generateSecret(): string {
160162 }
161163
162164 #[\Override]
163- public function generateTimestamp (): int {
164- $ time = (int )(microtime (true ) * 1000.0 );
165- if ($ time < 0 ) {
166- throw new RuntimeException ('Have you invented time travel? ' );
167- }
168-
169- return $ time ;
165+ public function getTime (): \DateTimeImmutable {
166+ return $ this ->clock ->now ();
170167 }
171168
172169 #[\Override]
@@ -178,7 +175,7 @@ public function createShare(ShareAccessContext $accessContext): string {
178175 $ this ->assertInTransaction ();
179176
180177 $ id = $ this ->snowflakeGenerator ->nextId ();
181- $ lastUpdated = $ this ->generateTimestamp ();
178+ $ lastUpdated = $ this ->getTime ();
182179 $ this ->backend ->createShare ($ id , new ShareUser ($ currentUser ->getUID (), null ), $ lastUpdated );
183180
184181 $ this ->processShareUpdates ([$ id ]);
@@ -210,7 +207,7 @@ public function onOwnerDeleted(ShareAccessContext $accessContext, ShareUser $own
210207 public function updateShareState (ShareAccessContext $ accessContext , string $ id , ShareState $ state ): void {
211208 $ this ->assertInTransaction ();
212209
213- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
210+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
214211
215212 $ owner = $ this ->backend ->getShareOwner ($ id );
216213 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -229,7 +226,7 @@ public function updateShareState(ShareAccessContext $accessContext, string $id,
229226 public function addShareSource (ShareAccessContext $ accessContext , string $ id , ShareSource $ source ): void {
230227 $ this ->assertInTransaction ();
231228
232- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
229+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
233230
234231 $ owner = $ this ->backend ->getShareOwner ($ id );
235232 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -270,7 +267,7 @@ public function addShareSource(ShareAccessContext $accessContext, string $id, Sh
270267 public function removeShareSource (ShareAccessContext $ accessContext , string $ id , ShareSource $ source ): void {
271268 $ this ->assertInTransaction ();
272269
273- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
270+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
274271
275272 $ owner = $ this ->backend ->getShareOwner ($ id );
276273 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -288,7 +285,7 @@ public function onSourceDeleted(ShareAccessContext $accessContext, ShareSource $
288285
289286 $ this ->assertInTransaction ();
290287
291- $ timestamp = $ this ->generateTimestamp ();
288+ $ timestamp = $ this ->getTime ();
292289
293290 $ updatedIds = $ this ->backend ->onSourceDeleted ($ source );
294291 if ($ updatedIds === []) {
@@ -308,7 +305,7 @@ public function addShareRecipient(ShareAccessContext $accessContext, string $id,
308305
309306 $ this ->assertInTransaction ();
310307
311- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
308+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
312309
313310 $ owner = $ this ->backend ->getShareOwner ($ id );
314311
@@ -371,7 +368,7 @@ public function removeShareRecipient(ShareAccessContext $accessContext, string $
371368
372369 $ this ->assertInTransaction ();
373370
374- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
371+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
375372
376373 $ owner = $ this ->backend ->getShareOwner ($ id );
377374
@@ -396,7 +393,7 @@ public function onRecipientDeleted(ShareAccessContext $accessContext, ShareRecip
396393
397394 $ this ->assertInTransaction ();
398395
399- $ timestamp = $ this ->generateTimestamp ();
396+ $ timestamp = $ this ->getTime ();
400397
401398 $ updatedIds = $ this ->backend ->onRecipientDeleted ($ recipient );
402399 if ($ updatedIds === []) {
@@ -416,7 +413,7 @@ public function onInitiatorDeleted(ShareAccessContext $accessContext, ShareUser
416413
417414 $ this ->assertInTransaction ();
418415
419- $ timestamp = $ this ->generateTimestamp ();
416+ $ timestamp = $ this ->getTime ();
420417
421418 $ updatedIds = $ this ->backend ->onInitiatorDeleted ($ initiator );
422419 if ($ updatedIds === []) {
@@ -432,7 +429,7 @@ public function onInitiatorDeleted(ShareAccessContext $accessContext, ShareUser
432429 public function updateShareRecipientSecret (ShareAccessContext $ accessContext , string $ id , ShareRecipient $ recipient , string $ secret ): void {
433430 $ this ->assertInTransaction ();
434431
435- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
432+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
436433
437434 $ owner = $ this ->backend ->getShareOwner ($ id );
438435
@@ -464,7 +461,7 @@ public function updateShareRecipientSecret(ShareAccessContext $accessContext, st
464461 public function createSharePropertyDefaultValue (Share $ share , string $ propertyTypeClass ): Share {
465462 $ this ->assertInTransaction ();
466463
467- $ timestamp = $ this ->generateTimestamp ();
464+ $ timestamp = $ this ->getTime ();
468465 $ this ->backend ->setLastUpdated ([$ share ->id ], $ timestamp );
469466
470467 if (($ propertyType = $ this ->registry ->getPropertyTypes ()[$ propertyTypeClass ] ?? null ) === null ) {
@@ -498,7 +495,7 @@ public function createSharePropertyDefaultValue(Share $share, string $propertyTy
498495 public function updateShareProperty (ShareAccessContext $ accessContext , string $ id , ShareProperty $ property ): void {
499496 $ this ->assertInTransaction ();
500497
501- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
498+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
502499
503500 $ owner = $ this ->backend ->getShareOwner ($ id );
504501 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -523,8 +520,8 @@ public function updateShareProperty(ShareAccessContext $accessContext, string $i
523520 public function createSharePermissionDefaultValue (Share $ share , string $ permissionTypeClass ): Share {
524521 $ this ->assertInTransaction ();
525522
526- $ timestamp = $ this ->generateTimestamp ();
527- $ this ->backend ->setLastUpdated ([$ share ->id ], $ timestamp );
523+ $ time = $ this ->getTime ();
524+ $ this ->backend ->setLastUpdated ([$ share ->id ], $ time );
528525
529526 if (($ permissionType = $ this ->registry ->getPermissionTypes ()[$ permissionTypeClass ] ?? null ) === null ) {
530527 throw new RuntimeException ('The permission is not registered: ' . $ permissionTypeClass );
@@ -540,7 +537,7 @@ public function createSharePermissionDefaultValue(Share $share, string $permissi
540537 $ share = new Share (
541538 $ share ->id ,
542539 $ share ->owner ,
543- $ timestamp ,
540+ $ time ,
544541 $ share ->state ,
545542 $ share ->sources ,
546543 $ share ->recipients ,
@@ -557,7 +554,7 @@ public function createSharePermissionDefaultValue(Share $share, string $permissi
557554 public function updateSharePermission (ShareAccessContext $ accessContext , string $ id , SharePermission $ permission ): void {
558555 $ this ->assertInTransaction ();
559556
560- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
557+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
561558
562559 $ owner = $ this ->backend ->getShareOwner ($ id );
563560 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -594,7 +591,7 @@ public function updateSharePermission(ShareAccessContext $accessContext, string
594591 public function selectSharePermissionPreset (ShareAccessContext $ accessContext , string $ id , string $ permissionPresetClass ): void {
595592 $ this ->assertInTransaction ();
596593
597- $ this ->backend ->setLastUpdated ([$ id ], $ this ->generateTimestamp ());
594+ $ this ->backend ->setLastUpdated ([$ id ], $ this ->getTime ());
598595
599596 $ owner = $ this ->backend ->getShareOwner ($ id );
600597 $ this ->validateShareOwnerOperation ($ accessContext , $ owner );
@@ -980,4 +977,13 @@ private function updateShare(Share $share): void {
980977 $ this ->backend ->createSharePermission ($ share ->id , $ permission );
981978 }
982979 }
980+
981+ public static function timeToMs (\DateTimeImmutable $ time ): int {
982+ if (method_exists ($ time , 'getMicrosecond ' )) {
983+ $ micros = $ time ->getMicrosecond ();
984+ } else {
985+ $ micros = (int )$ time ->format ('u ' );
986+ }
987+ return $ time ->getTimestamp () * 1000 + (int )floor ($ micros / 1000 );
988+ }
983989}
0 commit comments