Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: polish SystemTagsInUseCollection
- DI SystemTagManager
- add some comments and doc
- catch NoUserException
- add return type hints

Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz committed May 16, 2023
commit f50ce8b3bd3e119167f48972f5e5a55686145d72
5 changes: 1 addition & 4 deletions apps/dav/lib/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ public function __construct() {
$groupManager,
\OC::$server->getEventDispatcher()
);
$systemTagInUseCollection = new SystemTag\SystemTagsInUseCollection(
$userSession,
$rootFolder
);
$systemTagInUseCollection = \OCP\Server::get(SystemTag\SystemTagsInUseCollection::class);
$commentsCollection = new Comments\RootCollection(
\OC::$server->getCommentsManager(),
$userManager,
Expand Down
40 changes: 32 additions & 8 deletions apps/dav/lib/SystemTag/SystemTagsInUseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,31 @@
namespace OCA\DAV\SystemTag;

use OC\SystemTag\SystemTag;
use OC\User\NoUserException;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IUserSession;
use OCP\SystemTag\ISystemTagManager;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\SimpleCollection;

class SystemTagsInUseCollection extends \Sabre\DAV\SimpleCollection {
class SystemTagsInUseCollection extends SimpleCollection {
protected IUserSession $userSession;
protected IRootFolder $rootFolder;
protected string $mediaType;
private ISystemTagManager $systemTagManager;

public function __construct(IUserSession $userSession, IRootFolder $rootFolder, string $mediaType = '') {
/** @noinspection PhpMissingParentConstructorInspection */
public function __construct(
IUserSession $userSession,
IRootFolder $rootFolder,
ISystemTagManager $systemTagManager,
string $mediaType = ''
) {
$this->userSession = $userSession;
$this->rootFolder = $rootFolder;
$this->systemTagManager = $systemTagManager;
$this->mediaType = $mediaType;
$this->name = 'systemtags-assigned';
if ($this->mediaType != '') {
Expand All @@ -52,25 +63,38 @@ public function setName($name): void {
throw new Forbidden('Permission denied to rename this collection');
}

public function getChild($name) {
public function getChild($name): self {
if ($this->mediaType !== '') {
throw new NotFound('Invalid media type');
}
return new self($this->userSession, $this->rootFolder, $name);
return new self($this->userSession, $this->rootFolder, $this->systemTagManager, $name);
}

public function getChildren() {
/**
* @return SystemTagNode[]
* @throws NotPermittedException
* @throws Forbidden
*/
public function getChildren(): array {
$user = $this->userSession->getUser();
if ($user === null) {
$userFolder = null;
try {
if ($user) {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
}
} catch (NoUserException) {
// will throw a Sabre exception in the next step.
}
if ($user === null || $userFolder === null) {
throw new Forbidden('Permission denied to read this collection');
}

$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$result = $userFolder->getSystemTags($this->mediaType);
$children = [];
foreach ($result as $tagData) {
$tag = new SystemTag((string)$tagData['id'], $tagData['name'], (bool)$tagData['visibility'], (bool)$tagData['editable']);
$node = new SystemTagNode($tag, $user, false, \OCP\Server::get(ISystemTagManager::class));
// read only, so we can submit the isAdmin parameter as false generally
$node = new SystemTagNode($tag, $user, false, $this->systemTagManager);
$node->setNumberOfFiles($tagData['number_files']);
$node->setReferenceFileId($tagData['ref_file_id']);
$children[] = $node;
Expand Down