Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Wrap renaming of notes through autotile in locking context #1047

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 32 additions & 4 deletions lib/Controller/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OCA\Notes\Controller;

use OCA\Notes\Service\Note;
use OCA\Notes\Service\NotesService;
use OCA\Notes\Service\SettingsService;

Expand All @@ -12,35 +13,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 +218,9 @@
$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();
});
Expand Down Expand Up @@ -258,14 +270,18 @@

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;
Expand Down Expand Up @@ -309,8 +325,8 @@
$path
);
$response = new StreamResponse($targetimage->fopen('rb'));
$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurldecode($targetimage->getName()) . '"');

Check warning on line 328 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 128 characters

Check warning on line 328 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 128 characters
$response->addHeader('Content-Type', $this->mimeTypeDetector->getSecureMimeType($targetimage->getMimeType()));

Check warning on line 329 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 122 characters

Check warning on line 329 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 122 characters
$response->addHeader('Cache-Control', 'public, max-age=604800');
return $response;
} catch (\Exception $e) {
Expand All @@ -332,4 +348,16 @@
);
});
}

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