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
|
|
|
{
|
2023-10-15 01:51:17 +00:00
|
|
|
public function __construct(private IDBConnection $connection) {}
|
2022-10-26 23:20:28 +00:00
|
|
|
|
2023-08-02 22:22:06 +00:00
|
|
|
/**
|
|
|
|
* Get list of albums.
|
|
|
|
*
|
|
|
|
* @param bool $shared Whether to get shared albums
|
|
|
|
* @param int $fileid File to filter by
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public function getList(string $uid, bool $shared = false, int $fileid = 0): array
|
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');
|
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',
|
2023-10-15 01:59:00 +00:00
|
|
|
$count,
|
2023-07-06 20:18:59 +00:00
|
|
|
)->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
|
|
|
|
2023-08-02 22:22:06 +00:00
|
|
|
// WHERE these albums contain fileid if specified
|
2023-08-04 01:56:51 +00:00
|
|
|
if ($fileid) {
|
2023-08-02 22:22:06 +00:00
|
|
|
$fSq = $this->connection->getQueryBuilder()
|
|
|
|
->select('paf.file_id')
|
|
|
|
->from('photos_albums_files', 'paf')
|
|
|
|
->where($query->expr()->andX(
|
|
|
|
$query->expr()->eq('paf.album_id', 'pa.album_id'),
|
|
|
|
$query->expr()->eq('paf.file_id', $query->createNamedParameter($fileid, IQueryBuilder::PARAM_INT)),
|
|
|
|
))
|
|
|
|
->getSQL()
|
|
|
|
;
|
|
|
|
$query->andWhere($query->createFunction("EXISTS ({$fSq})"));
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:12:46 +00:00
|
|
|
// FETCH all albums
|
|
|
|
$albums = $query->executeQuery()->fetchAll();
|
|
|
|
|
|
|
|
// Post process
|
|
|
|
foreach ($albums as &$row) {
|
2023-03-25 01:09:18 +00:00
|
|
|
$row['cluster_id'] = $row['user'].'/'.$row['name'];
|
2023-08-02 22:28:28 +00:00
|
|
|
$row['album_id'] = (int) $row['album_id'];
|
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'];
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @return false|string owner of file if found
|
2023-01-18 03:02:00 +00:00
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public function hasFile(int $albumId, int $fileId): false|string
|
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)),
|
2023-10-15 01:59:00 +00:00
|
|
|
),
|
2023-01-18 03:02:00 +00:00
|
|
|
);
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
return $query->executeQuery()->fetchOne();
|
2023-01-18 03:02:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @return false|string owner of file if found
|
2022-12-03 04:07:06 +00:00
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public function userHasFile(string $uid, int $fileId): false|string
|
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'),
|
|
|
|
),
|
2023-10-15 01:59:00 +00:00
|
|
|
),
|
2022-12-03 04:07:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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)),
|
2023-10-15 01:59:00 +00:00
|
|
|
),
|
2023-01-18 03:02:00 +00:00
|
|
|
);
|
|
|
|
$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
|
2023-10-15 19:46:35 +00:00
|
|
|
/** @psalm-suppress PossiblyUndefinedVariable */
|
2022-10-27 07:11:35 +00:00
|
|
|
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)),
|
2023-10-15 01:59:00 +00:00
|
|
|
),
|
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.
|
|
|
|
*/
|
2023-10-14 09:07:18 +00:00
|
|
|
public function getAlbumByLink(string $token): ?array
|
2023-01-18 03:02:00 +00:00
|
|
|
{
|
|
|
|
$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-10-14 08:25:50 +00:00
|
|
|
public function getAlbumPhotos(int $albumId, ?int $limit): array
|
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-10-15 19:46:35 +00:00
|
|
|
/**
|
|
|
|
* Get the various collaborator IDs that a user has.
|
|
|
|
* This includes the groups the user is in and the user itself.
|
|
|
|
*
|
|
|
|
* @return string[] List of collaborator IDs
|
|
|
|
*/
|
2023-01-15 22:43:48 +00:00
|
|
|
private function getSelfCollaborators(string $uid)
|
|
|
|
{
|
2023-10-15 19:46:35 +00:00
|
|
|
// Get the user in question
|
|
|
|
$user = \OC::$server->get(\OCP\IUserManager::class)->get($uid)
|
|
|
|
?: throw new \Exception('User not found');
|
2023-01-15 22:43:48 +00:00
|
|
|
// Get groups for the user
|
2023-10-15 19:46:35 +00:00
|
|
|
$groups = \OC::$server->get(\OCP\IGroupManager::class)->getUserGroupIds($user);
|
2023-01-15 22:43:48 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
// Add the user itself as a collaborator
|
2023-01-15 22:43:48 +00:00
|
|
|
$groups[] = $uid;
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the collaborators table.
|
|
|
|
*/
|
|
|
|
private function collaboratorsTable(): string
|
2022-11-29 17:04:09 +00:00
|
|
|
{
|
2022-11-29 16:57:03 +00:00
|
|
|
// https://github.com/nextcloud/photos/commit/20e3e61ad577014e5f092a292c90a8476f630355
|
2023-10-15 19:46:35 +00:00
|
|
|
$photosVersion = \OC::$server->get(\OCP\App\IAppManager::class)->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
|
|
|
}
|