Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions js/photos-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/photos-main.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/photos-src_views_AlbumContent_vue.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/photos-src_views_AlbumContent_vue.js.map

Large diffs are not rendered by default.

27 changes: 10 additions & 17 deletions lib/Album/AlbumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [];
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
24 changes: 22 additions & 2 deletions lib/Album/AlbumWithFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -41,13 +47,20 @@ public function getAlbum(): AlbumInfo {
* @return AlbumFile[]
*/
public function getFiles(): array {
if (empty($this->files)) {
$this->files = $this->fetchFiles();
}
return $this->files;
}

/**
* @return AlbumFile[]
*/
public function addFile(AlbumFile $file): array {
if (empty($this->files)) {
$this->files = $this->fetchFiles();
}

array_push($this->files, $file);
return $this->files;
}
Expand All @@ -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()) ?? [];
}
}
6 changes: 3 additions & 3 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
namespace OCA\Photos\AppInfo;

use OCA\DAV\Connector\Sabre\Principal;
use OCA\Photos\Listener\CacheEntryRemovedListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCA\Photos\Event\MoveToTrashListener;
use OCP\Files\Cache\CacheEntryRemovedEvent;

class Application extends App implements IBootstrap {
public const APP_ID = 'photos';
Expand Down Expand Up @@ -63,7 +63,7 @@ public function __construct() {
public function register(IRegistrationContext $context): void {
/** Register $principalBackend for the DAV collection */
$context->registerServiceAlias('principalBackend', Principal::class);
$context->registerEventListener(NodeDeletedEvent::class, MoveToTrashListener::class);
$context->registerEventListener(CacheEntryRemovedEvent::class, CacheEntryRemovedListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?php

namespace OCA\Photos\Event;
namespace OCA\Photos\Listener;

use OCA\Photos\Album\AlbumMapper;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCA\Photos\Album\AlbumMapper;
use OCP\Files\Cache\CacheEntryRemovedEvent;

class MoveToTrashListener implements IEventListener {
class CacheEntryRemovedListener implements IEventListener {
private AlbumMapper $albumMapper;

public function __construct(AlbumMapper $albumMapper) {
$this->albumMapper = $albumMapper;
}

public function handle(Event $event): void {
if (!($event instanceof NodeDeletedEvent)) {
if (!($event instanceof CacheEntryRemovedEvent)) {
return;
}

// Remove node from all albums containing it.
$albums = $this->albumMapper->getForFile($event->getNode()->getId());
$albums = $this->albumMapper->getForFile($event->getFileId());
foreach ($albums as $album) {
$this->albumMapper->removeFile($album->getId(), $event->getNode()->getId());
$this->albumMapper->removeFile($album->getId(), $event->getFileId());
}
}
}
9 changes: 5 additions & 4 deletions lib/Sabre/Album/AlbumsHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/views/AlbumContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,13 @@ export default {
}
)

// Gen files info and filtering invalid files
const fetchedFiles = response.data
.map(file => genFileInfo(file))
.filter(file => file.fileid)

const fileIds = fetchedFiles
.map(file => file.fileid)
.map((fileId) => fileId.toString())
.map(file => file.fileid.toString())

this.appendFiles(fetchedFiles)

Expand Down