Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Change parent ids for chats and spaces

Hakan Shehu0897c34cc2bb
diff --git a/apps/desktop/src/main/mutations/chats/chat-create.ts b/apps/desktop/src/main/mutations/chats/chat-create.ts
index ba0413cd..0afb2c75 100644
--- a/apps/desktop/src/main/mutations/chats/chat-create.ts
+++ b/apps/desktop/src/main/mutations/chats/chat-create.ts
@@ -42,7 +42,7 @@ export class ChatCreateMutationHandler
const id = generateId(IdType.Chat);
const attributes: ChatAttributes = {
type: 'chat',
- parentId: input.workspaceId,
+ parentId: id,
collaborators: {
[input.userId]: 'admin',
[input.otherUserId]: 'admin',
diff --git a/apps/desktop/src/main/queries/chats/chat-list.ts b/apps/desktop/src/main/queries/chats/chat-list.ts
new file mode 100644
index 00000000..139fd847
--- /dev/null
+++ b/apps/desktop/src/main/queries/chats/chat-list.ts
@@ -0,0 +1,98 @@
+import { ChatEntry } from '@colanode/core';
+
+import { databaseService } from '@/main/data/database-service';
+import { SelectEntry } from '@/main/data/workspace/schema';
+import { ChangeCheckResult, QueryHandler } from '@/main/types';
+import { mapEntry } from '@/main/utils';
+import { ChatListQueryInput } from '@/shared/queries/chats/chat-list';
+import { Event } from '@/shared/types/events';
+
+export class ChatListQueryHandler implements QueryHandler<ChatListQueryInput> {
+ public async handleQuery(input: ChatListQueryInput): Promise<ChatEntry[]> {
+ const rows = await this.fetchChildren(input);
+ return rows.map(mapEntry) as ChatEntry[];
+ }
+
+ public async checkForChanges(
+ event: Event,
+ input: ChatListQueryInput,
+ output: ChatEntry[]
+ ): Promise<ChangeCheckResult<ChatListQueryInput>> {
+ if (
+ event.type === 'workspace_deleted' &&
+ event.workspace.userId === input.userId
+ ) {
+ return {
+ hasChanges: true,
+ result: [],
+ };
+ }
+
+ if (
+ event.type === 'entry_created' &&
+ event.userId === input.userId &&
+ event.entry.type === 'chat'
+ ) {
+ const newChildren = [...output, event.entry];
+ return {
+ hasChanges: true,
+ result: newChildren,
+ };
+ }
+
+ if (
+ event.type === 'entry_updated' &&
+ event.userId === input.userId &&
+ event.entry.type === 'chat'
+ ) {
+ const entry = output.find((entry) => entry.id === event.entry.id);
+ if (entry) {
+ const newChildren = output.map((entry) =>
+ entry.id === event.entry.id ? (event.entry as ChatEntry) : entry
+ );
+
+ return {
+ hasChanges: true,
+ result: newChildren,
+ };
+ }
+ }
+
+ if (
+ event.type === 'entry_deleted' &&
+ event.userId === input.userId &&
+ event.entry.type === 'chat'
+ ) {
+ const entry = output.find((entry) => entry.id === event.entry.id);
+ if (entry) {
+ const newChildren = output.filter(
+ (entry) => entry.id !== event.entry.id
+ );
+ return {
+ hasChanges: true,
+ result: newChildren,
+ };
+ }
+ }
+
+ return {
+ hasChanges: false,
+ };
+ }
+
+ private async fetchChildren(
+ input: ChatListQueryInput
+ ): Promise<SelectEntry[]> {
+ const workspaceDatabase = await databaseService.getWorkspaceDatabase(
+ input.userId
+ );
+
+ const rows = await workspaceDatabase
+ .selectFrom('entries')
+ .selectAll()
+ .where('type', '=', 'chat')
+ .execute();
+
+ return rows;
+ }
+}
diff --git a/apps/desktop/src/main/queries/entries/entry-tree-get.ts b/apps/desktop/src/main/queries/entries/entry-tree-get.ts
index 76631f24..7104dc7a 100644
--- a/apps/desktop/src/main/queries/entries/entry-tree-get.ts
+++ b/apps/desktop/src/main/queries/entries/entry-tree-get.ts
@@ -96,9 +96,11 @@ export class EntryTreeGetQueryHandler
while (entry) {
result.unshift(entry);
- entry = rows.find((row) => row.id === entry?.parent_id);
+ entry = rows.find(
+ (row) => row.id !== entry?.id && row.id === entry?.parent_id
+ );
- if (!entry || entry.id === entry.parent_id) {
+ if (!entry) {
break;
}
}
diff --git a/apps/desktop/src/main/queries/index.ts b/apps/desktop/src/main/queries/index.ts
index 6cea71c9..687363e7 100644
--- a/apps/desktop/src/main/queries/index.ts
+++ b/apps/desktop/src/main/queries/index.ts
@@ -20,6 +20,8 @@ import { UserListQueryHandler } from '@/main/queries/users/user-list';
import { DatabaseListQueryHandler } from '@/main/queries/databases/database-list';
import { RecordSearchQueryHandler } from '@/main/queries/records/record-search';
import { UserGetQueryHandler } from '@/main/queries/users/user-get';
+import { SpaceListQueryHandler } from '@/main/queries/spaces/space-list';
+import { ChatListQueryHandler } from '@/main/queries/chats/chat-list';
import { QueryHandler } from '@/main/types';
import { QueryMap } from '@/shared/queries';
@@ -50,4 +52,6 @@ export const queryHandlerMap: QueryHandlerMap = {
record_search: new RecordSearchQueryHandler(),
user_get: new UserGetQueryHandler(),
file_get: new FileGetQueryHandler(),
+ chat_list: new ChatListQueryHandler(),
+ space_list: new SpaceListQueryHandler(),
};
diff --git a/apps/desktop/src/main/queries/spaces/space-list.ts b/apps/desktop/src/main/queries/spaces/space-list.ts
new file mode 100644
index 00000000..f4a5b41d
--- /dev/null
+++ b/apps/desktop/src/main/queries/spaces/space-list.ts
@@ -0,0 +1,100 @@
+import { SpaceEntry } from '@colanode/core';
+
+import { databaseService } from '@/main/data/database-service';
+import { SelectEntry } from '@/main/data/workspace/schema';
+import { ChangeCheckResult, QueryHandler } from '@/main/types';
+import { mapEntry } from '@/main/utils';
+import { SpaceListQueryInput } from '@/shared/queries/spaces/space-list';
+import { Event } from '@/shared/types/events';
+
+export class SpaceListQueryHandler
+ implements QueryHandler<SpaceListQueryInput>
+{
+ public async handleQuery(input: SpaceListQueryInput): Promise<SpaceEntry[]> {
+ const rows = await this.fetchChildren(input);
+ return rows.map(mapEntry) as SpaceEntry[];
+ }
+
+ public async checkForChanges(
+ event: Event,
+ input: SpaceListQueryInput,
+ output: SpaceEntry[]
+ ): Promise<ChangeCheckResult<SpaceListQueryInput>> {
+ if (
+ event.type === 'workspace_deleted' &&
+ event.workspace.userId === input.userId
+ ) {
+ return {
+ hasChanges: true,
+ result: [],
+ };
+ }
+
+ if (
+ event.type === 'entry_created' &&
+ event.userId === input.userId &&
+ event.entry.type === 'space'
+ ) {
+ const newChildren = [...output, event.entry];
+ return {
+ hasChanges: true,
+ result: newChildren,
+ };
+ }
+
+ if (
+ event.type === 'entry_updated' &&
+ event.userId === input.userId &&
+ event.entry.type === 'space'
+ ) {
+ const entry = output.find((entry) => entry.id === event.entry.id);
+ if (entry) {
+ const newChildren = output.map((entry) =>
+ entry.id === event.entry.id ? (event.entry as SpaceEntry) : entry
+ );
+
+ return {
+ hasChanges: true,
+ result: newChildren,
+ };
+ }
+ }
+
+ if (
+ event.type === 'entry_deleted' &&
+ event.userId === input.userId &&
+ event.entry.type === 'space'
+ ) {
+ const entry = output.find((entry) => entry.id === event.entry.id);
+ if (entry) {
+ const newChildren = output.filter(
+ (entry) => entry.id !== event.entry.id
+ );
+ return {
+ hasChanges: true,
+ result: newChildren,
+ };
+ }
+ }
+
+ return {
+ hasChanges: false,
+ };
+ }
+
+ private async fetchChildren(
+ input: SpaceListQueryInput
+ ): Promise<SelectEntry[]> {
+ const workspaceDatabase = await databaseService.getWorkspaceDatabase(
+ input.userId
+ );
+
+ const rows = await workspaceDatabase
+ .selectFrom('entries')
+ .selectAll()
+ .where('type', '=', 'space')
+ .execute();
+
+ return rows;
+ }
+}
diff --git a/apps/desktop/src/renderer/components/layouts/layout-sidebar-chats.tsx b/apps/desktop/src/renderer/components/layouts/layout-sidebar-chats.tsx
index e6ab933d..982d4b2b 100644
--- a/apps/desktop/src/renderer/components/layouts/layout-sidebar-chats.tsx
+++ b/apps/desktop/src/renderer/components/layouts/layout-sidebar-chats.tsx
@@ -1,5 +1,3 @@
-import { ChatEntry } from '@colanode/core';
-
import { ChatCreatePopover } from '@/renderer/components/chats/chat-create-popover';
import { ChatSidebarItem } from '@/renderer/components/chats/chat-sidebar-item';
import { useWorkspace } from '@/renderer/contexts/workspace';
@@ -10,13 +8,14 @@ export const LayoutSidebarChats = () => {
const workspace = useWorkspace();
const { data } = useQuery({
- type: 'entry_children_get',
+ type: 'chat_list',
userId: workspace.userId,
- entryId: workspace.id,
- types: ['chat'],
+ parentId: workspace.id,
+ page: 0,
+ count: 100,
});
- const chats = data?.map((entry) => entry as ChatEntry) ?? [];
+ const chats = data ?? [];
return (
<div className="group/sidebar-chats flex w-full min-w-0 flex-col p-2">
diff --git a/apps/desktop/src/renderer/components/layouts/layout-sidebar-spaces.tsx b/apps/desktop/src/renderer/components/layouts/layout-sidebar-spaces.tsx
index 4846ca6a..a986fca8 100644
--- a/apps/desktop/src/renderer/components/layouts/layout-sidebar-spaces.tsx
+++ b/apps/desktop/src/renderer/components/layouts/layout-sidebar-spaces.tsx
@@ -1,5 +1,3 @@
-import { SpaceEntry } from '@colanode/core';
-
import { SpaceCreateButton } from '@/renderer/components/spaces/space-create-button';
import { SpaceSidebarItem } from '@/renderer/components/spaces/space-sidebar-item';
import { useWorkspace } from '@/renderer/contexts/workspace';
@@ -11,13 +9,14 @@ export const LayoutSidebarSpaces = () => {
workspace.role !== 'guest' && workspace.role !== 'none';
const { data } = useQuery({
- type: 'entry_children_get',
+ type: 'space_list',
userId: workspace.userId,
- entryId: workspace.id,
- types: ['space'],
+ parentId: workspace.id,
+ page: 0,
+ count: 100,
});
- const spaces = data?.map((entry) => entry as SpaceEntry) ?? [];
+ const spaces = data ?? [];
return (
<div className="group/sidebar-spaces flex w-full min-w-0 flex-col p-2">
diff --git a/apps/desktop/src/shared/queries/chats/chat-list.ts b/apps/desktop/src/shared/queries/chats/chat-list.ts
new file mode 100644
index 00000000..56ff2712
--- /dev/null
+++ b/apps/desktop/src/shared/queries/chats/chat-list.ts
@@ -0,0 +1,17 @@
+import { ChatEntry } from '@colanode/core';
+
+export type ChatListQueryInput = {
+ type: 'chat_list';
+ page: number;
+ count: number;
+ userId: string;
+};
+
+declare module '@/shared/queries' {
+ interface QueryMap {
+ chat_list: {
+ input: ChatListQueryInput;
+ output: ChatEntry[];
+ };
+ }
+}
diff --git a/apps/desktop/src/shared/queries/spaces/space-list.ts b/apps/desktop/src/shared/queries/spaces/space-list.ts
new file mode 100644
index 00000000..4825c04f
--- /dev/null
+++ b/apps/desktop/src/shared/queries/spaces/space-list.ts
@@ -0,0 +1,17 @@
+import { SpaceEntry } from '@colanode/core';
+
+export type SpaceListQueryInput = {
+ type: 'space_list';
+ page: number;
+ count: number;
+ userId: string;
+};
+
+declare module '@/shared/queries' {
+ interface QueryMap {
+ space_list: {
+ input: SpaceListQueryInput;
+ output: SpaceEntry[];
+ };
+ }
+}
diff --git a/apps/server/src/services/workspace-service.ts b/apps/server/src/services/workspace-service.ts
index 23d40fe2..ca626590 100644
--- a/apps/server/src/services/workspace-service.ts
+++ b/apps/server/src/services/workspace-service.ts
@@ -70,7 +70,7 @@ class WorkspaceService {
type: 'space',
name: 'Home',
description: 'This is your home space.',
- parentId: workspaceId,
+ parentId: spaceId,
collaborators: {
[userId]: 'admin',
},