From d68d62f052d7ca0f0c0ee73dc7b3534b627e17d3 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 5 Oct 2020 14:57:13 +0200 Subject: [PATCH] Make sure getUsersFavoritingObject can be run without a user Signed-off-by: Joas Schilling --- lib/private/TagManager.php | 54 ++++++++++++++++++++++++-------------- lib/private/Tags.php | 17 ------------ tests/lib/TagsTest.php | 4 +-- 3 files changed, 36 insertions(+), 39 deletions(-) diff --git a/lib/private/TagManager.php b/lib/private/TagManager.php index 96786c58a1..4613b6247f 100644 --- a/lib/private/TagManager.php +++ b/lib/private/TagManager.php @@ -38,32 +38,27 @@ namespace OC; use OC\Tagging\TagMapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\ITagManager; +use OCP\ITags; +use OCP\IUserSession; -class TagManager implements \OCP\ITagManager { +class TagManager implements ITagManager { - /** - * User session - * - * @var \OCP\IUserSession - */ - private $userSession; - - /** - * TagMapper - * - * @var TagMapper - */ + /** @var TagMapper */ private $mapper; - /** - * Constructor. - * - * @param TagMapper $mapper Instance of the TagMapper abstraction layer. - * @param \OCP\IUserSession $userSession the user session - */ - public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) { + /** @var IUserSession */ + private $userSession; + + /** @var IDBConnection */ + private $connection; + + public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection) { $this->mapper = $mapper; $this->userSession = $userSession; + $this->connection = $connection; } /** @@ -90,4 +85,23 @@ class TagManager implements \OCP\ITagManager { } return new Tags($this->mapper, $userId, $type, $defaultTags); } + + /** + * Get all users who favorited an object + * + * @param string $objectType + * @param int $objectId + * @return array + */ + public function getUsersFavoritingObject(string $objectType, int $objectId): array { + $query = $this->connection->getQueryBuilder(); + $query->select('uid') + ->from('vcategory_to_object', 'o') + ->innerJoin('o', 'vcategory', 'c', $query->expr()->eq('o.categoryid', 'c.id')) + ->where($query->expr()->eq('objid', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('c.type', $query->createNamedParameter($objectType))) + ->andWhere($query->expr()->eq('c.category', $query->createNamedParameter(ITags::TAG_FAVORITE))); + + return $query->execute()->fetchAll(\PDO::FETCH_COLUMN); + } } diff --git a/lib/private/Tags.php b/lib/private/Tags.php index 22c6c68ba6..3fc66c69d6 100644 --- a/lib/private/Tags.php +++ b/lib/private/Tags.php @@ -641,23 +641,6 @@ class Tags implements ITags { return $this->unTag($objid, ITags::TAG_FAVORITE); } - /** - * Get all users who favorited an object - */ - public function getUsersFavoritingObject($objId) { - $entries = []; - - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $query->select('uid') - ->from('vcategory_to_object', 'o') - ->innerJoin('o', 'vcategory', 'c', $query->expr()->eq('o.categoryid', 'c.id')) - ->where($query->expr()->eq('objid', $query->createNamedParameter($objId, IQueryBuilder::PARAM_INT))) - ->andWhere($query->expr()->eq('c.type', $query->createNamedParameter($this->type))) - ->andWhere($query->expr()->eq('c.category', $query->createNamedParameter(ITags::TAG_FAVORITE))); - - return $query->execute()->fetchAll(\PDO::FETCH_COLUMN); - } - /** * Creates a tag/object relation. * diff --git a/tests/lib/TagsTest.php b/tests/lib/TagsTest.php index 69c2833433..f6acc66242 100644 --- a/tests/lib/TagsTest.php +++ b/tests/lib/TagsTest.php @@ -61,7 +61,7 @@ class TagsTest extends \Test\TestCase { $this->objectType = $this->getUniqueID('type_'); $this->tagMapper = new \OC\Tagging\TagMapper(\OC::$server->getDatabaseConnection()); - $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession); + $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection()); } protected function tearDown(): void { @@ -78,7 +78,7 @@ class TagsTest extends \Test\TestCase { ->expects($this->any()) ->method('getUser') ->willReturn(null); - $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession); + $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection()); $this->assertNull($this->tagMgr->load($this->objectType)); }