Skip to content

Commit

Permalink
fix(psalm): systemtags
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Apr 28, 2023
1 parent 74ced3d commit 67ae3c5
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 9 deletions.
5 changes: 4 additions & 1 deletion apps/dav/lib/SystemTag/SystemTagList.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public function __construct(array $tags, ISystemTagManager $tagManager, IUser $u
$this->user = $user;
}

public function getTags() {
/**
* @return ISystemTag[]
*/
public function getTags(): array {
return $this->tags;
}

Expand Down
5 changes: 5 additions & 0 deletions apps/dav/lib/SystemTag/SystemTagMappingNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public function getName() {
* @param string $name The new name
*
* @throws MethodNotAllowed not allowed to rename node
*
* @return never
*/
public function setName($name) {
throw new MethodNotAllowed();
Expand All @@ -145,13 +147,16 @@ public function setName($name) {
/**
* Returns null, not supported
*
* @return null
*/
public function getLastModified() {
return null;
}

/**
* Delete tag to object association
*
* @return void
*/
public function delete() {
try {
Expand Down
9 changes: 8 additions & 1 deletion apps/dav/lib/SystemTag/SystemTagNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public function getSystemTag() {
* @param string $name The new name
*
* @throws MethodNotAllowed not allowed to rename node
*
* @return never
*/
public function setName($name) {
throw new MethodNotAllowed();
Expand All @@ -114,11 +116,12 @@ public function setName($name) {
* @param string $name new tag name
* @param bool $userVisible user visible
* @param bool $userAssignable user assignable
*
* @throws NotFound whenever the given tag id does not exist
* @throws Forbidden whenever there is no permission to update said tag
* @throws Conflict whenever a tag already exists with the given attributes
*/
public function update($name, $userVisible, $userAssignable) {
public function update($name, $userVisible, $userAssignable): void {
try {
if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
Expand Down Expand Up @@ -151,11 +154,15 @@ public function update($name, $userVisible, $userAssignable) {
/**
* Returns null, not supported
*
* @return null
*/
public function getLastModified() {
return null;
}

/**
* @return void
*/
public function delete() {
try {
if (!$this->isAdmin) {
Expand Down
11 changes: 8 additions & 3 deletions apps/dav/lib/SystemTag/SystemTagPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {

/** @var array<int, int[]> */
private array $cachedTagMappings = [];
/** @var array<int, ISystemTag> */
/** @var array<string, ISystemTag> */
private array $cachedTags = [];

private ISystemTagObjectMapper $tagMapper;
Expand Down Expand Up @@ -225,6 +225,8 @@ private function createTag($data, $contentType = 'application/json') {
*
* @param PropFind $propFind
* @param \Sabre\DAV\INode $node
*
* @return void
*/
public function handleGetProperties(
PropFind $propFind,
Expand Down Expand Up @@ -281,9 +283,9 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
&& !is_null($propFind->getStatus(self::SYSTEM_TAGS_PROPERTYNAME))) {
// note: pre-fetching only supported for depth <= 1
$folderContent = $node->getNode()->getDirectoryListing();
$fileIds[] = (int)$node->getId();
$fileIds[] = $node->getId();
foreach ($folderContent as $info) {
$fileIds[] = (int)$info->getId();
$fileIds[] = $info->getId();
}
$tags = $this->tagMapper->getTagIdsForObjects($fileIds, 'files');

Expand Down Expand Up @@ -318,20 +320,23 @@ private function getTagsForFile(int $fileId): array {
$tagIds = [];
}
}

$tags = array_filter(array_map(function($tagId) {
return $this->cachedTags[$tagId] ?? null;
}, $tagIds));

$uncachedTagIds = array_filter($tagIds, function($tagId): bool {
return !isset($this->cachedTags[$tagId]);
});

if (count($uncachedTagIds)) {
$retrievedTags = $this->tagManager->getTagsByIds($uncachedTagIds);
foreach ($retrievedTags as $tag) {
$this->cachedTags[$tag->getId()] = $tag;
}
$tags += $retrievedTags;
}

return array_filter($tags, function(ISystemTag $tag) use ($user) {
return $this->tagManager->canUserSeeTag($tag, $user);
});
Expand Down
25 changes: 24 additions & 1 deletion apps/dav/lib/SystemTag/SystemTagsByIdCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,28 @@ private function isAdmin() {
/**
* @param string $name
* @param resource|string $data Initial payload
*
* @throws Forbidden
*
* @return never
*/
public function createFile($name, $data = null) {
throw new Forbidden('Cannot create tags by id');
}

/**
* @param string $name
*
* @return never
*/
public function createDirectory($name) {
throw new Forbidden('Permission denied to create collections');
}

/**
* @param string $name
*
* @return SystemTagNode
*/
public function getChild($name) {
try {
Expand All @@ -115,6 +122,11 @@ public function getChild($name) {
}
}

/**
* @return SystemTagNode[]
*
* @psalm-return array<SystemTagNode>
*/
public function getChildren() {
$visibilityFilter = true;
if ($this->isAdmin()) {
Expand Down Expand Up @@ -145,22 +157,33 @@ public function childExists($name) {
}
}

/**
* @return never
*/
public function delete() {
throw new Forbidden('Permission denied to delete this collection');
}

/**
* @return string
*
* @psalm-return 'systemtags'
*/
public function getName() {
return 'systemtags';
}

/**
* @return never
*/
public function setName($name) {
throw new Forbidden('Permission denied to rename this collection');
}

/**
* Returns the last modification time, as a unix timestamp
*
* @return int
* @return null
*/
public function getLastModified() {
return null;
Expand Down
22 changes: 21 additions & 1 deletion apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public function __construct(
$this->user = $user;
}

/**
* @return void
*/
public function createFile($name, $data = null) {
$tagId = $name;
try {
Expand All @@ -110,10 +113,16 @@ public function createFile($name, $data = null) {
}
}

/**
* @return never
*/
public function createDirectory($name) {
throw new Forbidden('Permission denied to create collections');
}

/**
* @return SystemTagMappingNode
*/
public function getChild($tagName) {
try {
if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagName, true)) {
Expand All @@ -131,6 +140,11 @@ public function getChild($tagName) {
}
}

/**
* @return SystemTagMappingNode[]
*
* @psalm-return list<SystemTagMappingNode>
*/
public function getChildren() {
$tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType));
if (empty($tagIds)) {
Expand Down Expand Up @@ -168,6 +182,9 @@ public function childExists($tagId) {
}
}

/**
* @return never
*/
public function delete() {
throw new Forbidden('Permission denied to delete this collection');
}
Expand All @@ -176,14 +193,17 @@ public function getName() {
return $this->objectId;
}

/**
* @return never
*/
public function setName($name) {
throw new Forbidden('Permission denied to rename this collection');
}

/**
* Returns the last modification time, as a unix timestamp
*
* @return int
* @return null
*/
public function getLastModified() {
return null;
Expand Down
18 changes: 16 additions & 2 deletions apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public function __construct(
/**
* @param string $name
* @param resource|string $data Initial payload
* @return null|string
*
* @return never
*
* @throws Forbidden
*/
public function createFile($name, $data = null) {
Expand All @@ -107,7 +109,10 @@ public function createFile($name, $data = null) {

/**
* @param string $name
*
* @throws Forbidden
*
* @return never
*/
public function createDirectory($name) {
throw new Forbidden('Permission denied to create collections');
Expand All @@ -133,6 +138,9 @@ public function getChild($objectName) {
);
}

/**
* @return never
*/
public function getChildren() {
// do not list object ids
throw new MethodNotAllowed();
Expand All @@ -148,6 +156,9 @@ public function childExists($name) {
return call_user_func($this->childExistsFunction, $name);
}

/**
* @return never
*/
public function delete() {
throw new Forbidden('Permission denied to delete this collection');
}
Expand All @@ -158,7 +169,10 @@ public function getName() {

/**
* @param string $name
*
* @throws Forbidden
*
* @return never
*/
public function setName($name) {
throw new Forbidden('Permission denied to rename this collection');
Expand All @@ -167,7 +181,7 @@ public function setName($name) {
/**
* Returns the last modification time, as a unix timestamp
*
* @return int
* @return null
*/
public function getLastModified() {
return null;
Expand Down
8 changes: 8 additions & 0 deletions apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ function ($name) {
parent::__construct('root', $children);
}

/**
* @return string
*
* @psalm-return 'systemtags-relations'
*/
public function getName() {
return 'systemtags-relations';
}

/**
* @return never
*/
public function setName($name) {
throw new Forbidden('Permission denied to rename this collection');
}
Expand Down

0 comments on commit 67ae3c5

Please sign in to comment.