Skip to content

Commit

Permalink
fix: Wrap renaming of notes through autotile in locking context
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed May 22, 2023
1 parent 5074204 commit 86b171a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/Controller/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,44 @@
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;

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;
}

/**
Expand Down Expand Up @@ -208,7 +217,15 @@ public function autotitle(int $id) : JSONResponse {
$oldTitle = $note->getTitle();
$newTitle = $this->notesService->getTitleFromContent($note->getContent());
if ($oldTitle !== $newTitle) {
$note->setTitle($newTitle);
$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 ($note, $newTitle) {
$note->setTitle($newTitle);
});
}
return $note->getTitle();
});
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,8 @@ public function setFavorite(bool $favorite) : void {
$this->noteUtil->getTagService()->setFavorite($this->getId(), $favorite);
}
}

public function getFile(): File {
return $this->file;
}
}

0 comments on commit 86b171a

Please sign in to comment.