Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/photos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Chemineau <louis@chmn.me>2022-10-13 17:27:06 +0300
committerLouis Chemineau <louis@chmn.me>2022-10-13 17:27:06 +0300
commit0330548151d5a36bafbadcd5fd611c391c849123 (patch)
tree89e43fc99fe74bd0885f7ed3d410912138f0db16
parentabc4f5f4313133365265679f1e8f026a8f7a54c5 (diff)
Listen to more events for albumsartonge/feat/listen_to_more_event_for_albums
Signed-off-by: Louis Chemineau <louis@chmn.me>
-rw-r--r--lib/Album/AlbumMapper.php26
-rw-r--r--lib/AppInfo/Application.php8
-rw-r--r--lib/Listener/AlbumsManagementFileEventListener.php64
-rw-r--r--lib/Listener/CacheEntryRemovedListener.php28
4 files changed, 96 insertions, 30 deletions
diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php
index 8cb7b6bb..97e9c258 100644
--- a/lib/Album/AlbumMapper.php
+++ b/lib/Album/AlbumMapper.php
@@ -273,6 +273,32 @@ class AlbumMapper {
$query->executeStatement();
}
+ public function removeFileWithOwner(int $fileId, string $ownerId): void {
+ // Get concerned albums.
+ $query = $this->connection->getQueryBuilder();
+ $rows = $query->select('album_id')
+ ->from("photos_albums_files")
+ ->where($query->expr()->eq("owner_id", $query->createNamedParameter($ownerId)))
+ ->andWhere($query->expr()->eq("file_id", $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
+ $query->executeStatement();
+
+ // Remove any occurrence of fileId when owner is ownerId.
+ $query = $this->connection->getQueryBuilder();
+ $query->delete("photos_albums_files")
+ ->where($query->expr()->eq("owner_id", $query->createNamedParameter($ownerId)))
+ ->andWhere($query->expr()->eq("file_id", $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
+ $query->executeStatement();
+
+ // Update last_added_photo for concerned albums.
+ foreach ($rows as $row) {
+ $query = $this->connection->getQueryBuilder();
+ $query->update("photos_albums")
+ ->set('last_added_photo', $query->createNamedParameter($this->getLastAdded($row['album_id']), IQueryBuilder::PARAM_INT))
+ ->where($query->expr()->eq('album_id', $query->createNamedParameter($row['album_id'], IQueryBuilder::PARAM_INT)));
+ $query->executeStatement();
+ }
+ }
+
private function getLastAdded(int $albumId): int {
$query = $this->connection->getQueryBuilder();
$query->select("file_id")
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
index 4a1dc328..43a9ae22 100644
--- a/lib/AppInfo/Application.php
+++ b/lib/AppInfo/Application.php
@@ -28,12 +28,14 @@ namespace OCA\Photos\AppInfo;
use OCA\DAV\Events\SabrePluginAuthInitEvent;
use OCA\Photos\Listener\SabrePluginAuthInitListener;
use OCA\DAV\Connector\Sabre\Principal;
-use OCA\Photos\Listener\CacheEntryRemovedListener;
+use OCA\Photos\Listener\AlbumsManagementFileEventListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Cache\CacheEntryRemovedEvent;
+use OCP\Share\Events\ShareDeletedEvent;
+use OCP\User\Events\UserDeletedEvent;
class Application extends App implements IBootstrap {
public const APP_ID = 'photos';
@@ -65,7 +67,9 @@ class Application extends App implements IBootstrap {
public function register(IRegistrationContext $context): void {
/** Register $principalBackend for the DAV collection */
$context->registerServiceAlias('principalBackend', Principal::class);
- $context->registerEventListener(CacheEntryRemovedEvent::class, CacheEntryRemovedListener::class);
+ $context->registerEventListener(CacheEntryRemovedEvent::class, AlbumsManagementFileEventListener::class);
+ $context->registerEventListener(UserDeletedEvent::class, AlbumsManagementFileEventListener::class);
+ $context->registerEventListener(ShareDeletedEvent::class, AlbumsManagementFileEventListener::class);
$context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class);
}
diff --git a/lib/Listener/AlbumsManagementFileEventListener.php b/lib/Listener/AlbumsManagementFileEventListener.php
new file mode 100644
index 00000000..8ae5d970
--- /dev/null
+++ b/lib/Listener/AlbumsManagementFileEventListener.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace OCA\Photos\Listener;
+
+use OCA\Photos\Album\AlbumMapper;
+use OCP\Files\File;
+use OCP\Files\Folder;
+use OCP\Files\Node;
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventListener;
+use OCP\Files\Cache\CacheEntryRemovedEvent;
+use OCP\Share\Events\ShareDeletedEvent;
+use OCP\User\Events\UserDeletedEvent;
+
+class AlbumsManagementFileEventListener implements IEventListener {
+ private AlbumMapper $albumMapper;
+
+ public function __construct(AlbumMapper $albumMapper) {
+ $this->albumMapper = $albumMapper;
+ }
+
+ public function handle(Event $event): void {
+ if ($event instanceof CacheEntryRemovedEvent) {
+ // Remove node from all albums containing it.
+ $albums = $this->albumMapper->getForFile($event->getFileId());
+ foreach ($albums as $album) {
+ $this->albumMapper->removeFile($album->getId(), $event->getFileId());
+ }
+ }
+
+ if ($event instanceof UserDeletedEvent) {
+ // Delete all user's albums.
+ $albums = $this->albumMapper->getForUser($event->getUser()->getUID());
+ foreach ($albums as $album) {
+ $this->albumMapper->delete($album->getId());
+ }
+ }
+
+ if ($event instanceof ShareDeletedEvent) {
+ $receiverId = $event->getShare()->getSharedWith();
+ $this->forEachSubNode(
+ $event->getShare()->getNode(),
+ // Remove node from any album when the owner is receiverId.
+ fn ($node) => $this->albumMapper->removeFileWithOwner($node->getId(), $receiverId),
+ );
+ }
+ }
+
+ private function forEachSubNode(Node $node, callable $callback) {
+ if ($node instanceof Folder) {
+ foreach ($node->getDirectoryListing() as $subNode) {
+ $this->forEachSubNode($subNode, $callback);
+ }
+ }
+
+ if ($node instanceof File) {
+ if (!str_starts_with($node->getMimeType(), 'image')) {
+ return;
+ }
+
+ $callback($node);
+ }
+ }
+}
diff --git a/lib/Listener/CacheEntryRemovedListener.php b/lib/Listener/CacheEntryRemovedListener.php
deleted file mode 100644
index 20d088db..00000000
--- a/lib/Listener/CacheEntryRemovedListener.php
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-
-namespace OCA\Photos\Listener;
-
-use OCA\Photos\Album\AlbumMapper;
-use OCP\EventDispatcher\Event;
-use OCP\EventDispatcher\IEventListener;
-use OCP\Files\Cache\CacheEntryRemovedEvent;
-
-class CacheEntryRemovedListener implements IEventListener {
- private AlbumMapper $albumMapper;
-
- public function __construct(AlbumMapper $albumMapper) {
- $this->albumMapper = $albumMapper;
- }
-
- public function handle(Event $event): void {
- if (!($event instanceof CacheEntryRemovedEvent)) {
- return;
- }
-
- // Remove node from all albums containing it.
- $albums = $this->albumMapper->getForFile($event->getFileId());
- foreach ($albums as $album) {
- $this->albumMapper->removeFile($album->getId(), $event->getFileId());
- }
- }
-}