From fdbf2b60640bac89b694c9f88b5c24114fc56fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Wed, 17 May 2023 14:14:12 +0200 Subject: [PATCH] fix: Wrap renaming of notes through autotile in locking context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Controller/NotesController.php | 36 ++++++++++++++++++++++++++---- lib/Service/Note.php | 4 ++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/Controller/NotesController.php b/lib/Controller/NotesController.php index ce8f5695..cb946b73 100644 --- a/lib/Controller/NotesController.php +++ b/lib/Controller/NotesController.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace OCA\Notes\Controller; +use OCA\Notes\Service\Note; use OCA\Notes\Service\NotesService; use OCA\Notes\Service\SettingsService; @@ -12,6 +13,9 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\StreamResponse; use OCP\Files\IMimeTypeDetector; +use OCP\Files\Lock\ILock; +use OCP\Files\Lock\ILockManager; +use OCP\Files\Lock\LockContext; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -19,28 +23,34 @@ use OCP\IRequest; class NotesController extends Controller { private NotesService $notesService; private SettingsService $settingsService; + private ILockManager $lockManager; private Helper $helper; private IConfig $settings; private IL10N $l10n; private IMimeTypeDetector $mimeTypeDetector; + private string $userId; public function __construct( string $AppName, IRequest $request, NotesService $notesService, + ILockManager $lockManager, SettingsService $settingsService, Helper $helper, IConfig $settings, IL10N $l10n, - IMimeTypeDetector $mimeTypeDetector + IMimeTypeDetector $mimeTypeDetector, + string $userId ) { parent::__construct($AppName, $request); $this->notesService = $notesService; $this->settingsService = $settingsService; + $this->lockManager = $lockManager; $this->helper = $helper; $this->settings = $settings; $this->l10n = $l10n; $this->mimeTypeDetector = $mimeTypeDetector; + $this->userId = $userId; } /** @@ -208,7 +218,9 @@ class NotesController extends Controller { $oldTitle = $note->getTitle(); $newTitle = $this->notesService->getTitleFromContent($note->getContent()); if ($oldTitle !== $newTitle) { - $note->setTitle($newTitle); + $this->inLockScope($note, function () use ($note, $newTitle) { + $note->setTitle($newTitle); + }); } return $note->getTitle(); }); @@ -258,14 +270,18 @@ class NotesController extends Controller { case 'title': if ($title !== null) { - $note->setTitle($title); + $this->inLockScope($note, function () use ($note, $title) { + $note->setTitle($title); + }); } $result = $note->getTitle(); break; case 'category': if ($category !== null) { - $note->setCategory($category); + $this->inLockScope($note, function () use ($note, $category) { + $note->setCategory($category); + }); } $result = $note->getCategory(); break; @@ -332,4 +348,16 @@ class NotesController extends Controller { ); }); } + + private function inLockScope(Note $note, callable $callback) { + $isRichText = $this->settingsService->get($this->userId, 'noteMode') === 'rich'; + $lockContext = new LockContext( + $note->getFile(), + $isRichText ? ILock::TYPE_APP : ILock::TYPE_USER, + $isRichText ? 'text' : $this->userId + ); + $this->lockManager->runInScope($lockContext, function () use ($callback) { + $callback(); + }); + } } diff --git a/lib/Service/Note.php b/lib/Service/Note.php index f707190b..ecc2ace2 100644 --- a/lib/Service/Note.php +++ b/lib/Service/Note.php @@ -179,4 +179,8 @@ class Note { $this->noteUtil->getTagService()->setFavorite($this->getId(), $favorite); } } + + public function getFile(): File { + return $this->file; + } }