diff options
Diffstat (limited to 'apps/dav/lib/Comments')
-rw-r--r-- | apps/dav/lib/Comments/CommentNode.php | 75 | ||||
-rw-r--r-- | apps/dav/lib/Comments/CommentsPlugin.php | 61 | ||||
-rw-r--r-- | apps/dav/lib/Comments/EntityCollection.php | 49 | ||||
-rw-r--r-- | apps/dav/lib/Comments/EntityTypeCollection.php | 57 | ||||
-rw-r--r-- | apps/dav/lib/Comments/RootCollection.php | 81 |
5 files changed, 75 insertions, 248 deletions
diff --git a/apps/dav/lib/Comments/CommentNode.php b/apps/dav/lib/Comments/CommentNode.php index af76027671e..5dbefa82d93 100644 --- a/apps/dav/lib/Comments/CommentNode.php +++ b/apps/dav/lib/Comments/CommentNode.php @@ -1,34 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * 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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\DAV\Comments; use OCP\Comments\IComment; use OCP\Comments\ICommentsManager; use OCP\Comments\MessageTooLongException; -use OCP\ILogger; use OCP\IUserManager; use OCP\IUserSession; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\BadRequest; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\MethodNotAllowed; @@ -46,57 +30,30 @@ class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties { public const PROPERTY_NAME_MENTION_ID = '{http://owncloud.org/ns}mentionId'; public const PROPERTY_NAME_MENTION_DISPLAYNAME = '{http://owncloud.org/ns}mentionDisplayName'; - /** @var IComment */ - public $comment; - - /** @var ICommentsManager */ - protected $commentsManager; - - /** @var ILogger */ - protected $logger; - /** @var array list of properties with key being their name and value their setter */ protected $properties = []; - /** @var IUserManager */ - protected $userManager; - - /** @var IUserSession */ - protected $userSession; - /** * CommentNode constructor. - * - * @param ICommentsManager $commentsManager - * @param IComment $comment - * @param IUserManager $userManager - * @param IUserSession $userSession - * @param ILogger $logger */ public function __construct( - ICommentsManager $commentsManager, - IComment $comment, - IUserManager $userManager, - IUserSession $userSession, - ILogger $logger + protected ICommentsManager $commentsManager, + public IComment $comment, + protected IUserManager $userManager, + protected IUserSession $userSession, + protected LoggerInterface $logger, ) { - $this->commentsManager = $commentsManager; - $this->comment = $comment; - $this->logger = $logger; - $methods = get_class_methods($this->comment); $methods = array_filter($methods, function ($name) { - return strpos($name, 'get') === 0; + return str_starts_with($name, 'get'); }); foreach ($methods as $getter) { if ($getter === 'getMentions') { continue; // special treatment } - $name = '{'.self::NS_OWNCLOUD.'}' . lcfirst(substr($getter, 3)); + $name = '{' . self::NS_OWNCLOUD . '}' . lcfirst(substr($getter, 3)); $this->properties[$name] = $getter; } - $this->userManager = $userManager; - $this->userSession = $userSession; } /** @@ -172,10 +129,8 @@ class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties { /** * Returns the last modification time, as a unix timestamp - * - * @return int */ - public function getLastModified() { + public function getLastModified(): ?int { return null; } @@ -194,7 +149,7 @@ class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties { $this->commentsManager->save($this->comment); return true; } catch (\Exception $e) { - $this->logger->logException($e, ['app' => 'dav/comments']); + $this->logger->error($e->getMessage(), ['app' => 'dav/comments', 'exception' => $e]); if ($e instanceof MessageTooLongException) { $msg = 'Message exceeds allowed character limit of '; throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e); @@ -287,7 +242,7 @@ class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties { try { $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); } catch (\OutOfBoundsException $e) { - $this->logger->logException($e); + $this->logger->error($e->getMessage(), ['exception' => $e]); // No displayname, upon client's discretion what to display. $displayName = ''; } diff --git a/apps/dav/lib/Comments/CommentsPlugin.php b/apps/dav/lib/Comments/CommentsPlugin.php index a4932751897..2ab7d6ee018 100644 --- a/apps/dav/lib/Comments/CommentsPlugin.php +++ b/apps/dav/lib/Comments/CommentsPlugin.php @@ -1,32 +1,16 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * 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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\DAV\Comments; +use OCP\AppFramework\Http; use OCP\Comments\IComment; use OCP\Comments\ICommentsManager; +use OCP\Comments\MessageTooLongException; use OCP\IUserSession; use Sabre\DAV\Exception\BadRequest; use Sabre\DAV\Exception\NotFound; @@ -52,24 +36,19 @@ class CommentsPlugin extends ServerPlugin { public const REPORT_PARAM_OFFSET = '{http://owncloud.org/ns}offset'; public const REPORT_PARAM_TIMESTAMP = '{http://owncloud.org/ns}datetime'; - /** @var ICommentsManager */ - protected $commentsManager; - /** @var \Sabre\DAV\Server $server */ private $server; - /** @var \OCP\IUserSession */ - protected $userSession; - /** * Comments plugin * * @param ICommentsManager $commentsManager * @param IUserSession $userSession */ - public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) { - $this->commentsManager = $commentsManager; - $this->userSession = $userSession; + public function __construct( + protected ICommentsManager $commentsManager, + protected IUserSession $userSession, + ) { } /** @@ -85,13 +64,13 @@ class CommentsPlugin extends ServerPlugin { */ public function initialize(Server $server) { $this->server = $server; - if (strpos($this->server->getRequestUri(), 'comments/') !== 0) { + if (!str_starts_with($this->server->getRequestUri(), 'comments/')) { return; } $this->server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; - $this->server->xml->classMap['DateTime'] = function (Writer $writer, \DateTime $value) { + $this->server->xml->classMap['DateTime'] = function (Writer $writer, \DateTime $value): void { $writer->write(\Sabre\HTTP\toDate($value)); }; @@ -130,7 +109,7 @@ class CommentsPlugin extends ServerPlugin { $response->setHeader('Content-Location', $url); // created - $response->setStatus(201); + $response->setStatus(Http::STATUS_CREATED); return false; } @@ -176,7 +155,7 @@ class CommentsPlugin extends ServerPlugin { } if (!is_null($args['datetime'])) { - $args['datetime'] = new \DateTime($args['datetime']); + $args['datetime'] = new \DateTime((string)$args['datetime']); } $results = $node->findChildren($args['limit'], $args['offset'], $args['datetime']); @@ -189,7 +168,7 @@ class CommentsPlugin extends ServerPlugin { $responses[] = new Response( $this->server->getBaseUri() . $nodePath, [200 => $resultSet[0][200]], - 200 + '200' ); } } @@ -199,7 +178,7 @@ class CommentsPlugin extends ServerPlugin { new MultiStatus($responses) ); - $this->server->httpResponse->setStatus(207); + $this->server->httpResponse->setStatus(Http::STATUS_MULTI_STATUS); $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); $this->server->httpResponse->setBody($xml); @@ -220,7 +199,7 @@ class CommentsPlugin extends ServerPlugin { */ private function createComment($objectType, $objectId, $data, $contentType = 'application/json') { if (explode(';', $contentType)[0] === 'application/json') { - $data = json_decode($data, true); + $data = json_decode($data, true, 512, JSON_THROW_ON_ERROR); } else { throw new UnsupportedMediaType(); } @@ -234,7 +213,7 @@ class CommentsPlugin extends ServerPlugin { } } if (is_null($actorId)) { - throw new BadRequest('Invalid actor "' . $actorType .'"'); + throw new BadRequest('Invalid actor "' . $actorType . '"'); } try { @@ -245,9 +224,9 @@ class CommentsPlugin extends ServerPlugin { return $comment; } catch (\InvalidArgumentException $e) { throw new BadRequest('Invalid input values', 0, $e); - } catch (\OCP\Comments\MessageTooLongException $e) { + } catch (MessageTooLongException $e) { $msg = 'Message exceeds allowed character limit of '; - throw new BadRequest($msg . \OCP\Comments\IComment::MAX_MESSAGE_LENGTH, 0, $e); + throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e); } } } diff --git a/apps/dav/lib/Comments/EntityCollection.php b/apps/dav/lib/Comments/EntityCollection.php index d9b06e1240c..33c58ee44d2 100644 --- a/apps/dav/lib/Comments/EntityCollection.php +++ b/apps/dav/lib/Comments/EntityCollection.php @@ -1,33 +1,17 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * 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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\DAV\Comments; use OCP\Comments\ICommentsManager; use OCP\Comments\NotFoundException; -use OCP\ILogger; use OCP\IUserManager; use OCP\IUserSession; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\IProperties; use Sabre\DAV\PropPatch; @@ -43,27 +27,21 @@ use Sabre\DAV\PropPatch; class EntityCollection extends RootCollection implements IProperties { public const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker'; - /** @var string */ - protected $id; - - /** @var ILogger */ - protected $logger; - /** * @param string $id * @param string $name * @param ICommentsManager $commentsManager * @param IUserManager $userManager * @param IUserSession $userSession - * @param ILogger $logger + * @param LoggerInterface $logger */ public function __construct( - $id, + protected $id, $name, ICommentsManager $commentsManager, IUserManager $userManager, IUserSession $userSession, - ILogger $logger + protected LoggerInterface $logger, ) { foreach (['id', 'name'] as $property) { $$property = trim($$property); @@ -71,10 +49,8 @@ class EntityCollection extends RootCollection implements IProperties { throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string'); } } - $this->id = $id; $this->name = $name; $this->commentsManager = $commentsManager; - $this->logger = $logger; $this->userManager = $userManager; $this->userSession = $userSession; } @@ -131,7 +107,7 @@ class EntityCollection extends RootCollection implements IProperties { * @param \DateTime|null $datetime * @return CommentNode[] */ - public function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) { + public function findChildren($limit = 0, $offset = 0, ?\DateTime $datetime = null) { $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime); $result = []; foreach ($comments as $comment) { @@ -163,12 +139,9 @@ class EntityCollection extends RootCollection implements IProperties { /** * Sets the read marker to the specified date for the logged in user - * - * @param \DateTime $value - * @return bool */ - public function setReadMarker($value) { - $dateTime = new \DateTime($value); + public function setReadMarker(?string $value): bool { + $dateTime = new \DateTime($value ?? 'now'); $user = $this->userSession->getUser(); $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user); return true; diff --git a/apps/dav/lib/Comments/EntityTypeCollection.php b/apps/dav/lib/Comments/EntityTypeCollection.php index c9df2a068d7..1c8533ca375 100644 --- a/apps/dav/lib/Comments/EntityTypeCollection.php +++ b/apps/dav/lib/Comments/EntityTypeCollection.php @@ -1,32 +1,16 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * 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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\DAV\Comments; use OCP\Comments\ICommentsManager; -use OCP\ILogger; use OCP\IUserManager; use OCP\IUserSession; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\MethodNotAllowed; use Sabre\DAV\Exception\NotFound; @@ -42,42 +26,21 @@ use Sabre\DAV\Exception\NotFound; * @package OCA\DAV\Comments */ class EntityTypeCollection extends RootCollection { - - /** @var ILogger */ - protected $logger; - - /** @var IUserManager */ - protected $userManager; - - /** @var \Closure */ - protected $childExistsFunction; - - /** - * @param string $name - * @param ICommentsManager $commentsManager - * @param IUserManager $userManager - * @param IUserSession $userSession - * @param ILogger $logger - * @param \Closure $childExistsFunction - */ public function __construct( - $name, + string $name, ICommentsManager $commentsManager, - IUserManager $userManager, + protected IUserManager $userManager, IUserSession $userSession, - ILogger $logger, - \Closure $childExistsFunction + protected LoggerInterface $logger, + protected \Closure $childExistsFunction, ) { $name = trim($name); - if (empty($name) || !is_string($name)) { + if (empty($name)) { throw new \InvalidArgumentException('"name" parameter must be non-empty string'); } $this->name = $name; $this->commentsManager = $commentsManager; - $this->logger = $logger; - $this->userManager = $userManager; $this->userSession = $userSession; - $this->childExistsFunction = $childExistsFunction; } /** diff --git a/apps/dav/lib/Comments/RootCollection.php b/apps/dav/lib/Comments/RootCollection.php index e8e890696eb..493d73ec531 100644 --- a/apps/dav/lib/Comments/RootCollection.php +++ b/apps/dav/lib/Comments/RootCollection.php @@ -1,81 +1,35 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * 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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\DAV\Comments; use OCP\Comments\CommentsEntityEvent; use OCP\Comments\ICommentsManager; -use OCP\ILogger; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IUserManager; use OCP\IUserSession; +use Psr\Log\LoggerInterface; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotAuthenticated; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\ICollection; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class RootCollection implements ICollection { - /** @var EntityTypeCollection[]|null */ - private $entityTypeCollections; - - /** @var ICommentsManager */ - protected $commentsManager; - - /** @var string */ - protected $name = 'comments'; - - /** @var ILogger */ - protected $logger; + private ?array $entityTypeCollections = null; + protected string $name = 'comments'; - /** @var IUserManager */ - protected $userManager; - - /** @var IUserSession */ - protected $userSession; - - /** @var EventDispatcherInterface */ - protected $dispatcher; - - /** - * @param ICommentsManager $commentsManager - * @param IUserManager $userManager - * @param IUserSession $userSession - * @param EventDispatcherInterface $dispatcher - * @param ILogger $logger - */ public function __construct( - ICommentsManager $commentsManager, - IUserManager $userManager, - IUserSession $userSession, - EventDispatcherInterface $dispatcher, - ILogger $logger) { - $this->commentsManager = $commentsManager; - $this->logger = $logger; - $this->userManager = $userManager; - $this->userSession = $userSession; - $this->dispatcher = $dispatcher; + protected ICommentsManager $commentsManager, + protected IUserManager $userManager, + protected IUserSession $userSession, + protected IEventDispatcher $dispatcher, + protected LoggerInterface $logger, + ) { } /** @@ -94,7 +48,8 @@ class RootCollection implements ICollection { throw new NotAuthenticated(); } - $event = new CommentsEntityEvent(CommentsEntityEvent::EVENT_ENTITY); + $event = new CommentsEntityEvent(); + $this->dispatcher->dispatchTyped($event); $this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event); $this->entityTypeCollections = []; @@ -157,6 +112,7 @@ class RootCollection implements ICollection { */ public function getChildren() { $this->initCollections(); + assert(!is_null($this->entityTypeCollections)); return $this->entityTypeCollections; } @@ -168,6 +124,7 @@ class RootCollection implements ICollection { */ public function childExists($name) { $this->initCollections(); + assert(!is_null($this->entityTypeCollections)); return isset($this->entityTypeCollections[$name]); } @@ -204,7 +161,7 @@ class RootCollection implements ICollection { /** * Returns the last modification time, as a unix timestamp * - * @return int + * @return ?int */ public function getLastModified() { return null; |