memories/lib/Db/TimelineQueryAlbums.php

94 lines
3.3 KiB
PHP
Raw Normal View History

2022-10-26 22:12:46 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
2022-10-26 23:20:28 +00:00
use OCP\DB\QueryBuilder\IQueryBuilder;
2022-10-26 22:12:46 +00:00
use OCP\IDBConnection;
trait TimelineQueryAlbums
{
protected IDBConnection $connection;
2022-10-26 23:20:28 +00:00
/** Transform only for album */
public function transformAlbumFilter(IQueryBuilder &$query, string $uid, string $albumId)
{
if (!$this->hasAlbumPermission($query->getConnection(), $uid, (int) $albumId)) {
throw new \Exception("Album {$albumId} not found");
}
// WHERE these are items with this album
$query->innerJoin('m', 'photos_albums_files', 'paf', $query->expr()->andX(
$query->expr()->eq('paf.album_id', $query->createNamedParameter($albumId)),
$query->expr()->eq('paf.file_id', 'm.fileid'),
));
}
/** Get list of albums */
2022-10-26 22:12:46 +00:00
public function getAlbums(string $uid)
{
$query = $this->connection->getQueryBuilder();
// SELECT everything from albums
2022-10-26 22:48:46 +00:00
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
$query->select('pa.*', $count)->from('photos_albums', 'pa')->where(
2022-10-26 22:12:46 +00:00
$query->expr()->eq('user', $query->createNamedParameter($uid)),
);
2022-10-26 23:20:28 +00:00
// WHERE these are items with this album
2022-10-27 06:37:56 +00:00
$query->leftJoin('pa', 'photos_albums_files', 'paf', $query->expr()->andX(
2022-10-26 22:48:46 +00:00
$query->expr()->eq('paf.album_id', 'pa.album_id'),
));
// WHERE these items are memories indexed photos
2022-10-27 06:37:56 +00:00
$query->leftJoin('paf', 'memories', 'm', $query->expr()->eq('m.fileid', 'paf.file_id'));
2022-10-26 22:48:46 +00:00
// WHERE these photos are in the filecache
2022-10-27 06:37:56 +00:00
$query->leftJoin('m', 'filecache', 'f', $query->expr()->eq('m.fileid', 'f.fileid'));
2022-10-26 22:48:46 +00:00
2022-10-26 22:12:46 +00:00
// GROUP and ORDER by
2022-10-26 22:48:46 +00:00
$query->groupBy('pa.album_id');
2022-10-26 22:12:46 +00:00
$query->orderBy('pa.created', 'DESC');
$query->addOrderBy('pa.album_id', 'DESC'); // tie-breaker
// FETCH all albums
$albums = $query->executeQuery()->fetchAll();
// Post process
foreach ($albums as &$row) {
2022-10-26 22:48:46 +00:00
$row['album_id'] = (int) $row['album_id'];
$row['created'] = (int) $row['created'];
2022-10-26 22:12:46 +00:00
$row['last_added_photo'] = (int) $row['last_added_photo'];
}
return $albums;
}
2022-10-26 23:20:28 +00:00
private function hasAlbumPermission(IDBConnection $conn, string $uid, int $albumId)
{
// Check if owner
$query = $conn->getQueryBuilder();
$query->select('album_id')->from('photos_albums')->where(
$query->expr()->andX(
$query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)),
$query->expr()->eq('user', $query->createNamedParameter($uid)),
)
);
if (false !== $query->executeQuery()->fetchOne()) {
return true;
}
// Check in collaborators
$query = $conn->getQueryBuilder();
$query->select('album_id')->from('photos_collaborators')->where(
$query->expr()->andX(
$query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)),
$query->expr()->eq('collaborator_id', $query->createNamedParameter($uid)),
)
);
return false !== $query->executeQuery()->fetchOne();
}
2022-10-26 22:12:46 +00:00
}