diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Comments/Comment.php | 31 | ||||
-rw-r--r-- | lib/private/Comments/Manager.php | 84 | ||||
-rw-r--r-- | lib/private/DB/MissingColumnInformation.php | 43 | ||||
-rw-r--r-- | lib/private/legacy/helper.php | 20 | ||||
-rw-r--r-- | lib/private/legacy/util.php | 8 |
5 files changed, 147 insertions, 39 deletions
diff --git a/lib/private/Comments/Comment.php b/lib/private/Comments/Comment.php index 71687c7a609..3b4a523b884 100644 --- a/lib/private/Comments/Comment.php +++ b/lib/private/Comments/Comment.php @@ -42,6 +42,7 @@ class Comment implements IComment { 'actorId' => '', 'objectType' => '', 'objectId' => '', + 'referenceId' => null, 'creationDT' => null, 'latestChildDT' => null, ]; @@ -396,6 +397,36 @@ class Comment implements IComment { } /** + * returns the reference id of the comment + * + * @return string|null + * @since 19.0.0 + */ + public function getReferenceId(): ?string { + return $this->data['referenceId']; + } + + /** + * sets (overwrites) the reference id of the comment + * + * @param string $referenceId e.g. sha256 hash sum + * @return IComment + * @since 19.0.0 + */ + public function setReferenceId(?string $referenceId): IComment { + if ($referenceId === null) { + $this->data['referenceId'] = $referenceId; + } else { + $referenceId = trim($referenceId); + if ($referenceId === '') { + throw new \InvalidArgumentException('Non empty string expected.'); + } + $this->data['referenceId'] = $referenceId; + } + return $this; + } + + /** * sets the comment data based on an array with keys as taken from the * database. * diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index f1c72243597..d04f3f965b3 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -29,6 +29,7 @@ namespace OC\Comments; use Doctrine\DBAL\Exception\DriverException; +use Doctrine\DBAL\Exception\InvalidFieldNameException; use OCP\Comments\CommentsEvent; use OCP\Comments\IComment; use OCP\Comments\ICommentsEventHandler; @@ -96,6 +97,7 @@ class Manager implements ICommentsManager { $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); } $data['children_count'] = (int)$data['children_count']; + $data['reference_id'] = $data['reference_id'] ?? null; return $data; } @@ -744,23 +746,46 @@ class Manager implements ICommentsManager { * @param IComment $comment * @return bool */ - protected function insert(IComment &$comment) { + protected function insert(IComment $comment): bool { + + try { + $result = $this->insertQuery($comment, true); + } catch (InvalidFieldNameException $e) { + // The reference id field was only added in Nextcloud 19. + // In order to not cause too long waiting times on the update, + // it was decided to only add it lazy, as it is also not a critical + // feature, but only helps to have a better experience while commenting. + // So in case the reference_id field is missing, + // we simply save the comment without that field. + $result = $this->insertQuery($comment, false); + } + + return $result; + } + + protected function insertQuery(IComment $comment, bool $tryWritingReferenceId): bool { $qb = $this->dbConn->getQueryBuilder(); - $affectedRows = $qb - ->insert('comments') - ->values([ - 'parent_id' => $qb->createNamedParameter($comment->getParentId()), - 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), - 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), - 'actor_type' => $qb->createNamedParameter($comment->getActorType()), - 'actor_id' => $qb->createNamedParameter($comment->getActorId()), - 'message' => $qb->createNamedParameter($comment->getMessage()), - 'verb' => $qb->createNamedParameter($comment->getVerb()), - 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), - 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), - 'object_type' => $qb->createNamedParameter($comment->getObjectType()), - 'object_id' => $qb->createNamedParameter($comment->getObjectId()), - ]) + + $values = [ + 'parent_id' => $qb->createNamedParameter($comment->getParentId()), + 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), + 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), + 'actor_type' => $qb->createNamedParameter($comment->getActorType()), + 'actor_id' => $qb->createNamedParameter($comment->getActorId()), + 'message' => $qb->createNamedParameter($comment->getMessage()), + 'verb' => $qb->createNamedParameter($comment->getVerb()), + 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), + 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), + 'object_type' => $qb->createNamedParameter($comment->getObjectType()), + 'object_id' => $qb->createNamedParameter($comment->getObjectId()), + ]; + + if ($tryWritingReferenceId) { + $values['reference_id'] = $qb->createNamedParameter($comment->getReferenceId()); + } + + $affectedRows = $qb->insert('comments') + ->values($values) ->execute(); if ($affectedRows > 0) { @@ -785,8 +810,21 @@ class Manager implements ICommentsManager { $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); $this->uncache($comment->getId()); + try { + $result = $this->updateQuery($comment, true); + } catch (InvalidFieldNameException $e) { + // See function insert() for explanation + $result = $this->updateQuery($comment, false); + } + + $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); + + return $result; + } + + protected function updateQuery(IComment $comment, bool $tryWritingReferenceId): bool { $qb = $this->dbConn->getQueryBuilder(); - $affectedRows = $qb + $qb ->update('comments') ->set('parent_id', $qb->createNamedParameter($comment->getParentId())) ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) @@ -798,17 +836,19 @@ class Manager implements ICommentsManager { ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) ->set('object_type', $qb->createNamedParameter($comment->getObjectType())) - ->set('object_id', $qb->createNamedParameter($comment->getObjectId())) - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) - ->setParameter('id', $comment->getId()) + ->set('object_id', $qb->createNamedParameter($comment->getObjectId())); + + if ($tryWritingReferenceId) { + $qb->set('reference_id', $qb->createNamedParameter($comment->getReferenceId())); + } + + $affectedRows = $qb->where($qb->expr()->eq('id', $qb->createNamedParameter($comment->getId()))) ->execute(); if ($affectedRows === 0) { throw new NotFoundException('Comment to update does ceased to exist'); } - $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); - return $affectedRows > 0; } diff --git a/lib/private/DB/MissingColumnInformation.php b/lib/private/DB/MissingColumnInformation.php new file mode 100644 index 00000000000..bcf585848fd --- /dev/null +++ b/lib/private/DB/MissingColumnInformation.php @@ -0,0 +1,43 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\DB; + +class MissingColumnInformation { + + private $listOfMissingColumns = []; + + public function addHintForMissingColumn(string $tableName, string $columnName): void { + $this->listOfMissingColumns[] = [ + 'tableName' => $tableName, + 'columnName' => $columnName, + ]; + } + + public function getListOfMissingColumns(): array { + return $this->listOfMissingColumns; + } +} diff --git a/lib/private/legacy/helper.php b/lib/private/legacy/helper.php index 67d70e10bcf..626295af5a9 100644 --- a/lib/private/legacy/helper.php +++ b/lib/private/legacy/helper.php @@ -43,6 +43,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/> * */ + +use OCP\IUser; use Symfony\Component\Process\ExecutableFinder; /** @@ -504,19 +506,14 @@ class OC_Helper { || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') ) { /** @var \OC\Files\Storage\Home $storage */ - $userInstance = $storage->getUser(); - $user = ($userInstance === null) ? null : $userInstance->getUID(); - } else { - $user = \OC::$server->getUserSession()->getUser()->getUID(); - } - if ($user) { - $quota = OC_Util::getUserQuota($user); + $user = $storage->getUser(); } else { - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; + $user = \OC::$server->getUserSession()->getUser(); } + $quota = OC_Util::getUserQuota($user); if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { // always get free space / total space from root + mount points - return self::getGlobalStorageInfo(); + return self::getGlobalStorageInfo($quota); } } @@ -562,11 +559,10 @@ class OC_Helper { /** * Get storage info including all mount points and quota * + * @param int $quota * @return array */ - private static function getGlobalStorageInfo() { - $quota = OC_Util::getUserQuota(\OCP\User::getUser()); - + private static function getGlobalStorageInfo($quota) { $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); $used = $rootInfo['size']; if ($used < 0) { diff --git a/lib/private/legacy/util.php b/lib/private/legacy/util.php index 9fce2993a2a..f14095675dc 100644 --- a/lib/private/legacy/util.php +++ b/lib/private/legacy/util.php @@ -251,8 +251,7 @@ class OC_Util { ) { /** @var \OC\Files\Storage\Home $storage */ if (is_object($storage->getUser())) { - $user = $storage->getUser()->getUID(); - $quota = OC_Util::getUserQuota($user); + $quota = OC_Util::getUserQuota($storage->getUser()); if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { return new \OC\Files\Storage\Wrapper\Quota(['storage' => $storage, 'quota' => $quota, 'root' => 'files']); } @@ -375,11 +374,10 @@ class OC_Util { /** * Get the quota of a user * - * @param string $userId + * @param IUser|null $user * @return float Quota bytes */ - public static function getUserQuota($userId) { - $user = \OC::$server->getUserManager()->get($userId); + public static function getUserQuota(?IUser $user) { if (is_null($user)) { return \OCP\Files\FileInfo::SPACE_UNLIMITED; } |