|
4 | 4 |
|
5 | 5 | namespace OCA\Notes\Controller; |
6 | 6 |
|
| 7 | +use OCA\Notes\Service\Note; |
7 | 8 | use OCA\Notes\Service\NotesService; |
8 | 9 | use OCA\Notes\Service\SettingsService; |
9 | 10 |
|
|
12 | 13 | use OCP\AppFramework\Http\JSONResponse; |
13 | 14 | use OCP\AppFramework\Http\StreamResponse; |
14 | 15 | use OCP\Files\IMimeTypeDetector; |
| 16 | +use OCP\Files\Lock\ILock; |
| 17 | +use OCP\Files\Lock\ILockManager; |
| 18 | +use OCP\Files\Lock\LockContext; |
15 | 19 | use OCP\IConfig; |
16 | 20 | use OCP\IL10N; |
17 | 21 | use OCP\IRequest; |
18 | 22 |
|
19 | 23 | class NotesController extends Controller { |
20 | 24 | private NotesService $notesService; |
21 | 25 | private SettingsService $settingsService; |
| 26 | + private ILockManager $lockManager; |
22 | 27 | private Helper $helper; |
23 | 28 | private IConfig $settings; |
24 | 29 | private IL10N $l10n; |
25 | 30 | private IMimeTypeDetector $mimeTypeDetector; |
| 31 | + private string $userId; |
26 | 32 |
|
27 | 33 | public function __construct( |
28 | 34 | string $AppName, |
29 | 35 | IRequest $request, |
30 | 36 | NotesService $notesService, |
| 37 | + ILockManager $lockManager, |
31 | 38 | SettingsService $settingsService, |
32 | 39 | Helper $helper, |
33 | 40 | IConfig $settings, |
34 | 41 | IL10N $l10n, |
35 | | - IMimeTypeDetector $mimeTypeDetector |
| 42 | + IMimeTypeDetector $mimeTypeDetector, |
| 43 | + string $userId |
36 | 44 | ) { |
37 | 45 | parent::__construct($AppName, $request); |
38 | 46 | $this->notesService = $notesService; |
39 | 47 | $this->settingsService = $settingsService; |
| 48 | + $this->lockManager = $lockManager; |
40 | 49 | $this->helper = $helper; |
41 | 50 | $this->settings = $settings; |
42 | 51 | $this->l10n = $l10n; |
43 | 52 | $this->mimeTypeDetector = $mimeTypeDetector; |
| 53 | + $this->userId = $userId; |
44 | 54 | } |
45 | 55 |
|
46 | 56 | /** |
@@ -208,7 +218,9 @@ public function autotitle(int $id) : JSONResponse { |
208 | 218 | $oldTitle = $note->getTitle(); |
209 | 219 | $newTitle = $this->notesService->getTitleFromContent($note->getContent()); |
210 | 220 | if ($oldTitle !== $newTitle) { |
211 | | - $note->setTitle($newTitle); |
| 221 | + $this->inLockScope($note, function () use ($note, $newTitle) { |
| 222 | + $note->setTitle($newTitle); |
| 223 | + }); |
212 | 224 | } |
213 | 225 | return $note->getTitle(); |
214 | 226 | }); |
@@ -258,14 +270,18 @@ public function updateProperty( |
258 | 270 |
|
259 | 271 | case 'title': |
260 | 272 | if ($title !== null) { |
261 | | - $note->setTitle($title); |
| 273 | + $this->inLockScope($note, function () use ($note, $title) { |
| 274 | + $note->setTitle($title); |
| 275 | + }); |
262 | 276 | } |
263 | 277 | $result = $note->getTitle(); |
264 | 278 | break; |
265 | 279 |
|
266 | 280 | case 'category': |
267 | 281 | if ($category !== null) { |
268 | | - $note->setCategory($category); |
| 282 | + $this->inLockScope($note, function () use ($note, $category) { |
| 283 | + $note->setCategory($category); |
| 284 | + }); |
269 | 285 | } |
270 | 286 | $result = $note->getCategory(); |
271 | 287 | break; |
@@ -332,4 +348,16 @@ public function uploadFile(int $noteid): JSONResponse { |
332 | 348 | ); |
333 | 349 | }); |
334 | 350 | } |
| 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 | + } |
335 | 363 | } |
0 commit comments