]> source.dussan.org Git - nextcloud-server.git/commitdiff
Make sure getUsersFavoritingObject can be run without a user 23256/head
authorJoas Schilling <coding@schilljs.com>
Mon, 5 Oct 2020 12:57:13 +0000 (14:57 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Wed, 7 Oct 2020 11:10:38 +0000 (11:10 +0000)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/TagManager.php
lib/private/Tags.php
tests/lib/TagsTest.php

index 96786c58a1a9ac9cc2acffb4c8839ccb5ab04552..4613b6247f4fbfca4b385fd595c2672c18c8c083 100644 (file)
 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
-        */
+       /** @var TagMapper */
+       private $mapper;
+
+       /** @var IUserSession */
        private $userSession;
 
-       /**
-        * TagMapper
-        *
-        * @var TagMapper
-        */
-       private $mapper;
+       /** @var IDBConnection */
+       private $connection;
 
-       /**
-        * 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) {
+       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);
+       }
 }
index 22c6c68ba6c5c04f2b4f6fe673b10da49d4c0d1f..3fc66c69d6cc236fd82a6e566c8cb9f4b10893c0 100644 (file)
@@ -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.
         *
index 69c2833433d9ceb2591535c7a8bbd2b85e95effd..f6acc6624242d5fa764a9dd5298a53f0095e8709 100644 (file)
@@ -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));
        }