Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 61 additions & 1 deletion lib/Album/AlbumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ public function removeFile(int $albumId, int $fileId): void {
$query->executeStatement();
}

public function removeFilesForUser(int $albumId, string $userId) {
// Remove all photos by this user from the album:
$query = $this->connection->getQueryBuilder();
$query->delete('photos_albums_files')
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('owner', $query->createNamedParameter($userId)))
->executeStatement();

// Update the last added photo:
$query = $this->connection->getQueryBuilder();
$query->update("photos_albums")
->set('last_added_photo', $query->createNamedParameter($this->getLastAdded($albumId), IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)))
->executeStatement();
}

private function getLastAdded(int $albumId): int {
$query = $this->connection->getQueryBuilder();
$query->select("file_id")
Expand Down Expand Up @@ -422,6 +438,34 @@ function computeKey($c) {
$this->connection->commit();
}

/**
* @param string $collaboratorId
* @param int $collaboratorType
* @return AlbumInfo[]
*/
public function getSharedAlbumsForCollaborator(string $collaboratorId, int $collaboratorType): array {
$query = $this->connection->getQueryBuilder();
$rows = $query
->select("a.album_id", "name", "user", "location", "created", "last_added_photo")
->from("photos_albums_collabs", "c")
->leftJoin("c", "photos_albums", "a", $query->expr()->eq("a.album_id", "c.album_id"))
->where($query->expr()->eq('collaborator_id', $query->createNamedParameter($collaboratorId)))
->andWhere($query->expr()->eq('collaborator_type', $query->createNamedParameter($collaboratorType, IQueryBuilder::PARAM_INT)))
->executeQuery()
->fetchAll();

return array_map(function (array $row) {
return new AlbumInfo(
(int)$row['album_id'],
$row['user'],
$row['name'].' ('.$row['user'].')',
$row['location'],
(int)$row['created'],
(int)$row['last_added_photo']
);
}, $rows);
}

/**
* @param string $collaboratorId
* @param string $collaboratorsType - The type of the collaborator, either a user or a group.
Expand Down Expand Up @@ -477,13 +521,29 @@ public function getSharedAlbumsForCollaboratorWithFiles(string $collaboratorId,
* @return void
*/
public function deleteUserFromAlbumCollaboratorsList(string $userId, int $albumId): void {
// TODO: only delete if this was not a group share
$query = $this->connection->getQueryBuilder();
$query->delete('photos_albums_collabs')
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('collaborator_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('collaborator_type', $query->createNamedParameter(self::TYPE_USER, IQueryBuilder::PARAM_INT)))
->executeStatement();

// Remove all photos by this user from the album:
$this->removeFilesForUser($albumId, $userId);
}

/**
* @param string $groupId
* @param int $albumId
* @return void
*/
public function deleteGroupFromAlbumCollaboratorsList(string $groupId, int $albumId): void {
$query = $this->connection->getQueryBuilder();
$query->delete('photos_albums_collabs')
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('collaborator_id', $query->createNamedParameter($groupId)))
->andWhere($query->expr()->eq('collaborator_type', $query->createNamedParameter(self::TYPE_GROUP, IQueryBuilder::PARAM_INT)))
->executeStatement();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
use OCA\Photos\Listener\SabrePluginAuthInitListener;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\Photos\Listener\NodeDeletedListener;
use OCA\Photos\Listener\GroupUserRemovedListener;
use OCA\Photos\Listener\GroupDeletedListener;
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 OCP\Group\Events\UserRemovedEvent;
use OCP\Group\Events\GroupDeletedEvent;

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

$context->registerEventListener(NodeDeletedEvent::class, NodeDeletedListener::class);

$context->registerEventListener(UserRemovedEvent::class, GroupUserRemovedListener::class);

$context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class);

$context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class);
}

Expand Down
47 changes: 47 additions & 0 deletions lib/Listener/GroupDeletedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace OCA\Photos\Listener;

use OCA\Photos\Album\AlbumMapper;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Group\Events\GroupDeletedEvent;

class GroupDeletedListener implements IEventListener {
private AlbumMapper $albumMapper;

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

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

// Get all shared albums for this group:
$albums_group = $this->albumMapper->getSharedAlbumsForCollaborator($event->getGroup()->getGID(), AlbumMapper::TYPE_GROUP);

// Get all users of this group:
$users = $event->getGroup()->getUsers();

foreach ($users as $user) {
$uid = $user->getUID();

// Get all albums shared with this specific user:
$albums_user = $this->albumMapper->getSharedAlbumsForCollaborator($user->getUID(), AlbumMapper::TYPE_USER);

// Get all group-shared albums that are not directly shared with the removed user in addition
$albums = array_udiff($albums_group, $albums_user, fn ($a, $b) => ($a->getId() - $b->getId()));

// Remove their photos from theses albums:
foreach ($albums as $album) {
$this->albumMapper->removeFilesForUser($album->getId(), $user->getUID());
}
}

foreach ($albums_group as $album) {
$this->albumMapper->deleteGroupFromAlbumCollaboratorsList($event->getGroup()->getGID(), $album->getId());
}
}
}
34 changes: 34 additions & 0 deletions lib/Listener/GroupUserRemovedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace OCA\Photos\Listener;

use OCA\Photos\Album\AlbumMapper;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Group\Events\UserRemovedEvent;

class GroupUserRemovedListener implements IEventListener {
private AlbumMapper $albumMapper;

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

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

// Get all shared albums for this group:
$albums_group = $this->albumMapper->getSharedAlbumsForCollaborator($event->getGroup()->getGID(), AlbumMapper::TYPE_GROUP);
// Get all albums shared with this specific user:
$albums_user = $this->albumMapper->getSharedAlbumsForCollaborator($event->getUser()->getUID(), AlbumMapper::TYPE_USER);
// Get all group-shared albums that are not directly shared with the removed user in addition
$albums = array_udiff($albums_group, $albums_user, fn ($a, $b) => ($a->getId() - $b->getId()));

// Remove their photos from theses albums:
foreach ($albums as $album) {
$this->albumMapper->removeFilesForUser($album->getId(), $event->getUser()->getUID());
}
}
}