memories/lib/Db/TimelineQueryAlbums.php

130 lines
4.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)
{
2022-10-27 07:11:35 +00:00
// Get album object
$album = $this->getAlbumIfAllowed($query->getConnection(), $uid, $albumId);
// Check permission
2022-10-27 07:53:44 +00:00
if (null === $album) {
2022-10-26 23:20:28 +00:00
throw new \Exception("Album {$albumId} not found");
}
// WHERE these are items with this album
$query->innerJoin('m', 'photos_albums_files', 'paf', $query->expr()->andX(
2022-10-27 07:11:35 +00:00
$query->expr()->eq('paf.album_id', $query->createNamedParameter($album['album_id'])),
2022-10-26 23:20:28 +00:00
$query->expr()->eq('paf.file_id', 'm.fileid'),
));
}
/** Get list of albums */
2022-10-27 09:36:20 +00:00
public function getAlbums(string $uid, $shared=false)
2022-10-26 22:12:46 +00:00
{
$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');
2022-10-27 09:36:20 +00:00
$query->select('pa.*', $count)->from('photos_albums', 'pa');
if ($shared) {
$query->innerJoin('pa', 'photos_collaborators', 'pc', $query->expr()->andX(
$query->expr()->eq('pa.album_id', 'pc.album_id'),
$query->expr()->eq('pc.collaborator_id', $query->createNamedParameter($uid)),
));
} else {
$query->where(
$query->expr()->eq('user', $query->createNamedParameter($uid)),
);
}
2022-10-26 22:12:46 +00:00
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
2022-10-27 07:11:35 +00:00
/**
* Get album if allowed. Also check if album is shared with user.
*
* @param IDBConnection $connection
2022-10-27 07:53:44 +00:00
* @param string $uid UID of CURRENT user
* @param string $albumId $user/$name where $user is the OWNER of the album
2022-10-27 07:11:35 +00:00
*/
private function getAlbumIfAllowed(IDBConnection $conn, string $uid, string $albumId)
2022-10-26 23:20:28 +00:00
{
2022-10-27 07:11:35 +00:00
// Split name and uid
$parts = explode('/', $albumId);
2022-10-27 07:53:44 +00:00
if (2 !== \count($parts)) {
2022-10-27 07:11:35 +00:00
return null;
}
$albumUid = $parts[0];
$albumName = $parts[1];
2022-10-26 23:20:28 +00:00
// Check if owner
$query = $conn->getQueryBuilder();
2022-10-27 07:11:35 +00:00
$query->select('*')->from('photos_albums')->where(
2022-10-26 23:20:28 +00:00
$query->expr()->andX(
2022-10-27 07:11:35 +00:00
$query->expr()->eq('name', $query->createNamedParameter($albumName)),
$query->expr()->eq('user', $query->createNamedParameter($albumUid)),
2022-10-26 23:20:28 +00:00
)
);
2022-10-27 07:11:35 +00:00
$album = $query->executeQuery()->fetch();
if (!$album) {
return null;
2022-10-26 23:20:28 +00:00
}
2022-10-27 07:11:35 +00:00
// Check if user is owner
if ($albumUid === $uid) {
return $album;
}
// Check in collaborators instead
2022-10-26 23:20:28 +00:00
$query = $conn->getQueryBuilder();
$query->select('album_id')->from('photos_collaborators')->where(
$query->expr()->andX(
2022-10-27 07:11:35 +00:00
$query->expr()->eq('album_id', $query->createNamedParameter($album['album_id'])),
2022-10-26 23:20:28 +00:00
$query->expr()->eq('collaborator_id', $query->createNamedParameter($uid)),
)
);
2022-10-27 07:11:35 +00:00
if (false !== $query->executeQuery()->fetchOne()) {
return $album;
}
2022-10-26 23:20:28 +00:00
}
2022-10-26 22:12:46 +00:00
}