Skip to content

Commit 9e8e0d7

Browse files
committed
refactor(namespace-resources): update listChildren
1 parent 537f369 commit 9e8e0d7

File tree

5 files changed

+94
-16
lines changed

5 files changed

+94
-16
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Expose } from 'class-transformer';
2+
import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto';
3+
4+
export class ListChildrenRespDto extends ResourceMetaDto {
5+
@Expose({ name: 'has_children' })
6+
hasChildren: boolean;
7+
8+
constructor(resourceMeta: ResourceMetaDto, hasChildren: boolean) {
9+
super();
10+
Object.assign(this, resourceMeta);
11+
this.hasChildren = hasChildren;
12+
}
13+
}

src/namespace-resources/namespace-resources.controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { UserId } from 'omniboxd/decorators/user-id.decorator';
1919
import { Request } from 'express';
2020
import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto';
21+
import { ListChildrenRespDto } from './dto/list-children-resp.dto';
2122

2223
@Controller('api/v1/namespaces/:namespaceId/resources')
2324
export class NamespaceResourcesController {
@@ -91,8 +92,8 @@ export class NamespaceResourcesController {
9192
@UserId() userId: string,
9293
@Param('namespaceId') namespaceId: string,
9394
@Param('resourceId') resourceId: string,
94-
): Promise<ResourceMetaDto[]> {
95-
return this.namespaceResourcesService.getAndFilterSubResources(
95+
): Promise<ListChildrenRespDto[]> {
96+
return this.namespaceResourcesService.listChildren(
9697
namespaceId,
9798
resourceId,
9899
userId,

src/namespace-resources/namespace-resources.service.ts

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { TagDto } from 'omniboxd/tag/dto/tag.dto';
4040
import { ResourceAttachmentsService } from 'omniboxd/resource-attachments/resource-attachments.service';
4141
import { ResourcesService } from 'omniboxd/resources/resources.service';
4242
import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto';
43+
import { ListChildrenRespDto } from './dto/list-children-resp.dto';
4344

4445
const TASK_PRIORITY = 5;
4546

@@ -129,6 +130,28 @@ export class NamespaceResourcesService {
129130
return resources.map((resource) => resource.id);
130131
}
131132

133+
private async hasChildren(
134+
namespaceId: string,
135+
parents: ResourceMetaDto[],
136+
userId: string,
137+
): Promise<boolean> {
138+
const children = await this.resourcesService.getSubResources(
139+
namespaceId,
140+
parents[0].id,
141+
);
142+
for (const child of children) {
143+
const permission = await this.permissionsService.getCurrentPermission(
144+
namespaceId,
145+
[child, ...parents],
146+
userId,
147+
);
148+
if (permission !== ResourcePermission.NO_ACCESS) {
149+
return true;
150+
}
151+
}
152+
return false;
153+
}
154+
132155
async findByIds(namespaceId: string, ids: Array<string>) {
133156
if (ids.length <= 0) {
134157
return [];
@@ -385,7 +408,7 @@ export class NamespaceResourcesService {
385408
};
386409
// Self and child exclusions
387410
if (excludeResourceId) {
388-
const resourceChildren = await this.getAndFilterSubResources(
411+
const resourceChildren = await this.getSubResourcesByUser(
389412
namespaceId,
390413
excludeResourceId,
391414
userId,
@@ -410,7 +433,7 @@ export class NamespaceResourcesService {
410433
return filteredResources.map((res) => ResourceMetaDto.fromEntity(res));
411434
}
412435

413-
async getAndFilterSubResources(
436+
async getSubResourcesByUser(
414437
namespaceId: string,
415438
resourceId: string,
416439
userId: string,
@@ -420,6 +443,25 @@ export class NamespaceResourcesService {
420443
namespaceId,
421444
resourceId,
422445
);
446+
const children = await this.getSubResourcesByParents(
447+
namespaceId,
448+
parents,
449+
userId,
450+
);
451+
if (includeParent) {
452+
return [parents[0], ...children];
453+
}
454+
return children;
455+
}
456+
457+
async getSubResourcesByParents(
458+
namespaceId: string,
459+
parents: ResourceMetaDto[],
460+
userId: string,
461+
): Promise<ResourceMetaDto[]> {
462+
if (!parents) {
463+
return [];
464+
}
423465
const permission = await this.permissionsService.getCurrentPermission(
424466
namespaceId,
425467
parents,
@@ -430,7 +472,7 @@ export class NamespaceResourcesService {
430472
}
431473
const children = await this.resourcesService.getSubResources(
432474
namespaceId,
433-
resourceId,
475+
parents[0].id,
434476
);
435477
const filteredChildren: ResourceMetaDto[] = [];
436478
for (const child of children) {
@@ -443,12 +485,35 @@ export class NamespaceResourcesService {
443485
filteredChildren.push(child);
444486
}
445487
}
446-
if (includeParent) {
447-
return [parents[0], ...filteredChildren];
448-
}
449488
return filteredChildren;
450489
}
451490

491+
async listChildren(
492+
namespaceId: string,
493+
resourceId: string,
494+
userId: string,
495+
): Promise<ListChildrenRespDto[]> {
496+
const parents = await this.resourcesService.getParentResources(
497+
namespaceId,
498+
resourceId,
499+
);
500+
const children = await this.getSubResourcesByParents(
501+
namespaceId,
502+
parents,
503+
userId,
504+
);
505+
const resps: ListChildrenRespDto[] = [];
506+
for (const child of children) {
507+
const hasChildren = await this.hasChildren(
508+
namespaceId,
509+
[child, ...parents],
510+
userId,
511+
);
512+
resps.push(new ListChildrenRespDto(child, hasChildren));
513+
}
514+
return resps;
515+
}
516+
452517
async getSpaceType(
453518
namespaceId: string,
454519
rootResourceId: string,

src/namespaces/namespaces.service.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,17 @@ export class NamespacesService {
305305

306306
async getRoot(namespaceId: string, userId: string) {
307307
const privateRoot = await this.getPrivateRoot(userId, namespaceId);
308-
const privateChildren = await this.resourceService.getAndFilterSubResources(
308+
const privateChildren = await this.resourceService.getSubResourcesByUser(
309309
namespaceId,
310310
privateRoot.id,
311311
userId,
312312
);
313313
const teamspaceRoot = await this.getTeamspaceRoot(namespaceId);
314-
const teamspaceChildren =
315-
await this.resourceService.getAndFilterSubResources(
316-
namespaceId,
317-
teamspaceRoot.id,
318-
userId,
319-
);
314+
const teamspaceChildren = await this.resourceService.getSubResourcesByUser(
315+
namespaceId,
316+
teamspaceRoot.id,
317+
userId,
318+
);
320319
return {
321320
private: { ...privateRoot, parentId: '0', children: privateChildren },
322321
teamspace: {

src/wizard/stream.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class StreamService {
250250
for (const resource of tool.resources) {
251251
if (resource.type === 'folder') {
252252
const resources =
253-
await this.namespaceResourcesService.getAndFilterSubResources(
253+
await this.namespaceResourcesService.getSubResourcesByUser(
254254
tool.namespace_id,
255255
resource.id,
256256
user.id,

0 commit comments

Comments
 (0)