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;
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
class AlbumsQuery
|
2022-10-26 22:12:46 +00:00
|
|
|
{
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
public function __construct(IDBConnection $connection)
|
2022-10-26 23:20:28 +00:00
|
|
|
{
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->connection = $connection;
|
2022-10-26 23:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get list of albums */
|
2023-07-24 19:35:25 +00:00
|
|
|
public function getList(string $uid, int $fileId, bool $shared = false)
|
2022-10-26 22:12:46 +00:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2023-07-24 19:35:25 +00:00
|
|
|
$allPhotosQuery = $this->connection->getQueryBuilder();
|
2022-10-26 22:12:46 +00:00
|
|
|
|
|
|
|
// SELECT everything from albums
|
2023-07-24 19:35:25 +00:00
|
|
|
$allPhotosQuery->select('album_id')->from('photos_albums_files');
|
|
|
|
$allPhotosQuery->where(
|
|
|
|
$allPhotosQuery->expr()->eq('file_id', $allPhotosQuery->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))
|
|
|
|
);
|
2022-10-26 22:48:46 +00:00
|
|
|
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
|
2023-07-06 20:18:59 +00:00
|
|
|
$query->select(
|
|
|
|
'pa.album_id',
|
|
|
|
'pa.name',
|
|
|
|
'pa.user',
|
|
|
|
'pa.created',
|
|
|
|
'pa.created',
|
|
|
|
'pa.location',
|
|
|
|
'pa.last_added_photo',
|
|
|
|
$count
|
|
|
|
)->from('photos_albums', 'pa');
|
2022-10-27 09:36:20 +00:00
|
|
|
|
|
|
|
if ($shared) {
|
2023-01-15 22:43:48 +00:00
|
|
|
$ids = $this->getSelfCollaborators($uid);
|
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'),
|
2023-01-15 22:43:48 +00:00
|
|
|
$query->expr()->in('pc.collaborator_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_STR_ARRAY)),
|
2022-10-27 09:36:20 +00:00
|
|
|
));
|
|
|
|
} 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();
|
2023-07-24 19:35:25 +00:00
|
|
|
$allPhotos = $allPhotosQuery->executeQuery()->fetchAll();
|
|
|
|
$albumIds = array();
|
|
|
|
|
|
|
|
foreach ($allPhotos as &$album) {
|
|
|
|
$albumIds[$album['album_id']] = true;
|
|
|
|
}
|
2022-10-26 22:12:46 +00:00
|
|
|
|
|
|
|
// Post process
|
|
|
|
foreach ($albums as &$row) {
|
2023-07-24 19:35:25 +00:00
|
|
|
$albumId = (int) $row['album_id'];
|
2023-03-25 01:09:18 +00:00
|
|
|
$row['cluster_id'] = $row['user'].'/'.$row['name'];
|
2023-07-24 19:35:25 +00:00
|
|
|
$row['album_id'] = $albumId;
|
2022-10-26 22:48:46 +00:00
|
|
|
$row['created'] = (int) $row['created'];
|
2022-10-26 22:12:46 +00:00
|
|
|
$row['last_added_photo'] = (int) $row['last_added_photo'];
|
2023-07-24 19:35:25 +00:00
|
|
|
$row['has_file'] = !!$albumIds[$albumId];
|
2022-10-26 22:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $albums;
|
|
|
|
}
|
2022-10-26 23:20:28 +00:00
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
/**
|
|
|
|
* Check if an album has a file.
|
|
|
|
*
|
|
|
|
* @return bool|string owner of file
|
|
|
|
*/
|
2023-03-23 23:58:49 +00:00
|
|
|
public function hasFile(int $albumId, int $fileId)
|
2023-01-18 03:02:00 +00:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('owner')->from('photos_albums_files')->where(
|
|
|
|
$query->expr()->andX(
|
|
|
|
$query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)),
|
|
|
|
$query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $query->executeQuery()->fetchOne();
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:07:06 +00:00
|
|
|
/**
|
|
|
|
* Check if a file belongs to a user through an album.
|
2022-12-03 05:42:52 +00:00
|
|
|
*
|
|
|
|
* @return bool|string owner of file
|
2022-12-03 04:07:06 +00:00
|
|
|
*/
|
2023-03-23 23:58:49 +00:00
|
|
|
public function userHasFile(string $uid, int $fileId)
|
2022-12-03 04:07:06 +00:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2022-12-03 05:42:52 +00:00
|
|
|
$query->select('paf.owner')->from('photos_albums_files', 'paf')->where(
|
2022-12-03 04:07:06 +00:00
|
|
|
$query->expr()->andX(
|
|
|
|
$query->expr()->eq('paf.file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)),
|
|
|
|
$query->expr()->orX(
|
|
|
|
$query->expr()->eq('pa.album_id', 'paf.album_id'),
|
|
|
|
$query->expr()->eq('pc.album_id', 'paf.album_id'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check if user-owned album or shared album
|
|
|
|
$query->leftJoin('paf', 'photos_albums', 'pa', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('pa.album_id', 'paf.album_id'),
|
|
|
|
$query->expr()->eq('pa.user', $query->createNamedParameter($uid)),
|
|
|
|
));
|
|
|
|
|
|
|
|
// Join to shared album
|
2023-01-15 22:43:48 +00:00
|
|
|
$ids = $this->getSelfCollaborators($uid);
|
2022-12-03 04:07:06 +00:00
|
|
|
$query->leftJoin('paf', $this->collaboratorsTable(), 'pc', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('pc.album_id', 'paf.album_id'),
|
2023-01-15 22:43:48 +00:00
|
|
|
$query->expr()->in('pc.collaborator_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_STR_ARRAY)),
|
2022-12-03 04:07:06 +00:00
|
|
|
));
|
|
|
|
|
2022-12-03 05:42:52 +00:00
|
|
|
return $query->executeQuery()->fetchOne();
|
2022-12-03 04:07:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 07:11:35 +00:00
|
|
|
/**
|
|
|
|
* Get album if allowed. Also check if album is shared with user.
|
|
|
|
*
|
2022-12-03 04:07:06 +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
|
|
|
*/
|
2023-03-24 00:56:41 +00:00
|
|
|
public function getIfAllowed(string $uid, string $albumId): ?array
|
2022-10-26 23:20:28 +00:00
|
|
|
{
|
2023-01-18 03:02:00 +00:00
|
|
|
$album = null;
|
|
|
|
|
2022-10-27 07:11:35 +00:00
|
|
|
// Split name and uid
|
|
|
|
$parts = explode('/', $albumId);
|
2023-01-18 03:02:00 +00:00
|
|
|
if (2 === \count($parts)) {
|
|
|
|
$albumUid = $parts[0];
|
|
|
|
$albumName = $parts[1];
|
|
|
|
|
|
|
|
// Check if owner
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')->from('photos_albums')->where(
|
|
|
|
$query->expr()->andX(
|
|
|
|
$query->expr()->eq('name', $query->createNamedParameter($albumName)),
|
|
|
|
$query->expr()->eq('user', $query->createNamedParameter($albumUid)),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$album = $query->executeQuery()->fetch();
|
2022-10-27 07:11:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
// Album not found: it could be a link token at best
|
2022-10-27 07:11:35 +00:00
|
|
|
if (!$album) {
|
2023-01-18 03:02:00 +00:00
|
|
|
return $this->getAlbumByLink($albumId);
|
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
|
2023-03-10 02:13:29 +00:00
|
|
|
$albumNumId = (int) $album['album_id'];
|
2023-03-23 23:58:49 +00:00
|
|
|
if ($this->userIsCollaborator($uid, $albumNumId)) {
|
2023-03-10 02:13:29 +00:00
|
|
|
return $album;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if user is a collaborator by numeric ID.
|
|
|
|
* Also checks if a group is a collaborator.
|
|
|
|
* Does not check if the user is the owner.
|
|
|
|
*
|
|
|
|
* @param string $uid User ID
|
|
|
|
* @param int $albumId Album ID (numeric)
|
|
|
|
*/
|
2023-03-23 23:58:49 +00:00
|
|
|
public function userIsCollaborator(string $uid, int $albumId): bool
|
2023-03-10 02:13:29 +00:00
|
|
|
{
|
2022-12-03 03:23:29 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2023-01-15 22:43:48 +00:00
|
|
|
$ids = $this->getSelfCollaborators($uid);
|
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(
|
2023-03-10 02:13:29 +00:00
|
|
|
$query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)),
|
2023-01-15 22:43:48 +00:00
|
|
|
$query->expr()->in('collaborator_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_STR_ARRAY)),
|
2022-10-26 23:20:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-03-10 02:13:29 +00:00
|
|
|
return false !== $query->executeQuery()->fetchOne();
|
2022-10-26 23:20:28 +00:00
|
|
|
}
|
2022-11-29 16:57:03 +00:00
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
/**
|
|
|
|
* Get album object by token.
|
|
|
|
* Returns false if album link does not exist.
|
|
|
|
*/
|
|
|
|
public function getAlbumByLink(string $token)
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')->from('photos_albums', 'pa')
|
|
|
|
->innerJoin('pa', $this->collaboratorsTable(), 'pc', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('pc.album_id', 'pa.album_id'),
|
|
|
|
$query->expr()->eq('collaborator_id', $query->createNamedParameter($token)),
|
2023-04-25 05:07:58 +00:00
|
|
|
$query->expr()->eq('collaborator_type', $query->expr()->literal(3, \PDO::PARAM_INT)), // = TYPE_LINK
|
2023-01-18 03:02:00 +00:00
|
|
|
))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $query->executeQuery()->fetch() ?: null;
|
|
|
|
}
|
|
|
|
|
2022-12-06 19:38:57 +00:00
|
|
|
/**
|
2023-03-22 23:55:20 +00:00
|
|
|
* Get list of photos in album.
|
2022-12-06 19:38:57 +00:00
|
|
|
*/
|
2023-03-22 23:55:20 +00:00
|
|
|
public function getAlbumPhotos(int $albumId, ?int $limit)
|
2022-12-06 19:38:57 +00:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2023-03-25 01:09:18 +00:00
|
|
|
|
|
|
|
// SELECT all files
|
|
|
|
$query->select('file_id')->from('photos_albums_files', 'paf');
|
|
|
|
|
|
|
|
// WHERE they are in this album
|
|
|
|
$query->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)));
|
|
|
|
|
|
|
|
// AND in the filecache
|
2022-12-06 19:38:57 +00:00
|
|
|
$query->innerJoin('paf', 'filecache', 'fc', $query->expr()->eq('fc.fileid', 'paf.file_id'));
|
|
|
|
|
2023-03-25 01:09:18 +00:00
|
|
|
// Do not check if these files are indexed in memories
|
|
|
|
// This is since this function is called for downloads
|
|
|
|
// so funky things might happen if non-indexed files were
|
|
|
|
// added throught the Photos app
|
|
|
|
|
|
|
|
// ORDER by the id of the paf i.e. the order in which they were added
|
|
|
|
$query->orderBy('paf.album_file_id', 'DESC');
|
|
|
|
|
|
|
|
// LIMIT the results
|
2023-03-22 19:54:03 +00:00
|
|
|
if (null !== $limit) {
|
|
|
|
$query->setMaxResults($limit);
|
2022-12-06 19:38:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 19:54:03 +00:00
|
|
|
$result = $query->executeQuery()->fetchAll();
|
|
|
|
|
|
|
|
foreach ($result as &$row) {
|
|
|
|
$row['fileid'] = (int) $row['file_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2022-12-06 19:38:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 22:43:48 +00:00
|
|
|
/** Get list of collaborator ids including user id and groups */
|
|
|
|
private function getSelfCollaborators(string $uid)
|
|
|
|
{
|
|
|
|
// Get groups for the user
|
|
|
|
$groupManager = \OC::$server->get(\OCP\IGroupManager::class);
|
|
|
|
$user = \OC::$server->get(\OCP\IUserManager::class)->get($uid);
|
|
|
|
$groups = $groupManager->getUserGroupIds($user);
|
|
|
|
|
|
|
|
// And albums shared with user
|
|
|
|
$groups[] = $uid;
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:57:03 +00:00
|
|
|
/** Get the name of the collaborators table */
|
2022-11-29 17:04:09 +00:00
|
|
|
private function collaboratorsTable()
|
|
|
|
{
|
2022-11-29 16:57:03 +00:00
|
|
|
// https://github.com/nextcloud/photos/commit/20e3e61ad577014e5f092a292c90a8476f630355
|
2022-12-04 17:57:31 +00:00
|
|
|
$appManager = \OC::$server->get(\OCP\App\IAppManager::class);
|
2022-11-29 16:57:03 +00:00
|
|
|
$photosVersion = $appManager->getAppVersion('photos');
|
2022-12-03 07:54:09 +00:00
|
|
|
if (version_compare($photosVersion, '2.0.1', '>=')) {
|
2022-11-29 16:57:03 +00:00
|
|
|
return 'photos_albums_collabs';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'photos_collaborators';
|
|
|
|
}
|
2022-10-26 22:12:46 +00:00
|
|
|
}
|