diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index 52e954d95..57ff17c80 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -31,6 +31,8 @@ use OCA\Workspace\Exceptions\CreateGroupException; use OCA\Workspace\Exceptions\CreateWorkspaceException; use OCA\Workspace\Exceptions\WorkspaceNameExistException; +use OCA\Workspace\Folder\RootFolder; +use OCA\Workspace\Helper\GroupfolderHelper; use OCA\Workspace\Service\Group\GroupFormatter; use OCA\Workspace\Service\Group\ManagersWorkspace; use OCA\Workspace\Service\Group\UserGroup; @@ -50,7 +52,9 @@ class WorkspaceController extends Controller { public function __construct( IRequest $request, + private GroupfolderHelper $folderHelper, private IGroupManager $groupManager, + private RootFolder $rootFolder, private IUserManager $userManager, private LoggerInterface $logger, private SpaceMapper $spaceMapper, @@ -178,16 +182,34 @@ public function destroy(array $workspace): JSONResponse { */ public function findAll(): JSONResponse { $workspaces = $this->workspaceService->getAll(); + $spaces = []; + foreach ($workspaces as $workspace) { + $space = array_merge( + $workspace, + $this->folderHelper->getFolder( + $workspace['groupfolder_id'], + $this->rootFolder->getRootFolderStorageId() + ) + ); + + $gids = array_keys($space['groups']); + $groups = array_map(fn($gid) => $this->groupManager->get($gid), $gids); + + $space['groups'] = GroupFormatter::formatGroups($groups); + $space['users'] = $this->workspaceService->addUsersInfo($space); + + $spaces[] = $space; + } // We only want to return those workspaces for which the connected user is a manager if (!$this->userService->isUserGeneralAdmin()) { $this->logger->debug('Filtering workspaces'); - $filteredWorkspaces = array_values(array_filter($workspaces, function ($workspace) { - return $this->userService->isSpaceManagerOfSpace($workspace); + $filteredWorkspaces = array_values(array_filter($spaces, function ($space) { + return $this->userService->isSpaceManagerOfSpace($space); })); - $workspaces = $filteredWorkspaces; + $spaces = $filteredWorkspaces; } - return new JSONResponse($workspaces); + return new JSONResponse($spaces); } /** diff --git a/lib/Folder/RootFolder.php b/lib/Folder/RootFolder.php new file mode 100644 index 000000000..9aa629929 --- /dev/null +++ b/lib/Folder/RootFolder.php @@ -0,0 +1,16 @@ +rootFolder->getMountPoint()->getNumericStorageId(); + } +} diff --git a/lib/Service/WorkspaceService.php b/lib/Service/WorkspaceService.php index f1f02fdf5..07399c741 100644 --- a/lib/Service/WorkspaceService.php +++ b/lib/Service/WorkspaceService.php @@ -156,9 +156,6 @@ public function autoComplete(string $term, array|string $space): array { return $data; } - /** - * @return Space[] - all spaces - */ public function getAll(): array { // Gets all spaces $spaces = $this->spaceMapper->findAll(); @@ -171,13 +168,9 @@ public function getAll(): array { } /** - * * Adds users information to a workspace - * - * @param string|array The workspace to which we want to add users info - * */ - public function addUsersInfo(string|array $workspace): array { + public function addUsersInfo(string|array $workspace): \stdClass { // Caution: It is important to add users from the workspace's user group before adding the users // from the workspace's manager group, as users may be members of both groups $this->logger->debug('Adding users information to workspace'); @@ -196,9 +189,8 @@ public function addUsersInfo(string|array $workspace): array { $users[$user->getUID()] = $this->userService->formatUser($user, $workspace, 'admin'); }; } - $workspace['users'] = (object) $users; - return $workspace; + return (object) $users; } /** diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index 8ec179402..df7a77d96 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -29,18 +29,18 @@ use OCA\Workspace\Exceptions\BadRequestException; use OCA\Workspace\Exceptions\CreateWorkspaceException; use OCA\Workspace\Exceptions\WorkspaceNameExistException; +use OCA\Workspace\Folder\RootFolder; use OCA\Workspace\Helper\GroupfolderHelper; use OCA\Workspace\Service\Group\GroupFormatter; use OCA\Workspace\Service\Group\UserGroup; use OCA\Workspace\Service\Group\WorkspaceManagerGroup; use OCA\Workspace\Service\Workspace\WorkspaceCheckService; use OCP\AppFramework\Http; -use OCP\Files\IRootFolder; class SpaceManager { public function __construct( private GroupfolderHelper $folderHelper, - private IRootFolder $rootFolder, + private RootFolder $rootFolder, private WorkspaceCheckService $workspaceCheck, private UserGroup $userGroup, private SpaceMapper $spaceMapper, @@ -103,7 +103,7 @@ public function create(string $spacename): array { $groupfolder = $this->folderHelper->getFolder( $folderId, - $this->getRootFolderStorageId() + $this->rootFolder->getRootFolderStorageId() ); return [ @@ -130,8 +130,4 @@ public function create(string $spacename): array { private function deleteBlankSpaceName(string $spaceName): string { return trim($spaceName); } - - private function getRootFolderStorageId(): ?int { - return $this->rootFolder->getMountPoint()->getNumericStorageId(); - } } diff --git a/src/WorkspaceContent.vue b/src/WorkspaceContent.vue index 09e0d5ce9..a40b69c78 100644 --- a/src/WorkspaceContent.vue +++ b/src/WorkspaceContent.vue @@ -42,7 +42,6 @@ import axios from '@nextcloud/axios' import { generateUrl } from '@nextcloud/router' import showNotificationError from './services/Notifications/NotificationError.js' -import { get, formatGroups, formatUsers } from './services/groupfoldersService.js' import NcAppContent from '@nextcloud/vue/dist/Components/NcAppContent.js' import NcAppContentDetails from '@nextcloud/vue/dist/Components/NcAppContentDetails.js' import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js' @@ -65,18 +64,27 @@ export default { this.$store.state.loading = false return } - this.generateDataCreated(resp.data) - // resp return [ undefined, undefined, undefined, undefined] - // it is serious ? - .then(resp => { - // Finished loading - // When all promises is finished - this.$store.state.loading = false - }) - .catch(error => { - console.error('The generateDataCreated method has a problem in promises', error) - this.$store.state.loading = false + + resp.data.forEach(space => { + let quota = this.convertQuotaForFrontend(space.quota) + if (quota === 'unlimited') { + quota = t('workspace', 'unlimited') + } + + this.$store.commit('addSpace', { + color: space.color_code, + groupfolderId: space.groupfolder_id, + groups: space.groups, + id: space.id, + isOpen: false, + name: space.name, + quota, + users: space.users, }) + }) + + this.$store.state.loading = false + }) .catch((e) => { console.error('Problem to load spaces only', e) @@ -87,70 +95,6 @@ export default { } }, methods: { - // Method to generate the data when this component is created. - // It is necessary to await promises and catch the response to - // stop the loading. - // data object/json from space - generateDataCreated(data) { - // It possible which the data is not an array but an object. - // Because, the `/apps/workspace/spaces` route return an object if there is one element. - if (!Array.isArray(data)) { - data = [data] - } - // loop to build the json final - const result = Promise.all(data.map(async space => { - await get(space.groupfolder_id) - .then((resp) => { - space.acl = resp.acl - space.groups = resp.groups - space.quota = resp.quota - space.size = resp.size - return space - }) - .catch((e) => { - console.error('Impossible to format the spaces', e) - }) - const spaceWithUsers = await formatUsers(space) - .then((resp) => { - return resp.data - }) - .catch((error) => { - console.error('Impossible to generate a space with users format', error) - }) - const spaceWithUsersAndGroups = await formatGroups(spaceWithUsers) - .then((resp) => { - return resp.data - }) - .catch((error) => { - console.error('Impossible to generate a space with groups format', error) - }) - // Initialises the store - let codeColor = spaceWithUsersAndGroups.color_code - if (spaceWithUsersAndGroups.color_code === null) { - codeColor = '#' + (Math.floor(Math.random() * 2 ** 24)).toString(16).padStart(0, 6) - } - let quota = this.convertQuotaForFrontend(spaceWithUsersAndGroups.quota) - if (quota === 'unlimited') { - quota = t('workspace', 'unlimited') - } - // Convert an array empty to object - if (Array.isArray(spaceWithUsersAndGroups.users) - && spaceWithUsersAndGroups.users.length === 0) { - spaceWithUsersAndGroups.users = { } - } - this.$store.commit('addSpace', { - color: codeColor, - groupfolderId: spaceWithUsersAndGroups.groupfolder_id, - groups: spaceWithUsersAndGroups.groups, - id: spaceWithUsersAndGroups.id, - isOpen: false, - name: spaceWithUsersAndGroups.name, - quota, - users: spaceWithUsersAndGroups.users, - }) - })) - return result - }, // Shows a space quota in a user-friendly way convertQuotaForFrontend(quota) { if (quota === -3 || quota === '-3') {