Skip to content

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

Refactor queries and types

Hakan Shehufcb6165393a6
diff --git a/apps/desktop/src/main/handlers/queries/breadcrumb-list.ts b/apps/desktop/src/main/handlers/queries/breadcrumb-list.ts
deleted file mode 100644
index 56e4f9e7..00000000
--- a/apps/desktop/src/main/handlers/queries/breadcrumb-list.ts
+++ /dev/null
@@ -1,126 +0,0 @@
-import { BreadcrumbListQueryInput } from '@/operations/queries/breadcrumb-list';
-import { databaseManager } from '@/main/data/database-manager';
-import { sql } from 'kysely';
-import { SelectNode } from '@/main/data/workspace/schema';
-import { BreadcrumbNode } from '@/types/workspaces';
-import { mapNode } from '@/main/utils';
-import { isEqual } from 'lodash-es';
-import {
- MutationChange,
- ChangeCheckResult,
- QueryHandler,
- QueryResult,
-} from '@/main/types';
-import { NodeTypes } from '@colanode/core';
-
-export class BreadcrumbListQueryHandler
- implements QueryHandler<BreadcrumbListQueryInput>
-{
- public async handleQuery(
- input: BreadcrumbListQueryInput
- ): Promise<QueryResult<BreadcrumbListQueryInput>> {
- const rows = await this.fetchNodes(input);
- return {
- output: this.buildBreadcrumbNodes(input.nodeId, rows),
- state: {
- rows,
- },
- };
- }
-
- public async checkForChanges(
- changes: MutationChange[],
- input: BreadcrumbListQueryInput,
- state: Record<string, any>
- ): Promise<ChangeCheckResult<BreadcrumbListQueryInput>> {
- if (
- !changes.some(
- (change) =>
- change.type === 'workspace' &&
- change.table === 'nodes' &&
- change.userId === input.userId
- )
- ) {
- return {
- hasChanges: false,
- };
- }
-
- const rows = await this.fetchNodes(input);
- if (isEqual(rows, state.rows)) {
- return {
- hasChanges: false,
- };
- }
-
- return {
- hasChanges: true,
- result: {
- output: this.buildBreadcrumbNodes(input.nodeId, rows),
- state: {
- rows,
- },
- },
- };
- }
-
- private async fetchNodes(
- input: BreadcrumbListQueryInput
- ): Promise<SelectNode[]> {
- const workspaceDatabase = await databaseManager.getWorkspaceDatabase(
- input.userId
- );
-
- const query = sql<SelectNode>`
- WITH RECURSIVE breadcrumb_nodes AS (
- SELECT *
- FROM nodes
- WHERE id = ${input.nodeId}
- UNION ALL
- SELECT n.*
- FROM nodes n
- INNER JOIN breadcrumb_nodes b ON n.id = b.parent_id
- WHERE n.type <> ${NodeTypes.Workspace}
- )
- SELECT n.*
- FROM breadcrumb_nodes n;
- `.compile(workspaceDatabase);
-
- const result = await workspaceDatabase.executeQuery(query);
- return result.rows;
- }
-
- private buildBreadcrumbNodes = (
- nodeId: string,
- rows: SelectNode[]
- ): BreadcrumbNode[] => {
- const breadcrumbNodes: BreadcrumbNode[] = [];
-
- let currentId: string | null = nodeId;
- while (currentId !== null && currentId !== undefined) {
- const row = rows.find((row) => row.id === currentId);
- if (row && row.parent_id) {
- breadcrumbNodes.push(this.buildBreadcrumbNode(row));
- currentId = row.parent_id;
- } else {
- currentId = null;
- }
- }
- return breadcrumbNodes.reverse();
- };
-
- private buildBreadcrumbNode = (row: SelectNode): BreadcrumbNode => {
- const node = mapNode(row);
-
- const attributes = node.attributes as any;
- const name = attributes.name;
- const avatar = attributes.avatar;
-
- return {
- id: node.id,
- type: node.type,
- name,
- avatar,
- };
- };
-}
diff --git a/apps/desktop/src/main/handlers/queries/file-get.ts b/apps/desktop/src/main/handlers/queries/file-get.ts
deleted file mode 100644
index a49bda7f..00000000
--- a/apps/desktop/src/main/handlers/queries/file-get.ts
+++ /dev/null
@@ -1,127 +0,0 @@
-import { FileGetQueryInput } from '@/operations/queries/file-get';
-import { databaseManager } from '@/main/data/database-manager';
-import {
- MutationChange,
- ChangeCheckResult,
- QueryHandler,
- QueryResult,
-} from '@/main/types';
-import { isEqual } from 'lodash-es';
-import { FileDetails } from '@/types/files';
-
-interface FileRow {
- id: string;
- attributes: string;
- parent_id: string | null;
- type: string;
- download_progress?: number | null;
- created_at: string;
- created_by: string;
- created_by_attributes: string | null;
-}
-
-export class FileGetQueryHandler implements QueryHandler<FileGetQueryInput> {
- public async handleQuery(
- input: FileGetQueryInput
- ): Promise<QueryResult<FileGetQueryInput>> {
- const row = await this.fetchFile(input);
-
- return {
- output: row ? this.buildFile(row) : null,
- state: {
- row,
- },
- };
- }
-
- public async checkForChanges(
- changes: MutationChange[],
- input: FileGetQueryInput,
- state: Record<string, any>
- ): Promise<ChangeCheckResult<FileGetQueryInput>> {
- if (
- !changes.some(
- (change) =>
- change.type === 'workspace' &&
- (change.table === 'nodes' || change.table === 'downloads') &&
- change.userId === input.userId
- )
- ) {
- return {
- hasChanges: false,
- };
- }
-
- const row = await this.fetchFile(input);
- if (isEqual(row, state.row)) {
- return {
- hasChanges: false,
- };
- }
-
- return {
- hasChanges: true,
- result: {
- output: row ? this.buildFile(row) : null,
- state: {
- row,
- },
- },
- };
- }
-
- private async fetchFile(
- input: FileGetQueryInput
- ): Promise<FileRow | undefined> {
- const workspaceDatabase = await databaseManager.getWorkspaceDatabase(
- input.userId
- );
-
- const row = await workspaceDatabase
- .selectFrom('nodes as file')
- .leftJoin('downloads as file_downloads', (join) =>
- join.onRef('file.id', '=', 'file_downloads.node_id')
- )
- .leftJoin('nodes as created_by', (join) =>
- join.onRef('file.created_by', '=', 'created_by.id')
- )
- .select([
- 'file.id',
- 'file.parent_id',
- 'file.type',
- 'file.attributes',
- 'file_downloads.progress as download_progress',
- 'file.created_at',
- 'file.created_by',
- 'created_by.attributes as created_by_attributes',
- ])
- .where('file.id', '=', input.fileId)
- .executeTakeFirst();
-
- return row;
- }
-
- private buildFile(row: FileRow): FileDetails {
- const attributes = JSON.parse(row.attributes);
- const createdByAttributes = row.created_by_attributes
- ? JSON.parse(row.created_by_attributes)
- : null;
-
- return {
- id: row.id,
- name: attributes.name,
- mimeType: attributes.mimeType,
- size: attributes.size,
- extension: attributes.extension,
- fileName: attributes.fileName,
- downloadProgress: row.download_progress,
- createdAt: row.created_at,
- createdBy: {
- id: row.created_by,
- name: createdByAttributes.name,
- avatar: createdByAttributes.avatar,
- email: createdByAttributes.email,
- },
- };
- }
-}
diff --git a/apps/desktop/src/main/handlers/queries/index.ts b/apps/desktop/src/main/handlers/queries/index.ts
index 119ae154..cbc685e4 100644
--- a/apps/desktop/src/main/handlers/queries/index.ts
+++ b/apps/desktop/src/main/handlers/queries/index.ts
@@ -1,10 +1,7 @@
import { QueryMap } from '@/operations/queries';
import { QueryHandler } from '@/main/types';
import { AccountListQueryHandler } from '@/main/handlers/queries/accounts-list';
-import { BreadcrumbListQueryHandler } from '@/main/handlers/queries/breadcrumb-list';
import { MessageListQueryHandler } from '@/main/handlers/queries/message-list';
-import { NodeCollaboratorListQueryHandler } from '@/main/handlers/queries/node-collaborator-list';
-import { NodeCollaboratorSearchQueryHandler } from '@/main/handlers/queries/node-collaborator-search';
import { NodeGetQueryHandler } from '@/main/handlers/queries/node-get';
import { ServerListQueryHandler } from '@/main/handlers/queries/server-list';
import { SidebarChatListQueryHandler } from '@/main/handlers/queries/sidebar-chat-list';
@@ -14,7 +11,6 @@ import { WorkspaceListQueryHandler } from '@/main/handlers/queries/workspace-lis
import { WorkspaceUserListQueryHandler } from '@/main/handlers/queries/workspace-user-list';
import { RecordListQueryHandler } from '@/main/handlers/queries/record-list';
import { FileListQueryHandler } from '@/main/handlers/queries/file-list';
-import { FileGetQueryHandler } from '@/main/handlers/queries/file-get';
import { EmojisGetQueryHandler } from '@/main/handlers/queries/emojis-get';
import { IconsGetQueryHandler } from '@/main/handlers/queries/icons-get';
import { NodeWithAncestorsGetQueryHandler } from '@/main/handlers/queries/node-with-ancestors-get';
@@ -25,10 +21,7 @@ type QueryHandlerMap = {
export const queryHandlerMap: QueryHandlerMap = {
account_list: new AccountListQueryHandler(),
- breadcrumb_list: new BreadcrumbListQueryHandler(),
message_list: new MessageListQueryHandler(),
- node_collaborator_list: new NodeCollaboratorListQueryHandler(),
- node_collaborator_search: new NodeCollaboratorSearchQueryHandler(),
node_get: new NodeGetQueryHandler(),
record_list: new RecordListQueryHandler(),
server_list: new ServerListQueryHandler(),
@@ -38,7 +31,6 @@ export const queryHandlerMap: QueryHandlerMap = {
workspace_list: new WorkspaceListQueryHandler(),
workspace_user_list: new WorkspaceUserListQueryHandler(),
file_list: new FileListQueryHandler(),
- file_get: new FileGetQueryHandler(),
emojis_get: new EmojisGetQueryHandler(),
icons_get: new IconsGetQueryHandler(),
node_with_ancestors_get: new NodeWithAncestorsGetQueryHandler(),
diff --git a/apps/desktop/src/main/handlers/queries/message-list.ts b/apps/desktop/src/main/handlers/queries/message-list.ts
index 9c904d9e..ea664453 100644
--- a/apps/desktop/src/main/handlers/queries/message-list.ts
+++ b/apps/desktop/src/main/handlers/queries/message-list.ts
@@ -8,9 +8,12 @@ import {
} from '@/main/types';
import { SelectNode } from '@/main/data/workspace/schema';
import { NodeTypes } from '@colanode/core';
-import { MessageNode, MessageReactionCount } from '@/types/messages';
+import {
+ MessageNode,
+ MessageReactionCount,
+ MessageAuthor,
+} from '@/types/messages';
import { mapNode } from '@/main/utils';
-import { UserNode } from '@/types/users';
import { compareString } from '@/lib/utils';
import { isEqual } from 'lodash-es';
import { mapBlocksToContents } from '@/lib/editor';
@@ -125,7 +128,7 @@ export class MessageListQueryHandler
authorRows: SelectNode[]
): MessageNode[] => {
const messages: MessageNode[] = [];
- const authorMap = new Map<string, UserNode>();
+ const authorMap = new Map<string, MessageAuthor>();
for (const authorRow of authorRows) {
const authorNode = mapNode(authorRow);
if (authorNode.type !== 'user') {
diff --git a/apps/desktop/src/main/handlers/queries/node-collaborator-list.ts b/apps/desktop/src/main/handlers/queries/node-collaborator-list.ts
deleted file mode 100644
index df9ba04a..00000000
--- a/apps/desktop/src/main/handlers/queries/node-collaborator-list.ts
+++ /dev/null
@@ -1,255 +0,0 @@
-import { NodeCollaboratorListQueryInput } from '@/operations/queries/node-collaborator-list';
-import { databaseManager } from '@/main/data/database-manager';
-import {
- InheritNodeCollaboratorsGroup,
- NodeCollaboratorsWrapper,
- NodeCollaborator,
-} from '@/types/nodes';
-import { NodeAttributes } from '@colanode/core';
-import {
- MutationChange,
- ChangeCheckResult,
- QueryHandler,
- QueryResult,
-} from '@/main/types';
-import { isEqual } from 'lodash-es';
-
-type ExtractedNodeCollaborator = {
- nodeId: string;
- collaboratorId: string;
- role: string;
-};
-
-type NodeWithAttributesRow = {
- id: string;
- attributes: string;
-};
-
-export class NodeCollaboratorListQueryHandler
- implements QueryHandler<NodeCollaboratorListQueryInput>
-{
- public async handleQuery(
- input: NodeCollaboratorListQueryInput
- ): Promise<QueryResult<NodeCollaboratorListQueryInput>> {
- const ancestors = await this.fetchAncestors(input);
- const extractedCollaborators = this.extractCollaborators(ancestors);
- const collaboratorIds = extractedCollaborators.map(
- (collaborator) => collaborator.collaboratorId
- );
-
- const collaboratorNodes = await this.fetchCollaborators(
- input,
- collaboratorIds
- );
-
- return {
- output: this.buildNodeCollaborators(
- input.nodeId,
- ancestors,
- collaboratorNodes,
- extractedCollaborators
- ),
- state: {
- ancestors,
- extractedCollaborators,
- collaboratorNodes,
- },
- };
- }
-
- public async checkForChanges(
- changes: MutationChange[],
- input: NodeCollaboratorListQueryInput,
- state: Record<string, any>
- ): Promise<ChangeCheckResult<NodeCollaboratorListQueryInput>> {
- if (
- !changes.some(
- (change) =>
- change.type === 'workspace' &&
- change.table === 'nodes' &&
- change.userId === input.userId
- )
- ) {
- return {
- hasChanges: false,
- };
- }
-
- const ancestors = await this.fetchAncestors(input);
- const extractedCollaborators = this.extractCollaborators(ancestors);
- const collaboratorIds = extractedCollaborators.map(
- (collaborator) => collaborator.collaboratorId
- );
-
- const collaboratorNodes = await this.fetchCollaborators(
- input,
- collaboratorIds
- );
-
- if (
- isEqual(ancestors, state.ancestors) &&
- isEqual(extractedCollaborators, state.extractedCollaborators) &&
- isEqual(collaboratorNodes, state.collaboratorNodes)
- ) {
- return {
- hasChanges: false,
- };
- }
-
- return {
- hasChanges: true,
- result: {
- output: this.buildNodeCollaborators(
- input.nodeId,
- ancestors,
- collaboratorNodes,
- extractedCollaborators
- ),
- state: {
- ancestors,
- extractedCollaborators,
- collaboratorNodes,
- },
- },
- };
- }
-
- private async fetchAncestors(
- input: NodeCollaboratorListQueryInput
- ): Promise<NodeWithAttributesRow[]> {
- const workspaceDatabase = await databaseManager.getWorkspaceDatabase(
- input.userId
- );
-
- const result = await workspaceDatabase
- .selectFrom('nodes')
- .select(['nodes.id', 'nodes.attributes'])
- .innerJoin('node_paths', 'nodes.id', 'node_paths.ancestor_id')
- .where('node_paths.descendant_id', '=', input.nodeId)
- .orderBy('node_paths.level', 'desc')
- .execute();
-
- return result;
- }
-
- private extractCollaborators(
- nodes: NodeWithAttributesRow[]
- ): ExtractedNodeCollaborator[] {
- const map: Map<string, ExtractedNodeCollaborator> = new Map();
-
- for (const node of nodes) {
- const attributes = JSON.parse(node.attributes) as NodeAttributes;
-
- if (
- attributes.type !== 'space' &&
- attributes.type !== 'channel' &&
- attributes.type !== 'chat' &&
- attributes.type !== 'database' &&
- attributes.type !== 'page'
- ) {
- continue;
- }
-
- const collaborators = attributes.collaborators;
- if (!collaborators) {
- continue;
- }
-
- const collaboratorIds = Object.keys(collaborators);
- for (const collaboratorId of collaboratorIds) {
- map.set(collaboratorId, {
- nodeId: node.id,
- collaboratorId,
- role: collaborators[collaboratorId],
- });
- }
- }
-
- return Array.from(map.values());
- }
-
- private async fetchCollaborators(
- input: NodeCollaboratorListQueryInput,
- collaboratorIds: string[]
- ): Promise<NodeWithAttributesRow[]> {
- if (collaboratorIds.length === 0) {
- return [];
- }
-
- const workspaceDatabase = await databaseManager.getWorkspaceDatabase(
- input.userId
- );
-
- const result = await workspaceDatabase
- .selectFrom('nodes')
- .select(['id', 'attributes'])
- .where('id', 'in', collaboratorIds)
- .execute();
-
- return result;
- }
-
- private buildNodeCollaborators = (
- nodeId: string,
- ancestors: NodeWithAttributesRow[],
- collaboratorNodes: NodeWithAttributesRow[],
- extractedCollaborators: ExtractedNodeCollaborator[]
- ): NodeCollaboratorsWrapper => {
- const direct: NodeCollaborator[] = [];
- const inherit: InheritNodeCollaboratorsGroup[] = [];
-
- const inheritCollaboratorMap: Map<string, NodeCollaborator[]> = new Map();
-
- for (const extractedCollaborator of extractedCollaborators) {
- const collaboratorNode = collaboratorNodes.find(
- (row) => row.id === extractedCollaborator.collaboratorId
- );
-
- if (!collaboratorNode) {
- continue;
- }
-
- const collaboratorAttributes = JSON.parse(collaboratorNode?.attributes);
-
- const collaborator: NodeCollaborator = {
- id: extractedCollaborator.collaboratorId,
- name: collaboratorAttributes.name,
- email: collaboratorAttributes.email,
- avatar: collaboratorAttributes.avatar || null,
- role: extractedCollaborator.role,
- };
- if (extractedCollaborator.nodeId === nodeId) {
- direct.push(collaborator);
- } else {
- if (!inheritCollaboratorMap.has(extractedCollaborator.nodeId)) {
- inheritCollaboratorMap.set(extractedCollaborator.nodeId, []);
- }
-
- inheritCollaboratorMap
- .get(extractedCollaborator.nodeId)
- ?.push(collaborator);
- }
- }
-
- for (const ancestor of ancestors.reverse()) {
- if (!inheritCollaboratorMap.has(ancestor.id)) {
- continue;
- }
-
- const ancestorAttributes = JSON.parse(ancestor.attributes);
- const group: InheritNodeCollaboratorsGroup = {
- id: ancestor.id,
- name: ancestorAttributes.name,
- avatar: ancestorAttributes.avatar || null,
- collaborators: inheritCollaboratorMap.get(ancestor.id) ?? [],
- };
-
- inherit.push(group);
- }
-
- return {
- direct,
- inherit,
- };
- };
-}
diff --git a/apps/desktop/src/main/handlers/queries/node-collaborator-search.ts b/apps/desktop/src/main/handlers/queries/node-collaborator-search.ts
deleted file mode 100644
index 315627bf..00000000
--- a/apps/desktop/src/main/handlers/queries/node-collaborator-search.ts
+++ /dev/null
@@ -1,118 +0,0 @@
-import { NodeCollaboratorSearchQueryInput } from '@/operations/queries/node-collaborator-search';
-import { databaseManager } from '@/main/data/database-manager';
-import { sql } from 'kysely';
-import { SelectNode } from '@/main/data/workspace/schema';
-import { NodeTypes } from '@colanode/core';
-import { NodeCollaborator } from '@/types/nodes';
-import {
- MutationChange,
- ChangeCheckResult,
- QueryHandler,
- QueryResult,
-} from '@/main/types';
-import { isEqual } from 'lodash-es';
-
-export class NodeCollaboratorSearchQueryHandler
- implements QueryHandler<NodeCollaboratorSearchQueryInput>
-{
- public async handleQuery(
- input: NodeCollaboratorSearchQueryInput
- ): Promise<QueryResult<NodeCollaboratorSearchQueryInput>> {
- if (input.searchQuery.length === 0) {
- return {
- output: [],
- state: {
- rows: [],
- },
- };
- }
-
- const rows = await this.fetchNodes(input);
- return {
- output: this.buildCollaboratorNodes(rows),
- state: {
- rows: rows,
- },
- };
- }
-
- public async checkForChanges(
- changes: MutationChange[],
- input: NodeCollaboratorSearchQueryInput,
- state: Record<string, any>
- ): Promise<ChangeCheckResult<NodeCollaboratorSearchQueryInput>> {
- if (
- !changes.some(
- (change) =>
- change.type === 'workspace' &&
- change.table === 'nodes' &&
- change.userId === input.userId
- )
- ) {
- return {
- hasChanges: false,
- };
- }
-
- const rows = await this.fetchNodes(input);
- if (isEqual(rows, state.rows)) {
- return {
- hasChanges: false,
- };
- }
-
- return {
- hasChanges: true,
- result: {
- output: this.buildCollaboratorNodes(rows),
- state: {
- rows,
- },
- },
- };
- }
-
- private async fetchNodes(
- input: NodeCollaboratorSearchQueryInput
- ): Promise<SelectNode[]> {
- if (input.searchQuery.length === 0) {
- return [];
- }
-
- const workspaceDatabase = await databaseManager.getWorkspaceDatabase(
- input.userId
- );
-
- const query = sql<SelectNode>`
- SELECT n.*
- FROM nodes n
- JOIN node_names nn ON n.id = nn.id
- WHERE n.type = ${NodeTypes.User}
- AND nn.name MATCH ${input.searchQuery + '*'}
- ${
- input.excluded.length > 0
- ? sql`AND n.id NOT IN (${sql.join(
- input.excluded.map((id) => sql`${id}`),
- sql`, `
- )})`
- : sql``
- }
- `.compile(workspaceDatabase);
-
- const result = await workspaceDatabase.executeQuery(query);
- return result.rows;
- }
-
- private buildCollaboratorNodes = (rows: SelectNode[]): NodeCollaborator[] => {
- return rows.map((row) => {
- const attributes = JSON.parse(row.attributes);
- return {
- id: row.id,
- name: attributes.name,
- email: attributes.email,
- avatar: attributes.avatar,
- role: 'collaborator',
- };
- });
- };
-}
diff --git a/apps/desktop/src/main/handlers/queries/user-search.ts b/apps/desktop/src/main/handlers/queries/user-search.ts
index 70230972..d5e61d42 100644
--- a/apps/desktop/src/main/handlers/queries/user-search.ts
+++ b/apps/desktop/src/main/handlers/queries/user-search.ts
@@ -3,7 +3,7 @@ import { databaseManager } from '@/main/data/database-manager';
import { sql } from 'kysely';
import { SelectNode } from '@/main/data/workspace/schema';
import { NodeTypes } from '@colanode/core';
-import { UserNode } from '@/types/users';
+import { UserNode } from '@colanode/core';
import {
MutationChange,
ChangeCheckResult,
@@ -11,6 +11,7 @@ import {
QueryResult,
} from '@/main/types';
import { isEqual } from 'lodash-es';
+import { mapNode } from '@/main/utils';
export class UserSearchQueryHandler
implements QueryHandler<UserSearchQueryInput>
@@ -77,6 +78,7 @@ export class UserSearchQueryHandler
input.userId
);
+ const exclude = input.exclude ?? [];
const query = sql<SelectNode>`
SELECT n.*
FROM nodes n
@@ -84,6 +86,14 @@ export class UserSearchQueryHandler
WHERE n.type = ${NodeTypes.User}
AND n.id != ${input.userId}
AND nn.name MATCH ${input.searchQuery + '*'}
+ ${
+ exclude.length > 0
+ ? sql`AND n.id NOT IN (${sql.join(
+ exclude.map((id) => sql`${id}`),
+ sql`, `
+ )})`
+ : sql``
+ }
`.compile(workspaceDatabase);
const result = await workspaceDatabase.executeQuery(query);
@@ -95,23 +105,17 @@ export class UserSearchQueryHandler
input.userId
);
+ const exclude = input.exclude ?? [];
return workspaceDatabase
.selectFrom('nodes')
.where('type', '=', NodeTypes.User)
.where('id', '!=', input.userId)
+ .where('id', 'not in', exclude)
.selectAll()
.execute();
}
private buildUserNodes = (rows: SelectNode[]): UserNode[] => {
- return rows.map((row) => {
- const attributes = JSON.parse(row.attributes);
- return {
- id: row.id,
- name: attributes.name,
- email: attributes.email,
- avatar: attributes.avatar,
- };
- });
+ return rows.map((row) => mapNode(row) as UserNode);
};
}
diff --git a/apps/desktop/src/operations/queries/breadcrumb-list.ts b/apps/desktop/src/operations/queries/breadcrumb-list.ts
deleted file mode 100644
index 4b0acc0e..00000000
--- a/apps/desktop/src/operations/queries/breadcrumb-list.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { BreadcrumbNode } from '@/types/workspaces';
-
-export type BreadcrumbListQueryInput = {
- type: 'breadcrumb_list';
- nodeId: string;
- userId: string;
-};
-
-declare module '@/operations/queries' {
- interface QueryMap {
- breadcrumb_list: {
- input: BreadcrumbListQueryInput;
- output: BreadcrumbNode[];
- };
- }
-}
diff --git a/apps/desktop/src/operations/queries/file-get.ts b/apps/desktop/src/operations/queries/file-get.ts
deleted file mode 100644
index 96a63b54..00000000
--- a/apps/desktop/src/operations/queries/file-get.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { FileDetails } from '@/types/files';
-
-export type FileGetQueryInput = {
- type: 'file_get';
- fileId: string;
- userId: string;
-};
-
-declare module '@/operations/queries' {
- interface QueryMap {
- file_get: {
- input: FileGetQueryInput;
- output: FileDetails | null;
- };
- }
-}
diff --git a/apps/desktop/src/operations/queries/node-collaborator-list.ts b/apps/desktop/src/operations/queries/node-collaborator-list.ts
deleted file mode 100644
index e973c7bd..00000000
--- a/apps/desktop/src/operations/queries/node-collaborator-list.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { NodeCollaboratorsWrapper } from '@/types/nodes';
-
-export type NodeCollaboratorListQueryInput = {
- type: 'node_collaborator_list';
- nodeId: string;
- userId: string;
-};
-
-declare module '@/operations/queries' {
- interface QueryMap {
- node_collaborator_list: {
- input: NodeCollaboratorListQueryInput;
- output: NodeCollaboratorsWrapper;
- };
- }
-}
diff --git a/apps/desktop/src/operations/queries/node-collaborator-search.ts b/apps/desktop/src/operations/queries/node-collaborator-search.ts
deleted file mode 100644
index c814d785..00000000
--- a/apps/desktop/src/operations/queries/node-collaborator-search.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { NodeCollaborator } from '@/types/nodes';
-
-export type NodeCollaboratorSearchQueryInput = {
- type: 'node_collaborator_search';
- searchQuery: string;
- excluded: string[];
- userId: string;
-};
-
-declare module '@/operations/queries' {
- interface QueryMap {
- node_collaborator_search: {
- input: NodeCollaboratorSearchQueryInput;
- output: NodeCollaborator[];
- };
- }
-}
diff --git a/apps/desktop/src/operations/queries/user-search.ts b/apps/desktop/src/operations/queries/user-search.ts
index ae164dfa..2e96114e 100644
--- a/apps/desktop/src/operations/queries/user-search.ts
+++ b/apps/desktop/src/operations/queries/user-search.ts
@@ -1,9 +1,10 @@
-import { UserNode } from '@/types/users';
+import { UserNode } from '@colanode/core';
export type UserSearchQueryInput = {
type: 'user_search';
searchQuery: string;
userId: string;
+ exclude?: string[];
};
declare module '@/operations/queries' {
diff --git a/apps/desktop/src/renderer/components/chats/chat-create-command.tsx b/apps/desktop/src/renderer/components/chats/chat-create-command.tsx
index 3c703ae9..f07c78bd 100644
--- a/apps/desktop/src/renderer/components/chats/chat-create-command.tsx
+++ b/apps/desktop/src/renderer/components/chats/chat-create-command.tsx
@@ -56,13 +56,15 @@ export const ChatCreateCommand = () => {
<div className="flex w-full flex-row items-center gap-2">
<Avatar
id={user.id}
- name={user.name}
- avatar={user.avatar}
+ name={user.attributes.name}
+ avatar={user.attributes.avatar}
className="h-7 w-7"
/>
<div className="flex flex-grow flex-col">
- <p className="text-sm">{user.name}</p>
- <p className="text-xs text-muted-foreground">{user.email}</p>
+ <p className="text-sm">{user.attributes.name}</p>
+ <p className="text-xs text-muted-foreground">
+ {user.attributes.email}
+ </p>
</div>
</div>
</CommandItem>
diff --git a/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx b/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx
index 6466d899..d1a2e605 100644
--- a/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx
+++ b/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx
@@ -1,12 +1,12 @@
import React from 'react';
import { NodeCollaboratorSearch } from '@/renderer/components/collaborators/node-collaborator-search';
-import { NodeCollaborator } from '@/types/nodes';
import { Button } from '@/renderer/components/ui/button';
import { Spinner } from '@/renderer/components/ui/spinner';
import { NodeCollaboratorRoleDropdown } from '@/renderer/components/collaborators/node-collaborator-role-dropdown';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { toast } from '@/renderer/hooks/use-toast';
import { useWorkspace } from '@/renderer/contexts/workspace';
+import { UserNode } from '@colanode/core';
interface NodeCollaboratorCreate {
nodeId: string;
@@ -20,16 +20,14 @@ export const NodeCollaboratorCreate = ({
const workspace = useWorkspace();
const { mutate, isPending } = useMutation();
- const [collaborators, setCollaborators] = React.useState<NodeCollaborator[]>(
- []
- );
+ const [users, setUsers] = React.useState<UserNode[]>([]);
const [role, setRole] = React.useState('collaborator');
return (
<div className="flex flex-col gap-2">
<NodeCollaboratorSearch
- value={collaborators}
- onChange={setCollaborators}
+ value={users}
+ onChange={setUsers}
excluded={existingCollaborators}
/>
<div className="flex justify-end space-x-2">
@@ -38,7 +36,7 @@ export const NodeCollaboratorCreate = ({
variant="default"
className="shrink-0"
size="sm"
- disabled={collaborators.length === 0 || isPending}
+ disabled={users.length === 0 || isPending}
onClick={() => {
if (isPending) {
return;
@@ -48,14 +46,12 @@ export const NodeCollaboratorCreate = ({
input: {
type: 'node_collaborator_create',
nodeId,
- collaboratorIds: collaborators.map(
- (collaborator) => collaborator.id
- ),
+ collaboratorIds: users.map((user) => user.id),
role: role,
userId: workspace.userId,
},
onSuccess() {
- setCollaborators([]);
+ setUsers([]);
},
onError() {
toast({
diff --git a/apps/desktop/src/renderer/components/collaborators/node-collaborator-search.tsx b/apps/desktop/src/renderer/components/collaborators/node-collaborator-search.tsx
index d695b988..df68f280 100644
--- a/apps/desktop/src/renderer/components/collaborators/node-collaborator-search.tsx
+++ b/apps/desktop/src/renderer/components/collaborators/node-collaborator-search.tsx
@@ -1,5 +1,4 @@
import React from 'react';
-import { NodeCollaborator } from '@/types/nodes';
import {
Popover,
PopoverContent,
@@ -19,11 +18,12 @@ import { Avatar } from '@/renderer/components/avatars/avatar';
import { useQuery } from '@/renderer/hooks/use-query';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { X } from 'lucide-react';
+import { UserNode } from '@colanode/core';
interface NodeCollaboratorSearchProps {
excluded: string[];
- value: NodeCollaborator[];
- onChange: (value: NodeCollaborator[]) => void;
+ value: UserNode[];
+ onChange: (value: UserNode[]) => void;
}
export const NodeCollaboratorSearch = ({
@@ -37,12 +37,14 @@ export const NodeCollaboratorSearch = ({
const [open, setOpen] = React.useState(false);
const { data } = useQuery({
- type: 'node_collaborator_search',
+ type: 'user_search',
searchQuery: query,
- excluded: excluded,
+ exclude: excluded,
userId: workspace.userId,
});
+ const users = data ?? [];
+
return (
<Popover open={open} onOpenChange={setOpen} modal={true}>
<PopoverTrigger asChild>
@@ -52,15 +54,15 @@ export const NodeCollaboratorSearch = ({
aria-expanded={open}
className="w-full justify-start p-2"
>
- {value.map((collaborator) => (
- <Badge key={collaborator.id} variant="outline">
- {collaborator.name}
+ {value.map((user) => (
+ <Badge key={user.id} variant="outline">
+ {user.attributes.name}
<span
className="ml-1 rounded-full outline-none ring-offset-background focus:ring-2 focus:ring-ring focus:ring-offset-2"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
- onChange(value.filter((v) => v.id !== collaborator.id));
+ onChange(value.filter((v) => v.id !== user.id));
}}
>
<X className="size-3 text-muted-foreground hover:text-foreground" />
@@ -85,25 +87,25 @@ export const NodeCollaboratorSearch = ({
<CommandEmpty>No collaborator found.</CommandEmpty>
<CommandList>
<CommandGroup className="h-min max-h-96">
- {data?.map((collaborator) => (
+ {users.map((user) => (
<CommandItem
- key={collaborator.id}
+ key={user.id}
onSelect={() => {
- onChange([...value, collaborator]);
+ onChange([...value, user]);
setQuery('');
}}
>
<div className="flex w-full flex-row items-center gap-2">
<Avatar
- id={collaborator.id}
- name={collaborator.name}
- avatar={collaborator.avatar}
+ id={user.id}
+ name={user.attributes.name}
+ avatar={user.attributes.avatar}
className="h-7 w-7"
/>
<div className="flex flex-grow flex-col">
- <p className="text-sm">{collaborator.name}</p>
+ <p className="text-sm">{user.attributes.name}</p>
<p className="text-xs text-muted-foreground">
- {collaborator.email}
+ {user.attributes.email}
</p>
</div>
</div>
diff --git a/apps/desktop/src/renderer/components/files/file-block.tsx b/apps/desktop/src/renderer/components/files/file-block.tsx
index 053bc2e8..a1a8452c 100644
--- a/apps/desktop/src/renderer/components/files/file-block.tsx
+++ b/apps/desktop/src/renderer/components/files/file-block.tsx
@@ -12,16 +12,17 @@ export const FileBlock = ({ id }: FileBlockProps) => {
const workspace = useWorkspace();
const { data } = useQuery({
- type: 'file_get',
- fileId: id,
+ type: 'node_get',
+ nodeId: id,
userId: workspace.userId,
});
- if (!data) {
+ if (!data || data.type !== 'file') {
return null;
}
- const url = getFileUrl(workspace.userId, data.id, data.extension);
+ const downloadProgress: number = 0;
+ const url = getFileUrl(workspace.userId, data.id, data.attributes.extension);
return (
<div
className="flex h-72 max-h-72 w-full cursor-pointer overflow-hidden rounded-md p-2 hover:bg-gray-100"
@@ -29,10 +30,14 @@ export const FileBlock = ({ id }: FileBlockProps) => {
workspace.openModal(id);
}}
>
- {data.downloadProgress !== 100 ? (
- <FileDownload id={data.id} downloadProgress={data.downloadProgress} />
+ {downloadProgress !== 100 ? (
+ <FileDownload id={data.id} downloadProgress={downloadProgress} />
) : (
- <FilePreview url={url} name={data.name} mimeType={data.mimeType} />
+ <FilePreview
+ url={url}
+ name={data.attributes.name}
+ mimeType={data.attributes.mimeType}
+ />
)}
</div>
);
diff --git a/apps/desktop/src/types/files.ts b/apps/desktop/src/types/files.ts
index f16c493c..e090a49d 100644
--- a/apps/desktop/src/types/files.ts
+++ b/apps/desktop/src/types/files.ts
@@ -1,5 +1,3 @@
-import { UserNode } from '@/types/users';
-
export type ServerFileUploadResponse = {
id: string;
url: string;
@@ -21,18 +19,6 @@ export type FileNode = {
downloadProgress?: number | null;
};
-export type FileDetails = {
- id: string;
- name: string;
- mimeType: string;
- size: number;
- extension: string;
- fileName: string;
- downloadProgress?: number | null;
- createdAt: string;
- createdBy: UserNode;
-};
-
export type FilePreviewType = 'image' | 'video' | 'other';
export type FileMetadata = {
diff --git a/apps/desktop/src/types/messages.ts b/apps/desktop/src/types/messages.ts
index e3c02d41..c119f013 100644
--- a/apps/desktop/src/types/messages.ts
+++ b/apps/desktop/src/types/messages.ts
@@ -1,11 +1,10 @@
-import { UserNode } from '@/types/users';
import { JSONContent } from '@tiptap/core';
export type MessageNode = {
id: string;
content: JSONContent[];
createdAt: string;
- author: UserNode;
+ author: MessageAuthor;
reactionCounts: MessageReactionCount[];
versionId: string;
};
@@ -15,3 +14,10 @@ export type MessageReactionCount = {
count: number;
isReactedTo: boolean;
};
+
+export type MessageAuthor = {
+ id: string;
+ name: string;
+ email: string;
+ avatar: string | null;
+};
diff --git a/apps/desktop/src/types/users.ts b/apps/desktop/src/types/users.ts
deleted file mode 100644
index 9e4119e4..00000000
--- a/apps/desktop/src/types/users.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-export type UserNode = {
- id: string;
- name: string;
- email: string;
- avatar: string | null;
-};
-
-export type UserAttributes = {
- type: 'user';
- name: string;
- email: string;
- avatar: string | null;
-};