diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index fc7d925f..55f42985 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -78,7 +78,13 @@ class ApiController extends Controller { // Filter only videos if ($this->request->getParam('vid')) { - $transforms[] = array($this->timelineQuery, 'videoFilter'); + $transforms[] = array($this->timelineQuery, 'transformVideoFilter'); + } + + // Filter only for one tag + $tagName = $this->request->getParam('tag'); + if ($tagName) { + $transforms[] = array($this->timelineQuery, 'transformTagFilter', $tagName); } return $transforms; diff --git a/lib/Db/TimelineQueryDays.php b/lib/Db/TimelineQueryDays.php index bf698aec..b5e94d44 100644 --- a/lib/Db/TimelineQueryDays.php +++ b/lib/Db/TimelineQueryDays.php @@ -126,9 +126,7 @@ trait TimelineQueryDays { ->orderBy('m.dayid', 'DESC'); // Apply all transformations - foreach ($queryTransforms as &$transform) { - $transform($query, $uid); - } + $this->applyAllTransforms($queryTransforms, $query, $uid); $cursor = $query->executeQuery(); $rows = $cursor->fetchAll(); @@ -174,9 +172,7 @@ trait TimelineQueryDays { $query->orderBy('m.datetaken', 'DESC'); // Apply all transformations - foreach ($queryTransforms as &$transform) { - $transform($query, $uid); - } + $this->applyAllTransforms($queryTransforms, $query, $uid); $cursor = $query->executeQuery(); $rows = $cursor->fetchAll(); diff --git a/lib/Db/TimelineQueryFilters.php b/lib/Db/TimelineQueryFilters.php index 44e5e670..86e3ece4 100644 --- a/lib/Db/TimelineQueryFilters.php +++ b/lib/Db/TimelineQueryFilters.php @@ -7,14 +7,24 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\ITags; trait TimelineQueryFilters { - public function transformFavoriteFilter(IQueryBuilder $query, string $userId) { + private function applyAllTransforms(array $transforms, IQueryBuilder &$query, string $uid): void { + foreach ($transforms as &$transform) { + $fun = array_slice($transform, 0, 2); + $params = array_slice($transform, 2); + array_unshift($params, $uid); + array_unshift($params, $query); + $fun(...$params); + } + } + + public function transformFavoriteFilter(IQueryBuilder &$query, string $userId) { $query->innerJoin('m', 'vcategory_to_object', 'vcoi', $query->expr()->andX( $query->expr()->eq('vcoi.objid', 'm.fileid'), $query->expr()->in('vcoi.categoryid', $this->getFavoriteVCategoryFun($query, $userId)), )); } - public function addFavoriteTag(IQueryBuilder $query, string $userId) { + public function addFavoriteTag(IQueryBuilder &$query, string $userId) { $query->leftJoin('m', 'vcategory_to_object', 'vco', $query->expr()->andX( $query->expr()->eq('vco.objid', 'm.fileid'), $query->expr()->in('vco.categoryid', $this->getFavoriteVCategoryFun($query, $userId)), @@ -31,7 +41,29 @@ trait TimelineQueryFilters { ))->getSQL()); } - public function videoFilter(IQueryBuilder $query, string $userId) { + public function transformVideoFilter(IQueryBuilder &$query, string $userId) { $query->andWhere($query->expr()->eq('m.isvideo', $query->createNamedParameter('1'))); } + + public function getSystemTagId(IQueryBuilder &$query, string $tagName) { + $sqb = $query->getConnection()->getQueryBuilder(); + return $sqb->select('id')->from('systemtag')->where( + $sqb->expr()->andX( + $sqb->expr()->eq('name', $sqb->createNamedParameter($tagName)), + $sqb->expr()->eq('visibility', $sqb->createNamedParameter(1)), + ))->executeQuery()->fetchOne(); + } + + public function transformTagFilter(IQueryBuilder &$query, string $userId, string $tagName) { + $tagId = $this->getSystemTagId($query, $tagName); + if ($tagId === FALSE) { + $tagId = 0; // cannot abort here; that will show up everything in the response + } + + $query->innerJoin('m', 'systemtag_object_mapping', 'stom', $query->expr()->andX( + $query->expr()->eq('stom.objecttype', $query->createNamedParameter("files")), + $query->expr()->eq('stom.objectid', 'm.fileid'), + $query->expr()->eq('stom.systemtagid', $query->createNamedParameter($tagId)), + )); + } } \ No newline at end of file