Skip to content

Commit e10a05b

Browse files
Avoid creating circular dependencies in drive folders (#10193)
Signed-off-by: Artem Savchenko <[email protected]>
1 parent ca83fa2 commit e10a05b

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

plugins/drive-resources/src/components/MoveResource.svelte

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@
1515
<script lang="ts">
1616
import { Ref } from '@hcengineering/core'
1717
import type { Drive, Folder, Resource } from '@hcengineering/drive'
18-
import presentation, { Card, getClient, SpaceSelector } from '@hcengineering/presentation'
18+
import presentation, { Card, SpaceSelector } from '@hcengineering/presentation'
1919
import view from '@hcengineering/view'
2020
import { ObjectBox } from '@hcengineering/view-resources'
2121
import { createEventDispatcher } from 'svelte'
2222
2323
import drive from '../plugin'
24-
import { moveResources } from '../utils'
24+
import { findAllChildren, moveResources } from '../utils'
2525
2626
import ResourcePresenter from './ResourcePresenter.svelte'
2727
2828
export let value: Resource
2929
30-
const client = getClient()
31-
const hierarchy = client.getHierarchy()
3230
const dispatch = createEventDispatcher()
3331
3432
let space: Ref<Drive> = value.space as Ref<Drive>
@@ -42,21 +40,7 @@
4240
$: void updateChildren(value)
4341
4442
async function updateChildren (resource: Resource): Promise<void> {
45-
children = await findChildren(resource)
46-
}
47-
48-
async function findChildren (resource: Resource): Promise<Array<Ref<Folder>>> {
49-
if (hierarchy.isDerived(resource._class, drive.class.Folder)) {
50-
const children = await client.findAll(
51-
drive.class.Folder,
52-
{ space: resource.space, path: resource._id as Ref<Folder> },
53-
{ projection: { _id: 1 } }
54-
)
55-
56-
return children.map((p) => p._id)
57-
}
58-
59-
return []
43+
children = await findAllChildren(resource)
6044
}
6145
6246
$: canSave = space !== value.space || parent !== value.parent

plugins/drive-resources/src/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,38 @@ export async function resolveParents (object: Resource): Promise<Doc[]> {
169169
return parents.reverse()
170170
}
171171

172+
export async function findAllChildren (resource: Resource, maxDepth: number = 10): Promise<Array<Ref<Folder>>> {
173+
const client = getClient()
174+
const hierarchy = client.getHierarchy()
175+
176+
if (!hierarchy.isDerived(resource._class, drive.class.Folder)) {
177+
return []
178+
}
179+
180+
const allChildren: Array<Ref<Folder>> = []
181+
let currentLevel: Array<Ref<Folder>> = [resource._id as Ref<Folder>]
182+
let depth = 0
183+
184+
while (currentLevel.length > 0 && depth < maxDepth) {
185+
const children = await client.findAll(
186+
drive.class.Folder,
187+
{ space: resource.space, parent: { $in: currentLevel } },
188+
{ projection: { _id: 1 } }
189+
)
190+
191+
if (children.length === 0) {
192+
break
193+
}
194+
195+
const childIds = children.map((p) => p._id)
196+
allChildren.push(...childIds)
197+
currentLevel = childIds
198+
depth++
199+
}
200+
201+
return allChildren
202+
}
203+
172204
export async function getUploadOptionsByFragment (space: Ref<Drive>, fragment: string): Promise<FileUploadOptions> {
173205
const [, _id, _class] = decodeURIComponent(fragment).split('|')
174206
if (_class === drive.class.Folder) {

0 commit comments

Comments
 (0)