Skip to content

Commit

Permalink
refactor(php,vue): Get all workspaces from Backend
Browse files Browse the repository at this point in the history
We can get all workspaces from the backend.

Signed-off-by: Baptiste Fotia <[email protected]>
  • Loading branch information
zak39 committed Nov 20, 2023
1 parent 0f85f59 commit 3faedc3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 97 deletions.
30 changes: 26 additions & 4 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/Folder/RootFolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OCA\Workspace\Folder;

use OCP\Files\IRootFolder;

class RootFolder
{
public function __construct(private IRootFolder $rootFolder)
{
}

public function getRootFolderStorageId(): ?int {
return $this->rootFolder->getMountPoint()->getNumericStorageId();
}
}
12 changes: 2 additions & 10 deletions lib/Service/WorkspaceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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;
}

/**
Expand Down
10 changes: 3 additions & 7 deletions lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -103,7 +103,7 @@ public function create(string $spacename): array {

$groupfolder = $this->folderHelper->getFolder(
$folderId,
$this->getRootFolderStorageId()
$this->rootFolder->getRootFolderStorageId()
);

return [
Expand All @@ -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();
}
}
96 changes: 20 additions & 76 deletions src/WorkspaceContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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') {
Expand Down

0 comments on commit 3faedc3

Please sign in to comment.