Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\Text\Db\Session;
use OCA\Text\Db\SessionMapper;
use OCP\DirectEditing\IManager;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Lock\ILock;
use OCP\Files\Lock\ILockManager;
use OCP\Files\Lock\LockContext;
Expand Down Expand Up @@ -82,9 +83,11 @@ class DocumentService {
private IRootFolder $rootFolder;
private ICache $cache;
private IAppData $appData;
private ILockingProvider $lockingProvider;
private ILockManager $lockManager;
private $userMountCache;

public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapper, SessionMapper $sessionMapper, IAppData $appData, $userId, IRootFolder $rootFolder, ICacheFactory $cacheFactory, ILogger $logger, ShareManager $shareManager, IRequest $request, IManager $directManager, ILockingProvider $lockingProvider, ILockManager $lockManager) {
public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapper, SessionMapper $sessionMapper, IAppData $appData, $userId, IRootFolder $rootFolder, ICacheFactory $cacheFactory, ILogger $logger, ShareManager $shareManager, IRequest $request, IManager $directManager, ILockingProvider $lockingProvider, ILockManager $lockManager, IUserMountCache $userMountCache) {
$this->documentMapper = $documentMapper;
$this->stepMapper = $stepMapper;
$this->sessionMapper = $sessionMapper;
Expand All @@ -96,6 +99,7 @@ public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapp
$this->shareManager = $shareManager;
$this->lockingProvider = $lockingProvider;
$this->lockManager = $lockManager;
$this->userMountCache = $userMountCache;
$token = $request->getParam('token');
if ($this->userId === null && $token !== null) {
try {
Expand Down Expand Up @@ -384,8 +388,21 @@ public function getFileForSession(Session $session, $shareToken) {
* @throws NotFoundException
*/
public function getFileById($fileId, $userId = null): Node {
$userId = $userId ?? $this->userId;

// If no user is provided we need to get any file from existing mounts for cleanup jobs
if ($userId === null) {
$mounts = $this->userMountCache->getMountsForFileId($fileId);
$anyMount = array_shift($mounts);
if ($anyMount === null) {
throw new NotFoundException('Could not fallback to file from mounts');
}

$userId = $anyMount->getUser()->getUID();
}

try {
$userFolder = $this->rootFolder->getUserFolder($this->userId ?? $userId);
$userFolder = $this->rootFolder->getUserFolder($userId);
} catch (\OC\User\NoUserException $e) {
// It is a bit hacky to depend on internal exceptions here. But it is the best we can do for now
throw new NotFoundException();
Expand Down Expand Up @@ -502,17 +519,16 @@ public function lock(int $fileId): bool {
return true;
}

$file = $this->getFileById($fileId);
try {
$file = $this->getFileById($fileId);
$this->lockManager->lock(new LockContext(
$file,
ILock::TYPE_APP,
Application::APP_NAME
));
} catch (NoLockProviderException $e) {
} catch (NoLockProviderException | PreConditionNotMetException | NotFoundException $e) {
} catch (OwnerLockedException $e) {
return false;
} catch (PreConditionNotMetException $e) {
}
return true;
}
Expand All @@ -522,15 +538,14 @@ public function unlock(int $fileId): void {
return;
}

$file = $this->getFileById($fileId);
try {
$file = $this->getFileById($fileId);
$this->lockManager->unlock(new LockContext(
$file,
ILock::TYPE_APP,
Application::APP_NAME
));
} catch (NoLockProviderException $e) {
} catch (PreConditionNotMetException $e) {
} catch (NoLockProviderException | PreConditionNotMetException | NotFoundException $e) {
}
}
}