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
35 changes: 27 additions & 8 deletions lib/Controller/WhiteboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Exception;
use OCA\Whiteboard\Exception\InvalidUserException;
use OCA\Whiteboard\Exception\UnauthorizedException;
use OCA\Whiteboard\Exception\WhiteboardConflictException;
use OCA\Whiteboard\Service\Authentication\GetUserFromIdServiceFactory;
use OCA\Whiteboard\Service\ConfigService;
use OCA\Whiteboard\Service\ExceptionService;
Expand All @@ -20,6 +21,7 @@
use OCA\Whiteboard\Service\WhiteboardContentService;
use OCA\Whiteboard\Service\WhiteboardLibraryService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
Expand Down Expand Up @@ -75,23 +77,40 @@ public function show(int $fileId): DataResponse {
#[PublicPage]
public function update(int $fileId, array $data): DataResponse {
$lockKey = "sync_lock_{$fileId}";
$lockValue = uniqid();
$lockValue = uniqid('', true);
$lockTTL = 5; // 5 seconds

// Simple distributed lock
if (!$this->cache->add($lockKey, $lockValue, $lockTTL)) {
return new DataResponse(['status' => 'conflict'], 409);
}

try {
$maxLockAttempts = 5;
for ($attempt = 0; $attempt < $maxLockAttempts; $attempt++) {
if ($this->cache->add($lockKey, $lockValue, $lockTTL)) {
break;
}

if ($attempt === $maxLockAttempts - 1) {
throw new Exception('Whiteboard sync is temporarily busy', Http::STATUS_SERVICE_UNAVAILABLE);
}

usleep(100000);
}

$jwt = $this->getJwtFromRequest();
$userId = $this->jwtService->getUserIdFromJWT($jwt);
$user = $this->getUserFromIdServiceFactory->create($userId)->getUser();
$file = $this->getFileServiceFactory->create($user, $fileId)->getFile();

$this->contentService->updateContent($file, $data);
$meta = $this->contentService->updateContent($file, $data, $userId);

return new DataResponse(['status' => 'success']);
return new DataResponse([
'status' => 'success',
'meta' => $meta,
]);

} catch (WhiteboardConflictException $e) {
return new DataResponse([
'status' => 'conflict',
'data' => $e->getCurrentDocument(),
], Http::STATUS_CONFLICT);

} catch (Exception $e) {
$this->logger->error('Error syncing whiteboard data: ' . $e->getMessage());
Expand Down
30 changes: 30 additions & 0 deletions lib/Exception/WhiteboardConflictException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Exception;

use Exception;

final class WhiteboardConflictException extends Exception {
/**
* @param array<string,mixed> $currentDocument
*/
public function __construct(
private array $currentDocument,
) {
parent::__construct('Whiteboard content conflict', 409);
}

/**
* @return array<string,mixed>
*/
public function getCurrentDocument(): array {
return $this->currentDocument;
}
}
Loading
Loading