Skip to content

Commit fdbf2b6

Browse files
committed
fix: Wrap renaming of notes through autotile in locking context
Signed-off-by: Julius Härtl <[email protected]>
1 parent 5074204 commit fdbf2b6

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/Controller/NotesController.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace OCA\Notes\Controller;
66

7+
use OCA\Notes\Service\Note;
78
use OCA\Notes\Service\NotesService;
89
use OCA\Notes\Service\SettingsService;
910

@@ -12,35 +13,44 @@
1213
use OCP\AppFramework\Http\JSONResponse;
1314
use OCP\AppFramework\Http\StreamResponse;
1415
use OCP\Files\IMimeTypeDetector;
16+
use OCP\Files\Lock\ILock;
17+
use OCP\Files\Lock\ILockManager;
18+
use OCP\Files\Lock\LockContext;
1519
use OCP\IConfig;
1620
use OCP\IL10N;
1721
use OCP\IRequest;
1822

1923
class NotesController extends Controller {
2024
private NotesService $notesService;
2125
private SettingsService $settingsService;
26+
private ILockManager $lockManager;
2227
private Helper $helper;
2328
private IConfig $settings;
2429
private IL10N $l10n;
2530
private IMimeTypeDetector $mimeTypeDetector;
31+
private string $userId;
2632

2733
public function __construct(
2834
string $AppName,
2935
IRequest $request,
3036
NotesService $notesService,
37+
ILockManager $lockManager,
3138
SettingsService $settingsService,
3239
Helper $helper,
3340
IConfig $settings,
3441
IL10N $l10n,
35-
IMimeTypeDetector $mimeTypeDetector
42+
IMimeTypeDetector $mimeTypeDetector,
43+
string $userId
3644
) {
3745
parent::__construct($AppName, $request);
3846
$this->notesService = $notesService;
3947
$this->settingsService = $settingsService;
48+
$this->lockManager = $lockManager;
4049
$this->helper = $helper;
4150
$this->settings = $settings;
4251
$this->l10n = $l10n;
4352
$this->mimeTypeDetector = $mimeTypeDetector;
53+
$this->userId = $userId;
4454
}
4555

4656
/**
@@ -208,7 +218,9 @@ public function autotitle(int $id) : JSONResponse {
208218
$oldTitle = $note->getTitle();
209219
$newTitle = $this->notesService->getTitleFromContent($note->getContent());
210220
if ($oldTitle !== $newTitle) {
211-
$note->setTitle($newTitle);
221+
$this->inLockScope($note, function () use ($note, $newTitle) {
222+
$note->setTitle($newTitle);
223+
});
212224
}
213225
return $note->getTitle();
214226
});
@@ -258,14 +270,18 @@ public function updateProperty(
258270

259271
case 'title':
260272
if ($title !== null) {
261-
$note->setTitle($title);
273+
$this->inLockScope($note, function () use ($note, $title) {
274+
$note->setTitle($title);
275+
});
262276
}
263277
$result = $note->getTitle();
264278
break;
265279

266280
case 'category':
267281
if ($category !== null) {
268-
$note->setCategory($category);
282+
$this->inLockScope($note, function () use ($note, $category) {
283+
$note->setCategory($category);
284+
});
269285
}
270286
$result = $note->getCategory();
271287
break;
@@ -332,4 +348,16 @@ public function uploadFile(int $noteid): JSONResponse {
332348
);
333349
});
334350
}
351+
352+
private function inLockScope(Note $note, callable $callback) {
353+
$isRichText = $this->settingsService->get($this->userId, 'noteMode') === 'rich';
354+
$lockContext = new LockContext(
355+
$note->getFile(),
356+
$isRichText ? ILock::TYPE_APP : ILock::TYPE_USER,
357+
$isRichText ? 'text' : $this->userId
358+
);
359+
$this->lockManager->runInScope($lockContext, function () use ($callback) {
360+
$callback();
361+
});
362+
}
335363
}

lib/Service/Note.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,8 @@ public function setFavorite(bool $favorite) : void {
179179
$this->noteUtil->getTagService()->setFavorite($this->getId(), $favorite);
180180
}
181181
}
182+
183+
public function getFile(): File {
184+
return $this->file;
185+
}
182186
}

0 commit comments

Comments
 (0)