From 482b3b1e507c00c465f9ef5fe3ac81e3aef0eaee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Thu, 22 Sep 2022 16:50:25 +0200 Subject: [PATCH] Do not get all the files list when fetching the albums list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ --- lib/Album/AlbumMapper.php | 27 ++++++++++----------------- lib/Album/AlbumWithFiles.php | 24 ++++++++++++++++++++++-- lib/Sabre/Album/AlbumsHome.php | 9 +++++---- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php index 8f8d34287..4ece03c45 100644 --- a/lib/Album/AlbumMapper.php +++ b/lib/Album/AlbumMapper.php @@ -177,40 +177,33 @@ public function delete(int $id): void { } /** + * @param int $albumId * @param string $userId - * @return AlbumWithFiles[] + * @return AlbumFile[] */ - public function getForUserWithFiles(string $userId): array { + public function getForAlbumIdAndUserWithFiles(int $albumId, string $userId): array { $query = $this->connection->getQueryBuilder(); - $query->select("fileid", "mimetype", "a.album_id", "size", "mtime", "etag", "location", "created", "last_added_photo", "added", "owner") + $query->select("fileid", "mimetype", "a.album_id", "size", "mtime", "etag", "added", "owner") ->selectAlias("f.name", "file_name") ->selectAlias("a.name", "album_name") ->from("photos_albums", "a") ->leftJoin("a", "photos_albums_files", "p", $query->expr()->eq("a.album_id", "p.album_id")) ->leftJoin("p", "filecache", "f", $query->expr()->eq("p.file_id", "f.fileid")) - ->where($query->expr()->eq('user', $query->createNamedParameter($userId))); + ->where($query->expr()->eq('a.album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('user', $query->createNamedParameter($userId))); $rows = $query->executeQuery()->fetchAll(); - $filesByAlbum = []; - $albumsById = []; + $files = []; foreach ($rows as $row) { $albumId = (int)$row['album_id']; if ($row['fileid']) { $mimeId = $row['mimetype']; $mimeType = $this->mimeTypeLoader->getMimetypeById($mimeId); - $filesByAlbum[$albumId][] = new AlbumFile((int)$row['fileid'], $row['file_name'], $mimeType, (int)$row['size'], (int)$row['mtime'], $row['etag'], (int)$row['added'], $row['owner']); - } - - if (!isset($albumsById[$albumId])) { - $albumsById[$albumId] = new AlbumInfo($albumId, $userId, $row['album_name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']); + $files[] = new AlbumFile((int)$row['fileid'], $row['file_name'], $mimeType, (int)$row['size'], (int)$row['mtime'], $row['etag'], (int)$row['added'], $row['owner']); } } - $result = []; - foreach ($albumsById as $id => $album) { - $result[] = new AlbumWithFiles($album, $filesByAlbum[$id] ?? []); - } - return $result; + return $files ?? []; } /** @@ -414,7 +407,7 @@ public function getSharedAlbumsForCollaboratorWithFiles(string $collaboratorId, $result = []; foreach ($albumsById as $id => $album) { - $result[] = new AlbumWithFiles($album, $filesByAlbum[$id] ?? []); + $result[] = new AlbumWithFiles($album, $this, $filesByAlbum[$id] ?? []); } return $result; } diff --git a/lib/Album/AlbumWithFiles.php b/lib/Album/AlbumWithFiles.php index 54c89f8bb..37ca1f130 100644 --- a/lib/Album/AlbumWithFiles.php +++ b/lib/Album/AlbumWithFiles.php @@ -25,11 +25,17 @@ class AlbumWithFiles { private AlbumInfo $info; + private AlbumMapper $albumMapper; + /** @var AlbumFile[] */ private array $files; - public function __construct(AlbumInfo $info, array $files) { + public function __construct( + AlbumInfo $info, + AlbumMapper $albumMapper, + array $files = []) { $this->info = $info; + $this->albumMapper = $albumMapper; $this->files = $files; } @@ -41,6 +47,9 @@ public function getAlbum(): AlbumInfo { * @return AlbumFile[] */ public function getFiles(): array { + if (empty($this->files)) { + $this->files = $this->fetchFiles(); + } return $this->files; } @@ -48,6 +57,10 @@ public function getFiles(): array { * @return AlbumFile[] */ public function addFile(AlbumFile $file): array { + if (empty($this->files)) { + $this->files = $this->fetchFiles(); + } + array_push($this->files, $file); return $this->files; } @@ -58,6 +71,13 @@ public function addFile(AlbumFile $file): array { public function getFileIds(): array { return array_map(function (AlbumFile $file) { return $file->getFileId(); - }, $this->files); + }, $this->getFiles()); + } + + /** + * @return AlbumFile[] + */ + private function fetchFiles(): array { + return $this->albumMapper->getForAlbumIdAndUserWithFiles($this->info->getId(), $this->info->getUserId()) ?? []; } } diff --git a/lib/Sabre/Album/AlbumsHome.php b/lib/Sabre/Album/AlbumsHome.php index 14051f57d..b3d42f4fb 100644 --- a/lib/Sabre/Album/AlbumsHome.php +++ b/lib/Sabre/Album/AlbumsHome.php @@ -23,6 +23,7 @@ namespace OCA\Photos\Sabre\Album; +use OCA\Photos\Album\AlbumInfo; use OCA\Photos\Album\AlbumMapper; use OCA\Photos\Album\AlbumWithFiles; use OCA\Photos\Service\UserConfigService; @@ -106,10 +107,10 @@ public function getChild($name) { */ public function getChildren(): array { if ($this->children === null) { - $folders = $this->albumMapper->getForUserWithFiles($this->user->getUID()); - $this->children = array_map(function (AlbumWithFiles $folder) { - return new AlbumRoot($this->albumMapper, $folder, $this->rootFolder, $this->userFolder, $this->user, $this->userConfigService); - }, $folders); + $albumInfos = $this->albumMapper->getForUser($this->user->getUID()); + $this->children = array_map(function (AlbumInfo $albumInfo) { + return new AlbumRoot($this->albumMapper, new AlbumWithFiles($albumInfo, $this->albumMapper), $this->rootFolder, $this->userFolder, $this->user, $this->userConfigService); + }, $albumInfos); } return $this->children;