Skip to content

Commit

Permalink
refactor: Move groupfolder renaming to back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
zak39 committed Oct 31, 2024
1 parent b438446 commit 2b65ac5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 14 deletions.
3 changes: 1 addition & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@
],
[
'name' => 'workspace#renameSpace',
// TODO move this route to /api/spaces
'url' => '/api/space/rename',
'url' => '/spaces/{spaceId}',
'verb' => 'PATCH'
],
[
Expand Down
17 changes: 7 additions & 10 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCA\Workspace\Service\UserService;
use OCA\Workspace\Service\Workspace\WorkspaceCheckService;
use OCA\Workspace\Service\WorkspaceService;
use OCA\Workspace\Space\SpaceManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
Expand All @@ -64,6 +65,7 @@ public function __construct(
private WorkspaceService $workspaceService,
private UserGroup $userGroup,
private WorkspaceManagerGroup $workspaceManagerGroup,
private SpaceManager $spaceManager,
public $AppName
) {
parent::__construct($AppName, $request);
Expand Down Expand Up @@ -308,16 +310,12 @@ public function changeUserRole(array|string $space,
*
* @NoAdminRequired
* @SpaceAdminRequired
* @param array|string $workspace
* @param int $spaceId
* @param string $newSpaceName
*
* @todo Manage errors
*/
public function renameSpace(array|string $workspace,
public function renameSpace(int $spaceId,
string $newSpaceName): JSONResponse {
if (gettype($workspace) === 'string') {
$workspace = json_decode($workspace, true);
}

if ($this->workspaceCheck->containSpecialChar($newSpaceName)) {
throw new BadRequestException('Your Workspace name must not contain the following characters: ' . implode(' ', str_split(WorkspaceCheckService::CHARACTERS_SPECIAL)));
Expand All @@ -332,12 +330,11 @@ public function renameSpace(array|string $workspace,

$spaceName = $this->deleteBlankSpaceName($newSpaceName);

$spaceRenamed = $this->spaceService->updateSpaceName($newSpaceName, (int)$workspace['id']);

// TODO Handle API call failure (revert space rename and inform user)
$this->spaceManager->rename($spaceId, $spaceName);

return new JSONResponse([
'statuscode' => Http::STATUS_NO_CONTENT,
'space' => $spaceRenamed,
'space' => $spaceName,
]);
}
}
8 changes: 8 additions & 0 deletions lib/Helper/GroupfolderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@ public function setFolderQuota(int $folderId, int $quota): void {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the setFolderQuota from FolderManager.');
}
}

public function renameFolder(int $folderId, string $newMountPoint):void {
try {
$this->folderManager->renameFolder($folderId, $newMountPoint);
} catch (\Exception $e) {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the renameFolder from FolderManager.');
}
}
}
11 changes: 11 additions & 0 deletions lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,15 @@ public function attachGroup(int $folderId, string $gid): void {
private function deleteBlankSpaceName(string $spaceName): string {
return trim($spaceName);
}

/**
* @param int $spaceId related to the id of a space.
* @param string $newSpaceName related to the new space name.
*/
public function rename(int $spaceId, string $newSpaceName): void {
$space = $this->get($spaceId);

$this->folderHelper->renameFolder($space['groupfolder_id'], $newSpaceName);
$this->spaceMapper->updateSpaceName($newSpaceName, $spaceId);
}
}
5 changes: 3 additions & 2 deletions src/SpaceDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import SelectUsers from './SelectUsers.vue'
import RemoveSpace from './RemoveSpace.vue'
import UserTable from './UserTable.vue'
import { destroy, rename, checkGroupfolderNameExist } from './services/groupfoldersService.js'

Check failure on line 111 in src/SpaceDetails.vue

View workflow job for this annotation

GitHub Actions / lint

'rename' is defined but never used
import { renameSpace } from './services/spaceService.js'
import showNotificationError from './services/Notifications/NotificationError.js'

export default {
Expand Down Expand Up @@ -191,13 +192,13 @@ export default {
// TODO: Change : the key from $root.spaces, groupnames, change the route into new spacename because
// the path is `https://instance-nc/apps/workspace/workspace/Aang`
const oldSpaceName = this.$route.params.space
let responseRename = await rename(this.$store.state.spaces[oldSpaceName], newSpaceName)
let responseRename = await renameSpace(this.$store.state.spaces[oldSpaceName].id, newSpaceName)
responseRename = responseRename.data

if (responseRename.statuscode === 204) {
const space = { ...this.$store.state.spaces[oldSpaceName] }
space.name = responseRename.space
space.groups = responseRename.groups

this.$store.dispatch('updateSpace', {
space,
})
Expand Down
1 change: 1 addition & 0 deletions src/services/groupfoldersService.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export function destroy(workspace) {
* @param {object} workspace it's the object relative to workspace
* @param {string} newSpaceName it's the new name for the workspace
* @return {Promise}
* @deprecated
*/
export function rename(workspace, newSpaceName) {
// Response format to return
Expand Down
35 changes: 35 additions & 0 deletions src/services/spaceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,38 @@ export function addGroupToWorkspace(spaceId, gid) {
throw new AddGroupToGroupfolderError('Error to add Space Manager group in the groupfolder')
})
}

export function renameSpace(spaceId, newSpaceName) {
const respFormat = {
data: {},
}
respFormat.data.statuscode = 500
respFormat.data.message = 'Rename the space is impossible.'

newSpaceName = deleteBlankSpacename(newSpaceName)

const respFormatFinal = axios.patch(generateUrl(`/apps/workspace/spaces/${spaceId}`),
{
newSpaceName,
})
.then(resp => {
if (resp.data.statuscode === 400) {
respFormat.data.statuscode = 400
respFormat.data.space = null
return respFormat
}

if (resp.data.statuscode === 204) {
respFormat.data.statuscode = 204
respFormat.data.space = newSpaceName
return respFormat
}

return respFormat
})
.catch(error => {
console.error('Problem to rename the space', error)
})

return respFormatFinal
}

0 comments on commit 2b65ac5

Please sign in to comment.