@@ -33,12 +33,14 @@ import { PermissionsService } from 'omniboxd/permissions/permissions.service';
3333import { PrivateSearchResourceDto } from 'omniboxd/wizard/dto/agent-request.dto' ;
3434import { ResourcePermission } from 'omniboxd/permissions/resource-permission.enum' ;
3535import { Response } from 'express' ;
36- import { ResourceDto , ResourceMetaDto , SpaceType } from './dto/resource.dto' ;
36+ import { ResourceDto , SpaceType } from './dto/resource.dto' ;
3737import { Namespace } from 'omniboxd/namespaces/entities/namespace.entity' ;
3838import { TagService } from 'omniboxd/tag/tag.service' ;
3939import { TagDto } from 'omniboxd/tag/dto/tag.dto' ;
4040import { ResourceAttachmentsService } from 'omniboxd/resource-attachments/resource-attachments.service' ;
4141import { ResourcesService } from 'omniboxd/resources/resources.service' ;
42+ import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto' ;
43+ import { ListChildrenRespDto } from './dto/list-children-resp.dto' ;
4244
4345const TASK_PRIORITY = 5 ;
4446
@@ -128,6 +130,28 @@ export class NamespaceResourcesService {
128130 return resources . map ( ( resource ) => resource . id ) ;
129131 }
130132
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+
131155 async findByIds ( namespaceId : string , ids : Array < string > ) {
132156 if ( ids . length <= 0 ) {
133157 return [ ] ;
@@ -384,13 +408,17 @@ export class NamespaceResourcesService {
384408 } ;
385409 // Self and child exclusions
386410 if ( excludeResourceId ) {
387- const resourceChildren = await this . getAllSubResources (
411+ const resourceChildren = await this . getSubResourcesByUser (
388412 namespaceId ,
389413 excludeResourceId ,
390- '' ,
391- true ,
414+ userId ,
415+ ) ;
416+ where . id = Not (
417+ In ( [
418+ excludeResourceId ,
419+ ...resourceChildren . map ( ( children ) => children . id ) ,
420+ ] ) ,
392421 ) ;
393- where . id = Not ( In ( resourceChildren . map ( ( children ) => children . id ) ) ) ;
394422 }
395423 if ( name ) {
396424 where . name = Like ( `%${ name } %` ) ;
@@ -406,90 +434,84 @@ export class NamespaceResourcesService {
406434 userId ,
407435 resources ,
408436 ) ;
409-
410- // Load tags for filtered resources
411- const tagsMap = await this . getTagsForResources (
412- namespaceId ,
413- filteredResources ,
414- ) ;
415-
416- return filteredResources . map ( ( res ) =>
417- ResourceMetaDto . fromEntity ( res , tagsMap . get ( res . id ) || [ ] ) ,
418- ) ;
419- }
420-
421- async getAllSubResources (
422- namespaceId : string ,
423- parentId : string ,
424- userId ?: string ,
425- includeRoot : boolean = false ,
426- ) : Promise < Resource [ ] > {
427- let resources : Resource [ ] = [ await this . get ( parentId ) ] ;
428- for ( const res of resources ) {
429- const subResources : Resource [ ] = await this . query ( namespaceId , res . id ) ;
430- resources . push ( ...subResources ) ;
431- }
432- resources = includeRoot ? resources : resources . slice ( 1 ) ;
433- return userId
434- ? await this . permissionFilter ( namespaceId , userId , resources )
435- : resources ;
437+ return filteredResources . map ( ( res ) => ResourceMetaDto . fromEntity ( res ) ) ;
436438 }
437439
438- async listChildren (
440+ async getSubResourcesByUser (
439441 namespaceId : string ,
440442 resourceId : string ,
441443 userId : string ,
442444 ) : Promise < ResourceMetaDto [ ] > {
443- const parentResources = await this . resourcesService . getParentResources (
445+ const parents = await this . resourcesService . getParentResources (
444446 namespaceId ,
445447 resourceId ,
446448 ) ;
449+ const children = await this . getSubResourcesByParents (
450+ namespaceId ,
451+ parents ,
452+ userId ,
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+ }
447465 const permission = await this . permissionsService . getCurrentPermission (
448466 namespaceId ,
449- parentResources ,
467+ parents ,
450468 userId ,
451469 ) ;
452470 if ( permission === ResourcePermission . NO_ACCESS ) {
453471 return [ ] ;
454472 }
455-
456- const children = await this . resourceRepository . find ( {
457- select : [
458- 'id' ,
459- 'name' ,
460- 'parentId' ,
461- 'resourceType' ,
462- 'attrs' ,
463- 'tagIds' ,
464- 'createdAt' ,
465- 'updatedAt' ,
466- ] ,
467- where : {
468- namespaceId,
469- parentId : resourceId ,
470- } ,
471- } ) ;
472-
473- const filteredChildren : Resource [ ] = [ ] ;
473+ const children = await this . resourcesService . getSubResources (
474+ namespaceId ,
475+ parents [ 0 ] . id ,
476+ ) ;
477+ const filteredChildren : ResourceMetaDto [ ] = [ ] ;
474478 for ( const child of children ) {
475479 const permission = await this . permissionsService . getCurrentPermission (
476480 namespaceId ,
477- [ child , ...parentResources ] ,
481+ [ child , ...parents ] ,
478482 userId ,
479483 ) ;
480484 if ( permission !== ResourcePermission . NO_ACCESS ) {
481485 filteredChildren . push ( child ) ;
482486 }
483487 }
484- // Load tags for filtered children
485- const tagsMap = await this . getTagsForResources (
488+ return filteredChildren ;
489+ }
490+
491+ async listChildren (
492+ namespaceId : string ,
493+ resourceId : string ,
494+ userId : string ,
495+ ) : Promise < ListChildrenRespDto [ ] > {
496+ const parents = await this . resourcesService . getParentResources (
486497 namespaceId ,
487- filteredChildren ,
498+ resourceId ,
488499 ) ;
489-
490- return filteredChildren . map ( ( r ) =>
491- ResourceMetaDto . fromEntity ( r , tagsMap . get ( r . id ) || [ ] ) ,
500+ const children = await this . getSubResourcesByParents (
501+ namespaceId ,
502+ parents ,
503+ userId ,
492504 ) ;
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 ;
493515 }
494516
495517 async getSpaceType (
@@ -528,24 +550,20 @@ export class NamespaceResourcesService {
528550 : resourceId ;
529551 const spaceType = await this . getSpaceType ( namespaceId , rootResourceId ) ;
530552
553+ const resourceMeta = ResourceMetaDto . fromEntity ( resource ) ;
531554 const curPermission = await this . permissionsService . getCurrentPermission (
532555 namespaceId ,
533- [ resource , ...parentResources ] ,
556+ [ resourceMeta , ...parentResources ] ,
534557 userId ,
535558 ) ;
536559
537560 if ( curPermission === ResourcePermission . NO_ACCESS ) {
538561 throw new ForbiddenException ( 'Not authorized' ) ;
539562 }
540563
541- // Load tags for resource and path
542- const allResources = [ resource , ...parentResources ] ;
543- const tagsMap = await this . getTagsForResources ( namespaceId , allResources ) ;
544-
545- const path = [ resource , ...parentResources ]
546- . map ( ( r ) => ResourceMetaDto . fromEntity ( r , tagsMap . get ( r . id ) || [ ] ) )
547- . reverse ( ) ;
548-
564+ // Load tags of the resource
565+ const tagsMap = await this . getTagsForResources ( namespaceId , [ resource ] ) ;
566+ const path = [ resourceMeta , ...parentResources ] . reverse ( ) ;
549567 return ResourceDto . fromEntity (
550568 resource ,
551569 curPermission ,
0 commit comments