aboutsummaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorJohn Molakvoæ <skjnldsv@protonmail.com>2023-04-28 10:59:34 +0200
committerJohn Molakvoæ <skjnldsv@protonmail.com>2023-04-28 12:23:52 +0200
commit20df0019f1af926b838ef548ec22a9c809537b70 (patch)
tree9228f807ef88168b2dd12978f5cd8e98f5918b52 /apps
parent74ced3d2eb3952799a968bc08c28abb51c1542d8 (diff)
downloadnextcloud-server-20df0019f1af926b838ef548ec22a9c809537b70.tar.gz
nextcloud-server-20df0019f1af926b838ef548ec22a9c809537b70.zip
fix(psalm): systemtags
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'apps')
-rw-r--r--apps/dav/lib/SystemTag/SystemTagList.php12
-rw-r--r--apps/dav/lib/SystemTag/SystemTagMappingNode.php5
-rw-r--r--apps/dav/lib/SystemTag/SystemTagNode.php9
-rw-r--r--apps/dav/lib/SystemTag/SystemTagPlugin.php34
-rw-r--r--apps/dav/lib/SystemTag/SystemTagsByIdCollection.php25
-rw-r--r--apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php22
-rw-r--r--apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php18
-rw-r--r--apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php8
8 files changed, 110 insertions, 23 deletions
diff --git a/apps/dav/lib/SystemTag/SystemTagList.php b/apps/dav/lib/SystemTag/SystemTagList.php
index 67d33b9701e..678c8042a39 100644
--- a/apps/dav/lib/SystemTag/SystemTagList.php
+++ b/apps/dav/lib/SystemTag/SystemTagList.php
@@ -1,10 +1,8 @@
<?php
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
*
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Vincent Petry <vincent@nextcloud.com>
+ * @author Robin Appelman <robin@icewind.nl>
*
* @license AGPL-3.0
*
@@ -19,7 +17,6 @@
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
- *
*/
namespace OCA\DAV\SystemTag;
@@ -49,7 +46,10 @@ class SystemTagList implements Element {
$this->user = $user;
}
- public function getTags() {
+ /**
+ * @return ISystemTag[]
+ */
+ public function getTags(): array {
return $this->tags;
}
diff --git a/apps/dav/lib/SystemTag/SystemTagMappingNode.php b/apps/dav/lib/SystemTag/SystemTagMappingNode.php
index 344ff1dbc70..9762b6e1db9 100644
--- a/apps/dav/lib/SystemTag/SystemTagMappingNode.php
+++ b/apps/dav/lib/SystemTag/SystemTagMappingNode.php
@@ -137,6 +137,8 @@ class SystemTagMappingNode implements \Sabre\DAV\INode {
* @param string $name The new name
*
* @throws MethodNotAllowed not allowed to rename node
+ *
+ * @return never
*/
public function setName($name) {
throw new MethodNotAllowed();
@@ -145,6 +147,7 @@ class SystemTagMappingNode implements \Sabre\DAV\INode {
/**
* Returns null, not supported
*
+ * @return null
*/
public function getLastModified() {
return null;
@@ -152,6 +155,8 @@ class SystemTagMappingNode implements \Sabre\DAV\INode {
/**
* Delete tag to object association
+ *
+ * @return void
*/
public function delete() {
try {
diff --git a/apps/dav/lib/SystemTag/SystemTagNode.php b/apps/dav/lib/SystemTag/SystemTagNode.php
index a31deb59a93..7310cdb19a2 100644
--- a/apps/dav/lib/SystemTag/SystemTagNode.php
+++ b/apps/dav/lib/SystemTag/SystemTagNode.php
@@ -103,6 +103,8 @@ class SystemTagNode implements \Sabre\DAV\INode {
* @param string $name The new name
*
* @throws MethodNotAllowed not allowed to rename node
+ *
+ * @return never
*/
public function setName($name) {
throw new MethodNotAllowed();
@@ -114,11 +116,12 @@ class SystemTagNode implements \Sabre\DAV\INode {
* @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');
@@ -151,11 +154,15 @@ class SystemTagNode implements \Sabre\DAV\INode {
/**
* Returns null, not supported
*
+ * @return null
*/
public function getLastModified() {
return null;
}
+ /**
+ * @return void
+ */
public function delete() {
try {
if (!$this->isAdmin) {
diff --git a/apps/dav/lib/SystemTag/SystemTagPlugin.php b/apps/dav/lib/SystemTag/SystemTagPlugin.php
index 5fe1c013571..c5c828cfbff 100644
--- a/apps/dav/lib/SystemTag/SystemTagPlugin.php
+++ b/apps/dav/lib/SystemTag/SystemTagPlugin.php
@@ -28,6 +28,7 @@ namespace OCA\DAV\SystemTag;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\Node;
use OCP\IGroupManager;
+use OCP\IUser;
use OCP\IUserSession;
use OCP\SystemTag\ISystemTag;
use OCP\SystemTag\ISystemTagManager;
@@ -81,9 +82,9 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
*/
protected $groupManager;
- /** @var array<int, int[]> */
+ /** @var array<int, string[]> */
private array $cachedTagMappings = [];
- /** @var array<int, ISystemTag> */
+ /** @var array<string, ISystemTag> */
private array $cachedTags = [];
private ISystemTagObjectMapper $tagMapper;
@@ -225,6 +226,8 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
*
* @param PropFind $propFind
* @param \Sabre\DAV\INode $node
+ *
+ * @return void
*/
public function handleGetProperties(
PropFind $propFind,
@@ -279,16 +282,19 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
if ($node instanceof Directory
&& $propFind->getDepth() !== 0
&& !is_null($propFind->getStatus(self::SYSTEM_TAGS_PROPERTYNAME))) {
+ $fileIds = [$node->getId()];
+
// note: pre-fetching only supported for depth <= 1
$folderContent = $node->getNode()->getDirectoryListing();
- $fileIds[] = (int)$node->getId();
foreach ($folderContent as $info) {
- $fileIds[] = (int)$info->getId();
+ $fileIds[] = $info->getId();
}
+
$tags = $this->tagMapper->getTagIdsForObjects($fileIds, 'files');
$this->cachedTagMappings = $this->cachedTagMappings + $tags;
$emptyFileIds = array_diff($fileIds, array_keys($tags));
+
// also cache the ones that were not found
foreach ($emptyFileIds as $fileId) {
$this->cachedTagMappings[$fileId] = [];
@@ -296,8 +302,13 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
}
$propFind->handle(self::SYSTEM_TAGS_PROPERTYNAME, function () use ($node) {
- $tags = $this->getTagsForFile($node->getId());
- return new SystemTagList($tags, $this->tagManager, $this->userSession->getUser());
+ $user = $this->userSession->getUser();
+ if ($user === null) {
+ return;
+ }
+
+ $tags = $this->getTagsForFile($node->getId(), $user);
+ return new SystemTagList($tags, $this->tagManager, $user);
});
}
@@ -305,8 +316,8 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
* @param int $fileId
* @return ISystemTag[]
*/
- private function getTagsForFile(int $fileId): array {
- $user = $this->userSession->getUser();
+ private function getTagsForFile(int $fileId, IUser $user): array {
+
if (isset($this->cachedTagMappings[$fileId])) {
$tagIds = $this->cachedTagMappings[$fileId];
} else {
@@ -318,13 +329,15 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
$tagIds = [];
}
}
- $tags = array_filter(array_map(function($tagId) {
+
+ $tags = array_filter(array_map(function(string $tagId) {
return $this->cachedTags[$tagId] ?? null;
}, $tagIds));
- $uncachedTagIds = array_filter($tagIds, function($tagId): bool {
+ $uncachedTagIds = array_filter($tagIds, function(string $tagId): bool {
return !isset($this->cachedTags[$tagId]);
});
+
if (count($uncachedTagIds)) {
$retrievedTags = $this->tagManager->getTagsByIds($uncachedTagIds);
foreach ($retrievedTags as $tag) {
@@ -332,6 +345,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
}
$tags += $retrievedTags;
}
+
return array_filter($tags, function(ISystemTag $tag) use ($user) {
return $this->tagManager->canUserSeeTag($tag, $user);
});
diff --git a/apps/dav/lib/SystemTag/SystemTagsByIdCollection.php b/apps/dav/lib/SystemTag/SystemTagsByIdCollection.php
index 1256c58921e..86ccadf5f56 100644
--- a/apps/dav/lib/SystemTag/SystemTagsByIdCollection.php
+++ b/apps/dav/lib/SystemTag/SystemTagsByIdCollection.php
@@ -84,7 +84,10 @@ class SystemTagsByIdCollection implements ICollection {
/**
* @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');
@@ -92,6 +95,8 @@ class SystemTagsByIdCollection implements ICollection {
/**
* @param string $name
+ *
+ * @return never
*/
public function createDirectory($name) {
throw new Forbidden('Permission denied to create collections');
@@ -99,6 +104,8 @@ class SystemTagsByIdCollection implements ICollection {
/**
* @param string $name
+ *
+ * @return SystemTagNode
*/
public function getChild($name) {
try {
@@ -115,6 +122,11 @@ class SystemTagsByIdCollection implements ICollection {
}
}
+ /**
+ * @return SystemTagNode[]
+ *
+ * @psalm-return array<SystemTagNode>
+ */
public function getChildren() {
$visibilityFilter = true;
if ($this->isAdmin()) {
@@ -145,14 +157,25 @@ class SystemTagsByIdCollection implements ICollection {
}
}
+ /**
+ * @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');
}
@@ -160,7 +183,7 @@ class SystemTagsByIdCollection implements ICollection {
/**
* Returns the last modification time, as a unix timestamp
*
- * @return int
+ * @return null
*/
public function getLastModified() {
return null;
diff --git a/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php b/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php
index 8bb34182b0c..4d73c17d7dd 100644
--- a/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php
+++ b/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php
@@ -92,6 +92,9 @@ class SystemTagsObjectMappingCollection implements ICollection {
$this->user = $user;
}
+ /**
+ * @return void
+ */
public function createFile($name, $data = null) {
$tagId = $name;
try {
@@ -110,10 +113,16 @@ class SystemTagsObjectMappingCollection implements ICollection {
}
}
+ /**
+ * @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)) {
@@ -131,6 +140,11 @@ class SystemTagsObjectMappingCollection implements ICollection {
}
}
+ /**
+ * @return SystemTagMappingNode[]
+ *
+ * @psalm-return list<SystemTagMappingNode>
+ */
public function getChildren() {
$tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType));
if (empty($tagIds)) {
@@ -168,6 +182,9 @@ class SystemTagsObjectMappingCollection implements ICollection {
}
}
+ /**
+ * @return never
+ */
public function delete() {
throw new Forbidden('Permission denied to delete this collection');
}
@@ -176,6 +193,9 @@ class SystemTagsObjectMappingCollection implements ICollection {
return $this->objectId;
}
+ /**
+ * @return never
+ */
public function setName($name) {
throw new Forbidden('Permission denied to rename this collection');
}
@@ -183,7 +203,7 @@ class SystemTagsObjectMappingCollection implements ICollection {
/**
* Returns the last modification time, as a unix timestamp
*
- * @return int
+ * @return null
*/
public function getLastModified() {
return null;
diff --git a/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php b/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php
index 1ca45c32ce4..3fa40278cdb 100644
--- a/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php
+++ b/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php
@@ -98,7 +98,9 @@ class SystemTagsObjectTypeCollection implements ICollection {
/**
* @param string $name
* @param resource|string $data Initial payload
- * @return null|string
+ *
+ * @return never
+ *
* @throws Forbidden
*/
public function createFile($name, $data = null) {
@@ -107,7 +109,10 @@ class SystemTagsObjectTypeCollection implements ICollection {
/**
* @param string $name
+ *
* @throws Forbidden
+ *
+ * @return never
*/
public function createDirectory($name) {
throw new Forbidden('Permission denied to create collections');
@@ -133,6 +138,9 @@ class SystemTagsObjectTypeCollection implements ICollection {
);
}
+ /**
+ * @return never
+ */
public function getChildren() {
// do not list object ids
throw new MethodNotAllowed();
@@ -148,6 +156,9 @@ class SystemTagsObjectTypeCollection implements ICollection {
return call_user_func($this->childExistsFunction, $name);
}
+ /**
+ * @return never
+ */
public function delete() {
throw new Forbidden('Permission denied to delete this collection');
}
@@ -158,7 +169,10 @@ class SystemTagsObjectTypeCollection implements ICollection {
/**
* @param string $name
+ *
* @throws Forbidden
+ *
+ * @return never
*/
public function setName($name) {
throw new Forbidden('Permission denied to rename this collection');
@@ -167,7 +181,7 @@ class SystemTagsObjectTypeCollection implements ICollection {
/**
* Returns the last modification time, as a unix timestamp
*
- * @return int
+ * @return null
*/
public function getLastModified() {
return null;
diff --git a/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php b/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php
index 199bf28fb7d..8341c6ca009 100644
--- a/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php
+++ b/apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php
@@ -85,7 +85,10 @@ class SystemTagPluginTest extends \Test\TestCase {
*/
private $plugin;
- private ISystemTagObjectMapper $tagMapper;
+ /**
+ * @var ISystemTagObjectMapper
+ */
+ private $tagMapper;
protected function setUp(): void {
parent::setUp();
@@ -111,7 +114,8 @@ class SystemTagPluginTest extends \Test\TestCase {
->expects($this->any())
->method('isLoggedIn')
->willReturn(true);
- $this->tagMapper = $this->getMockBuilder(ISystemTagObjectMapper::class);
+ $this->tagMapper = $this->getMockBuilder(ISystemTagObjectMapper::class)
+ ->getMock();
$this->plugin = new \OCA\DAV\SystemTag\SystemTagPlugin(
$this->tagManager,