Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions apps/files_sharing/lib/Listener/SharesUpdatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class SharesUpdatedListener implements IEventListener {
private float $cutOffMarkTime;

/**
* The total amount of time we've spent so far processing updates
* Timestamp marking the first time this listener was triggered, used to
* determine if we should update the share date immediately or just mark the
* users for refresh.
*/
private float $updatedTime = 0.0;
private bool $inUpdate = false;
Expand Down Expand Up @@ -134,14 +136,15 @@ public function handle(Event $event): void {
}

private function markOrRun(IUser $user, callable $callback): void {
$start = floatval($this->clock->now()->format('U.u'));
if ($this->cutOffMarkTime === -1.0 || $this->updatedTime < $this->cutOffMarkTime) {
$now = (float)$this->clock->now()->format('U.u');
$this->firstRun ??= $now;
$elapsed = $now - $this->firstRun;

if ($this->cutOffMarkTime === -1.0 || $elapsed < $this->cutOffMarkTime) {
$callback();
} else {
$this->markUserForRefresh($user);
}
$end = floatval($this->clock->now()->format('U.u'));
$this->updatedTime += $end - $start;
}

private function updateOrMarkUser(IUser $user): void {
Expand Down
34 changes: 33 additions & 1 deletion apps/files_sharing/tests/SharesUpdatedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class SharesUpdatedListenerTest extends \Test\TestCase {
private LoggerInterface&MockObject $logger;
private $clockFn;

private int $time = 0;

protected function setUp(): void {
parent::setUp();

Expand All @@ -50,7 +52,7 @@ protected function setUp(): void {
$this->userConfig = new MockUserConfig();
$this->clock = $this->createMock(ClockInterface::class);
$this->clockFn = function () {
return new \DateTimeImmutable('@0');
return new \DateTimeImmutable("@{$this->time}");
};
$this->clock->method('now')
->willReturnCallback(function () {
Expand Down Expand Up @@ -161,6 +163,36 @@ public static function shareMarkAfterTimeProvider(): array {
];
}

public function testCutoffSpansMultipleHandleInvocations(): void {
$share = $this->createMock(IShare::class);
$user1 = $this->createUser('user1', '');

$this->manager->method('getUsersForShare')
->willReturn([$user1]);

$event = new ShareCreatedEvent($share);
$this->sharesUpdatedListener->setCutOffMarkTime(4);

// First handle at t=0: firstRun=0, elapsed=0 < 0.5 → callback runs
$this->shareRecipientUpdater
->expects($this->exactly(2))
->method('updateForAddedShare');

$this->assertFalse(
$this->userConfig->getValueBool($user1->getUID(), 'files_sharing', ConfigLexicon::USER_NEEDS_SHARE_REFRESH)
);

$this->sharesUpdatedListener->handle($event);
$this->time = 1;
$this->sharesUpdatedListener->handle($event);
$this->time = 4;
$this->sharesUpdatedListener->handle($event);

$this->assertTrue(
$this->userConfig->getValueBool($user1->getUID(), 'files_sharing', ConfigLexicon::USER_NEEDS_SHARE_REFRESH)
);
}

#[DataProvider('shareMarkAfterTimeProvider')]
public function testShareMarkAfterTime(float $cutOff, int $expectedCount) {
$share = $this->createMock(IShare::class);
Expand Down