diff options
author | Louis Chemineau <louis@chmn.me> | 2022-09-16 14:58:05 +0300 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2022-09-16 15:41:29 +0300 |
commit | 73e726a3dc460a4b5292998779c123cbec70fa5b (patch) | |
tree | 3f470f0469fc17a08eaf966447eb30bf6fdf6f00 | |
parent | 58947bb6188ab50aa5c005093dbc19ca7f9c9dc2 (diff) |
Rename collaborator.source to .type
And do the mapping in the frontend
Signed-off-by: Louis Chemineau <louis@chmn.me>
-rw-r--r-- | lib/Album/AlbumMapper.php | 43 | ||||
-rw-r--r-- | lib/Migration/Version20001Date20220830131446.php | 4 | ||||
-rw-r--r-- | lib/Sabre/Album/SharedAlbumRoot.php | 4 | ||||
-rw-r--r-- | src/components/Albums/CollaboratorsSelectionForm.vue | 23 |
4 files changed, 42 insertions, 32 deletions
diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php index cd6cb32b..28126007 100644 --- a/lib/Album/AlbumMapper.php +++ b/lib/Album/AlbumMapper.php @@ -249,11 +249,11 @@ class AlbumMapper { /** * @param int $albumId - * @return array<array{'id': string, 'label': string, 'source': string}> + * @return array<array{'id': string, 'label': string, 'type': int}> */ public function getCollaborators(int $albumId): array { $query = $this->connection->getQueryBuilder(); - $query->select("collaborator_id", "collaborator_source") + $query->select("collaborator_id", "collaborator_type") ->from("photos_collaborators") ->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))); @@ -263,7 +263,7 @@ class AlbumMapper { /** @var IUser|IGroup|null */ $collaborator = null; - switch ($row['collaborator_source']) { + switch ($row['collaborator_type']) { case self::TYPE_USER: $collaborator = $this->userManager->get($row['collaborator_id']); break; @@ -271,7 +271,7 @@ class AlbumMapper { $collaborator = $this->groupManager->get($row['collaborator_id']); break; default: - throw new \Exception('Invalid collaborator source: ' . $row['collaborator_source']); + throw new \Exception('Invalid collaborator type: ' . $row['collaborator_type']); } if (is_null($collaborator)) { @@ -281,7 +281,7 @@ class AlbumMapper { return [ 'id' => $row['collaborator_id'], 'label' => $collaborator->getDisplayName(), - 'source' => $row['collaborator_source'] === self::TYPE_USER ? "users" : "groups", + 'type' => $row['collaborator_type'], ]; }, $rows); @@ -290,40 +290,37 @@ class AlbumMapper { /** * @param int $albumId - * @param array{'id': string, 'label': string, 'source': int} $collaborators + * @param array{'id': string, 'label': string, 'type': int} $collaborators */ public function setCollaborators(int $albumId, array $collaborators): void { $existingCollaborators = $this->getCollaborators($albumId); - $collaboratorsToAdd = array_udiff($collaborators, $existingCollaborators, fn ($a, $b) => strcmp($a['id'].$a['source'], $b['id'].$b['source'])); - $collaboratorsToRemove = array_udiff($existingCollaborators, $collaborators, fn ($a, $b) => strcmp($a['id'].$a['source'], $b['id'].$b['source'])); + $collaboratorsToAdd = array_udiff($collaborators, $existingCollaborators, fn ($a, $b) => strcmp($a['id'].$a['type'], $b['id'].$b['type'])); + $collaboratorsToRemove = array_udiff($existingCollaborators, $collaborators, fn ($a, $b) => strcmp($a['id'].$a['type'], $b['id'].$b['type'])); $this->connection->beginTransaction(); foreach ($collaboratorsToAdd as $collaborator) { - $source = null; - switch ($collaborator['source']) { + switch ($collaborator['type']) { case self::TYPE_USER: - $source = self::TYPE_USER; if (is_null($this->userManager->get($collaborator['id']))) { throw new \Exception('Unknown collaborator: ' . $collaborator['id']); } break; case self::TYPE_GROUP: - $source = self::TYPE_GROUP; if (is_null($this->groupManager->get($collaborator['id']))) { throw new \Exception('Unknown collaborator: ' . $collaborator['id']); } break; default: - throw new \Exception('Invalid collaborator source: ' . $collaborator['source']); + throw new \Exception('Invalid collaborator type: ' . $collaborator['type']); } $query = $this->connection->getQueryBuilder(); $query->insert('photos_collaborators') ->setValue('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)) ->setValue('collaborator_id', $query->createNamedParameter($collaborator['id'])) - ->setValue('collaborator_source', $query->createNamedParameter($source, IQueryBuilder::PARAM_INT)) + ->setValue('collaborator_type', $query->createNamedParameter($collaborator['type'], IQueryBuilder::PARAM_INT)) ->executeStatement(); } @@ -332,7 +329,7 @@ class AlbumMapper { $query->delete('photos_collaborators') ->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))) ->andWhere($query->expr()->eq('collaborator_id', $query->createNamedParameter($collaborator['id']))) - ->andWhere($query->expr()->eq('collaborator_source', $query->createNamedParameter($collaborator['source'], IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('collaborator_type', $query->createNamedParameter($collaborator['type'], IQueryBuilder::PARAM_INT))) ->executeStatement(); } @@ -341,10 +338,10 @@ class AlbumMapper { /** * @param string $collaboratorId - * @param string $collaboratorsSource - The source of the collaborator, either a user or a group. + * @param string $collaboratorsType - The type of the collaborator, either a user or a group. * @return AlbumWithFiles[] */ - public function getSharedAlbumsForCollaboratorWithFiles(string $collaboratorId, int $collaboratorSource): array { + public function getSharedAlbumsForCollaboratorWithFiles(string $collaboratorId, int $collaboratorType): array { $query = $this->connection->getQueryBuilder(); $rows = $query ->select("fileid", "mimetype", "a.album_id", "size", "mtime", "etag", "location", "created", "last_added_photo", "added", 'owner') @@ -356,7 +353,7 @@ class AlbumMapper { ->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('collaborator_id', $query->createNamedParameter($collaboratorId))) - ->andWhere($query->expr()->eq('collaborator_source', $query->createNamedParameter($collaboratorSource, IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('collaborator_type', $query->createNamedParameter($collaboratorType, IQueryBuilder::PARAM_INT))) ->executeQuery() ->fetchAll(); @@ -387,23 +384,23 @@ class AlbumMapper { * @param int $albumId * @return void */ - public function deleteCollaboratorFromAlbum(string $userId, int $albumId): 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_collaborators') ->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_source', $query->createNamedParameter(self::TYPE_USER, IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('collaborator_type', $query->createNamedParameter(self::TYPE_USER, IQueryBuilder::PARAM_INT))) ->executeStatement(); } /** * @param string $collaboratorId - * @param int $collaboratorSource + * @param int $collaboratorType * @param int $fileId * @return AlbumInfo[] */ - public function getAlbumForCollaboratorIdAndFileId(string $collaboratorId, int $collaboratorSource, int $fileId): array { + public function getAlbumForCollaboratorIdAndFileId(string $collaboratorId, int $collaboratorType, int $fileId): array { $query = $this->connection->getQueryBuilder(); $rows = $query ->select("a.album_id", "name", "user", "location", "created", "last_added_photo") @@ -411,7 +408,7 @@ class AlbumMapper { ->leftJoin("c", "photos_albums", "a", $query->expr()->eq("a.album_id", "c.album_id")) ->leftJoin("a", "photos_albums_files", "p", $query->expr()->eq("a.album_id", "p.album_id")) ->where($query->expr()->eq('collaborator_id', $query->createNamedParameter($collaboratorId))) - ->andWhere($query->expr()->eq('collaborator_source', $query->createNamedParameter($collaboratorSource, IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('collaborator_type', $query->createNamedParameter($collaboratorType, IQueryBuilder::PARAM_INT))) ->andWhere($query->expr()->eq('file_id', $query->createNamedParameter($fileId))) ->groupBy('album_id') ->executeQuery() diff --git a/lib/Migration/Version20001Date20220830131446.php b/lib/Migration/Version20001Date20220830131446.php index b16d6e1f..6c9079e6 100644 --- a/lib/Migration/Version20001Date20220830131446.php +++ b/lib/Migration/Version20001Date20220830131446.php @@ -58,11 +58,11 @@ class Version20001Date20220830131446 extends SimpleMigrationStep { 'notnull' => true, 'length' => 64, ]); - $table->addColumn('collaborator_source', Types::INTEGER, [ + $table->addColumn('collaborator_type', Types::INTEGER, [ 'notnull' => true, ]); - $table->addUniqueConstraint(['album_id', 'collaborator_id', 'collaborator_source'], 'collaborators_unique_idx'); + $table->addUniqueConstraint(['album_id', 'collaborator_id', 'collaborator_type'], 'collaborators_unique_idx'); } if (!$schema->getTable("photos_albums_files")->hasColumn("owner")) { diff --git a/lib/Sabre/Album/SharedAlbumRoot.php b/lib/Sabre/Album/SharedAlbumRoot.php index edb8d3ca..26a2272e 100644 --- a/lib/Sabre/Album/SharedAlbumRoot.php +++ b/lib/Sabre/Album/SharedAlbumRoot.php @@ -32,7 +32,7 @@ class SharedAlbumRoot extends AlbumRoot { * @return void */ public function delete() { - $this->albumMapper->deleteCollaboratorFromAlbum($this->user->getUID(), $this->album->getAlbum()->getId()); + $this->albumMapper->deleteUserFromAlbumCollaboratorsList($this->user->getUID(), $this->album->getAlbum()->getId()); } /** @@ -48,7 +48,7 @@ class SharedAlbumRoot extends AlbumRoot { } $collaboratorIds = array_map( - fn ($collaborator) => $collaborator['source'].':'.$collaborator['id'], + fn ($collaborator) => $collaborator['type'].':'.$collaborator['id'], $this->albumMapper->getCollaborators($this->album->getAlbum()->getId()), ); $uid = $this->user->getUID(); diff --git a/src/components/Albums/CollaboratorsSelectionForm.vue b/src/components/Albums/CollaboratorsSelectionForm.vue index 5651808b..fc237e3e 100644 --- a/src/components/Albums/CollaboratorsSelectionForm.vue +++ b/src/components/Albums/CollaboratorsSelectionForm.vue @@ -192,7 +192,7 @@ export default { searchResults() { return this.currentSearchResults .filter(({ id }) => id !== getCurrentUser().uid) - .map(({ source, id }) => `${source}:${id}`) + .map(({ type, id }) => `${type}:${id}`) .filter(key => !this.selectedCollaboratorsKeys.includes(key)) .map((key) => ({ key, height: 48 })) }, @@ -207,11 +207,11 @@ export default { mounted() { this.searchCollaborators() - this.selectedCollaboratorsKeys = this.collaborators.map(({ source, id }) => `${source}:${id}`) + this.selectedCollaboratorsKeys = this.collaborators.map(({ type, id }) => `${type}:${id}`) this.availableCollaborators = { ...this.availableCollaborators, ...this.collaborators - .reduce((collaborators, collaborator) => ({ ...collaborators, [`${collaborator.source}:${collaborator.id}`]: collaborator }), {}), + .reduce((collaborators, collaborator) => ({ ...collaborators, [`${collaborator.type}:${collaborator.id}`]: collaborator }), {}), } }, @@ -238,10 +238,23 @@ export default { }) this.currentSearchResults = response.data.ocs.data + .map(collaborator => { + let type = -1 + switch (collaborator.source) { + case 'users': + type = OC.Share.SHARE_TYPE_USER + break + case 'groups': + type = OC.Share.SHARE_TYPE_GROUP + break + } + + return { ...collaborator, type } + }) this.availableCollaborators = { ...this.availableCollaborators, - ...response.data.ocs.data - .reduce((collaborators, collaborator) => ({ ...collaborators, [`${collaborator.source}:${collaborator.id}`]: collaborator }), {}), + ...this.currentSearchResults + .reduce((collaborators, collaborator) => ({ ...collaborators, [`${collaborator.type}:${collaborator.id}`]: collaborator }), {}), } } catch (error) { this.errorFetchingCollaborators = error |