diff options
| author | Mohamed Bassem <me@mbassem.com> | 2025-09-28 17:49:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-28 17:49:20 +0100 |
| commit | 7d0b414f1f5681dcc73254fe97cb67de4c0cb748 (patch) | |
| tree | b76d5f88fe9b35bb60c8dbfb07effa5b23fa977d /packages/trpc/models/lists.ts | |
| parent | ed1f24f2df639786a7e6f6ef8951c0d9197f57ff (diff) | |
| download | karakeep-7d0b414f1f5681dcc73254fe97cb67de4c0cb748.tar.zst | |
feat: recursive list delete (#1989)
Diffstat (limited to 'packages/trpc/models/lists.ts')
| -rw-r--r-- | packages/trpc/models/lists.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/packages/trpc/models/lists.ts b/packages/trpc/models/lists.ts index c0e17bfc..48da5ed1 100644 --- a/packages/trpc/models/lists.ts +++ b/packages/trpc/models/lists.ts @@ -215,6 +215,41 @@ export abstract class List implements PrivacyAware { } } + async getChildren(): Promise<(ManualList | SmartList)[]> { + const lists = await List.getAll(this.ctx); + const listById = new Map(lists.map((l) => [l.list.id, l])); + + const adjecencyList = new Map<string, string[]>(); + + // Initialize all lists with empty arrays first + lists.forEach((l) => { + adjecencyList.set(l.list.id, []); + }); + + // Then populate the parent-child relationships + lists.forEach((l) => { + if (l.list.parentId) { + const currentChildren = adjecencyList.get(l.list.parentId) ?? []; + currentChildren.push(l.list.id); + adjecencyList.set(l.list.parentId, currentChildren); + } + }); + + const resultIds: string[] = []; + const queue: string[] = [this.list.id]; + + while (queue.length > 0) { + const id = queue.pop()!; + const children = adjecencyList.get(id) ?? []; + children.forEach((childId) => { + queue.push(childId); + resultIds.push(childId); + }); + } + + return resultIds.map((id) => listById.get(id)!); + } + async update( input: z.infer<typeof zEditBookmarkListSchemaWithValidation>, ): Promise<void> { |
