memories/lib/Db/TimelineQueryAlbums.php

180 lines
5.8 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-11-24 02:28:34 +00:00
public function getAlbums(string $uid, bool $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) {
2022-11-29 16:57:03 +00:00
$query->innerJoin('pa', $this->collaboratorsTable(), 'pc', $query->expr()->andX(
2022-10-27 09:36:20 +00:00
$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-11-03 22:39:48 +00:00
/**
* Convert days response to months response.
* The dayId is used to group the days into months.
*/
public function daysToMonths(array &$days)
{
$months = [];
foreach ($days as &$day) {
$dayId = $day['dayid'];
$time = $dayId * 86400;
$monthid = strtotime(date('Ym', $time).'01') / 86400;
if (empty($months) || $months[\count($months) - 1]['dayid'] !== $monthid) {
$months[] = [
'dayid' => $monthid,
'count' => 0,
];
}
$months[\count($months) - 1]['count'] += $day['count'];
}
return $months;
}
/** Convert list of month IDs to list of dayIds */
public function monthIdToDayIds(int $monthId)
{
$dayIds = [];
$firstDay = (int) $monthId;
$lastDay = strtotime(date('Ymt', $firstDay * 86400)) / 86400;
for ($i = $firstDay; $i <= $lastDay; ++$i) {
$dayIds[] = (string) $i;
}
return $dayIds;
}
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();
2022-11-29 16:57:03 +00:00
$query->select('album_id')->from($this->collaboratorsTable())->where(
2022-10-26 23:20:28 +00:00
$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-11-29 16:57:03 +00:00
/** Get the name of the collaborators table */
private function collaboratorsTable() {
// https://github.com/nextcloud/photos/commit/20e3e61ad577014e5f092a292c90a8476f630355
$appManager = \OC::$server->getAppManager();
$photosVersion = $appManager->getAppVersion('photos');
if (version_compare($photosVersion, '2.2.0', '>=')) {
return 'photos_albums_collabs';
}
return 'photos_collaborators';
}
2022-10-26 22:12:46 +00:00
}