-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Louis Chemineau <louis@chmn.me>
- Loading branch information
Showing
4 changed files
with
97 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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): void { | ||
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); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.