Skip to content

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

Implement message handlers

Hakan Shehu2210306d32c2
diff --git a/desktop/src/main/mediator.ts b/desktop/src/main/mediator.ts
index c257f889..1bed5251 100644
--- a/desktop/src/main/mediator.ts
+++ b/desktop/src/main/mediator.ts
@@ -13,6 +13,8 @@ import {
} from '@/types/queries';
import { queryHandlerMap } from '@/main/queries';
import { eventBus } from '@/lib/event-bus';
+import { MessageHandler, MessageInput } from '@/operations/messages';
+import { messageHandlerMap } from '@/main/messages';
class Mediator {
private readonly subscribedQueries: Map<string, SubscribedQuery<QueryInput>> =
@@ -24,8 +26,8 @@ class Mediator {
const handler = mutationHandlerMap[input.type] as MutationHandler<T>;
const result = await handler.handleMutation(input);
- if (result.changedTables) {
- await this.checkForQueryChanges(result.changedTables);
+ if (result.changes) {
+ await this.checkForQueryChanges(result.changes);
}
return result.output;
@@ -52,6 +54,11 @@ class Mediator {
return result.output;
}
+ public async executeMessage<T extends MessageInput>(input: T): Promise<void> {
+ const handler = messageHandlerMap[input.type] as MessageHandler<T>;
+ await handler.handleMessage(input);
+ }
+
public unsubscribeQuery(id: string) {
this.subscribedQueries.delete(id);
}
diff --git a/desktop/src/main/messages/index.ts b/desktop/src/main/messages/index.ts
new file mode 100644
index 00000000..53239b29
--- /dev/null
+++ b/desktop/src/main/messages/index.ts
@@ -0,0 +1,10 @@
+import { MessageHandler, MessageMap } from '@/operations/messages';
+import { ServerMutationMessageHandler } from '@/main/messages/server-mutation';
+
+type MessageHandlerMap = {
+ [K in keyof MessageMap]: MessageHandler<MessageMap[K]>;
+};
+
+export const messageHandlerMap: MessageHandlerMap = {
+ server_mutation: new ServerMutationMessageHandler(),
+};
diff --git a/desktop/src/main/messages/server-mutation.ts b/desktop/src/main/messages/server-mutation.ts
new file mode 100644
index 00000000..7dc9f649
--- /dev/null
+++ b/desktop/src/main/messages/server-mutation.ts
@@ -0,0 +1,47 @@
+import { MessageHandler } from '@/operations/messages';
+import { ServerMutationMessageInput } from '@/operations/messages/server-mutation';
+import { mediator } from '@/main/mediator';
+
+export class ServerMutationMessageHandler
+ implements MessageHandler<ServerMutationMessageInput>
+{
+ public async handleMessage(input: ServerMutationMessageInput): Promise<void> {
+ if (input.mutation.table === 'nodes' && input.mutation.workspaceId) {
+ await mediator.executeMutation({
+ type: 'node_sync',
+ id: input.mutation.id,
+ accountId: input.accountId,
+ workspaceId: input.mutation.workspaceId,
+ action: input.mutation.action,
+ after: input.mutation.after,
+ before: input.mutation.before,
+ });
+ } else if (
+ input.mutation.table === 'node_reactions' &&
+ input.mutation.workspaceId
+ ) {
+ await mediator.executeMutation({
+ type: 'node_reaction_sync',
+ id: input.mutation.id,
+ accountId: input.accountId,
+ workspaceId: input.mutation.workspaceId,
+ action: input.mutation.action,
+ after: input.mutation.after,
+ before: input.mutation.before,
+ });
+ } else if (
+ input.mutation.table === 'node_collaborator' &&
+ input.mutation.workspaceId
+ ) {
+ await mediator.executeMutation({
+ type: 'node_collaborator_sync',
+ id: input.mutation.id,
+ accountId: input.accountId,
+ workspaceId: input.mutation.workspaceId,
+ action: input.mutation.action,
+ after: input.mutation.after,
+ before: input.mutation.before,
+ });
+ }
+ }
+}
diff --git a/desktop/src/main/mutations/board-view-create.ts b/desktop/src/main/mutations/board-view-create.ts
index 36e6b147..f47ced81 100644
--- a/desktop/src/main/mutations/board-view-create.ts
+++ b/desktop/src/main/mutations/board-view-create.ts
@@ -16,10 +16,6 @@ export class BoardViewCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const siblings = await workspaceDatabase
.selectFrom('nodes')
.where((eb) =>
@@ -60,7 +56,7 @@ export class BoardViewCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/calendar-view-create.ts b/desktop/src/main/mutations/calendar-view-create.ts
index b41dba08..779a85c0 100644
--- a/desktop/src/main/mutations/calendar-view-create.ts
+++ b/desktop/src/main/mutations/calendar-view-create.ts
@@ -16,10 +16,6 @@ export class CalendarViewCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const siblings = await workspaceDatabase
.selectFrom('nodes')
.where((eb) =>
@@ -60,7 +56,7 @@ export class CalendarViewCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/channel-create.ts b/desktop/src/main/mutations/channel-create.ts
index 3bdb9df1..2713436e 100644
--- a/desktop/src/main/mutations/channel-create.ts
+++ b/desktop/src/main/mutations/channel-create.ts
@@ -16,10 +16,6 @@ export class ChannelCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const siblings = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
@@ -55,7 +51,7 @@ export class ChannelCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/chat-create.ts b/desktop/src/main/mutations/chat-create.ts
index 08603ba1..2510da1c 100644
--- a/desktop/src/main/mutations/chat-create.ts
+++ b/desktop/src/main/mutations/chat-create.ts
@@ -8,17 +8,13 @@ import { ChatCreateMutationInput } from '@/types/mutations/chat-create';
export class ChatCreateMutationHandler
implements MutationHandler<ChatCreateMutationInput>
{
- async handleMutation(
+ public async handleMutation(
input: ChatCreateMutationInput,
): Promise<MutationResult<ChatCreateMutationInput>> {
const workspaceDatabase = await databaseContext.getWorkspaceDatabase(
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const existingChats = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
@@ -88,7 +84,7 @@ export class ChatCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/database-create.ts b/desktop/src/main/mutations/database-create.ts
index a3d9d3d7..e3243990 100644
--- a/desktop/src/main/mutations/database-create.ts
+++ b/desktop/src/main/mutations/database-create.ts
@@ -16,10 +16,6 @@ export class DatabaseCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const siblings = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
@@ -84,7 +80,7 @@ export class DatabaseCreateMutationHandler
output: {
id: databaseId,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/email-login.ts b/desktop/src/main/mutations/email-login.ts
index 86ce2b82..4faf013d 100644
--- a/desktop/src/main/mutations/email-login.ts
+++ b/desktop/src/main/mutations/email-login.ts
@@ -88,7 +88,7 @@ export class EmailLoginMutationHandler
output: {
success: true,
},
- changedTables: changedTables,
+ changes: changedTables,
};
}
}
diff --git a/desktop/src/main/mutations/email-register.ts b/desktop/src/main/mutations/email-register.ts
index dc1a14b6..1c0a4182 100644
--- a/desktop/src/main/mutations/email-register.ts
+++ b/desktop/src/main/mutations/email-register.ts
@@ -90,7 +90,7 @@ export class EmailRegisterMutationHandler
output: {
success: true,
},
- changedTables: changedTables,
+ changes: changedTables,
};
};
}
diff --git a/desktop/src/main/mutations/field-create.ts b/desktop/src/main/mutations/field-create.ts
index 83fc03b2..a7888976 100644
--- a/desktop/src/main/mutations/field-create.ts
+++ b/desktop/src/main/mutations/field-create.ts
@@ -16,10 +16,6 @@ export class FieldCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const siblings = await workspaceDatabase
.selectFrom('nodes')
.where((eb) =>
@@ -62,7 +58,7 @@ export class FieldCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/index.ts b/desktop/src/main/mutations/index.ts
index 6add8ee3..fe76c833 100644
--- a/desktop/src/main/mutations/index.ts
+++ b/desktop/src/main/mutations/index.ts
@@ -26,6 +26,9 @@ import { WorkspaceAccountRoleUpdateMutationHandler } from '@/main/mutations/work
import { WorkspaceAccountsInviteMutationHandler } from '@/main/mutations/workspace-accounts-invite';
import { WorkspaceCreateMutationHandler } from '@/main/mutations/workspace-create';
import { WorkspaceUpdateMutationHandler } from '@/main/mutations/workspace-update';
+import { NodeSyncMutationHandler } from '@/main/mutations/node-sync';
+import { NodeReactionSyncMutationHandler } from '@/main/mutations/node-reaction-sync';
+import { NodeCollaboratorSyncMutationHandler } from '@/main/mutations/node-collaborator-sync';
type MutationHandlerMap = {
[K in keyof MutationMap]: MutationHandler<MutationMap[K]['input']>;
@@ -60,4 +63,7 @@ export const mutationHandlerMap: MutationHandlerMap = {
workspace_accounts_invite: new WorkspaceAccountsInviteMutationHandler(),
workspace_create: new WorkspaceCreateMutationHandler(),
workspace_update: new WorkspaceUpdateMutationHandler(),
+ node_sync: new NodeSyncMutationHandler(),
+ node_reaction_sync: new NodeReactionSyncMutationHandler(),
+ node_collaborator_sync: new NodeCollaboratorSyncMutationHandler(),
};
diff --git a/desktop/src/main/mutations/message-create.ts b/desktop/src/main/mutations/message-create.ts
index 4f13a9dd..ef2663e8 100644
--- a/desktop/src/main/mutations/message-create.ts
+++ b/desktop/src/main/mutations/message-create.ts
@@ -18,10 +18,6 @@ export class MessageCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const id = NeuronId.generate(NeuronId.Type.Message);
const editorNodes = mapContentsToEditorNodes(
input.content.content,
@@ -60,7 +56,7 @@ export class MessageCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/node-attribute-delete.ts b/desktop/src/main/mutations/node-attribute-delete.ts
index 1f10d2cd..8347aa65 100644
--- a/desktop/src/main/mutations/node-attribute-delete.ts
+++ b/desktop/src/main/mutations/node-attribute-delete.ts
@@ -15,10 +15,6 @@ export class NodeAttributeDeleteMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const node = await workspaceDatabase
.selectFrom('nodes')
.where('id', '=', input.nodeId)
@@ -64,7 +60,7 @@ export class NodeAttributeDeleteMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/node-attribute-set.ts b/desktop/src/main/mutations/node-attribute-set.ts
index ca583b09..b5705737 100644
--- a/desktop/src/main/mutations/node-attribute-set.ts
+++ b/desktop/src/main/mutations/node-attribute-set.ts
@@ -15,10 +15,6 @@ export class NodeAttributeSetMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const node = await workspaceDatabase
.selectFrom('nodes')
.where('id', '=', input.nodeId)
@@ -57,7 +53,7 @@ export class NodeAttributeSetMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/node-collaborator-create.ts b/desktop/src/main/mutations/node-collaborator-create.ts
index ac853145..91ef13b2 100644
--- a/desktop/src/main/mutations/node-collaborator-create.ts
+++ b/desktop/src/main/mutations/node-collaborator-create.ts
@@ -13,10 +13,6 @@ export class NodeCollaboratorCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
await workspaceDatabase
.insertInto('node_collaborators')
.values([
@@ -38,7 +34,7 @@ export class NodeCollaboratorCreateMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'node_collaborators',
diff --git a/desktop/src/main/mutations/node-collaborator-delete.ts b/desktop/src/main/mutations/node-collaborator-delete.ts
index 89a7530a..83c920b3 100644
--- a/desktop/src/main/mutations/node-collaborator-delete.ts
+++ b/desktop/src/main/mutations/node-collaborator-delete.ts
@@ -12,10 +12,6 @@ export class NodeCollaboratorDeleteMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
await workspaceDatabase
.deleteFrom('node_collaborators')
.where((eb) =>
@@ -30,7 +26,7 @@ export class NodeCollaboratorDeleteMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'node_collaborators',
diff --git a/desktop/src/main/mutations/node-collaborator-sync.ts b/desktop/src/main/mutations/node-collaborator-sync.ts
new file mode 100644
index 00000000..c1fde91e
--- /dev/null
+++ b/desktop/src/main/mutations/node-collaborator-sync.ts
@@ -0,0 +1,160 @@
+import { databaseContext } from '@/main/data/database-context';
+import { MutationHandler, MutationResult } from '@/types/mutations';
+import { NodeCollaboratorSyncMutationInput } from '@/types/mutations/node-collaborator-sync';
+import { ServerNodeCollaborator, ServerNodeReaction } from '@/types/nodes';
+
+export class NodeCollaboratorSyncMutationHandler
+ implements MutationHandler<NodeCollaboratorSyncMutationInput>
+{
+ public async handleMutation(
+ input: NodeCollaboratorSyncMutationInput,
+ ): Promise<MutationResult<NodeCollaboratorSyncMutationInput>> {
+ const workspace = await databaseContext.appDatabase
+ .selectFrom('workspaces')
+ .selectAll()
+ .where((eb) =>
+ eb.and([
+ eb('account_id', '=', input.accountId),
+ eb('workspace_id', '=', input.workspaceId),
+ ]),
+ )
+ .executeTakeFirst();
+
+ if (!workspace) {
+ return {
+ output: {
+ success: false,
+ },
+ };
+ }
+
+ const userId = workspace.user_id;
+ if (input.action === 'insert' && input.after) {
+ await this.insertNodeCollaborator(userId, input.after);
+ } else if (input.action === 'update' && input.after) {
+ await this.updateNodeCollaborator(userId, input.after);
+ } else if (input.action === 'delete' && input.before) {
+ await this.deleteNodeReaction(userId, input.before);
+ }
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'node_collaborators',
+ userId: userId,
+ },
+ ],
+ };
+ }
+
+ private async insertNodeCollaborator(
+ userId: string,
+ nodeCollaborator: ServerNodeCollaborator,
+ ): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ await workspaceDatabase
+ .insertInto('node_collaborators')
+ .values({
+ node_id: nodeCollaborator.nodeId,
+ collaborator_id: nodeCollaborator.collaboratorId,
+ role: nodeCollaborator.role,
+ created_at: nodeCollaborator.createdAt,
+ created_by: nodeCollaborator.createdBy,
+ updated_by: nodeCollaborator.updatedBy,
+ updated_at: nodeCollaborator.updatedAt,
+ version_id: nodeCollaborator.versionId,
+ server_created_at: nodeCollaborator.serverCreatedAt,
+ server_updated_at: nodeCollaborator.serverUpdatedAt,
+ server_version_id: nodeCollaborator.versionId,
+ })
+ .onConflict((cb) =>
+ cb
+ .doUpdateSet({
+ server_created_at: nodeCollaborator.serverCreatedAt,
+ server_updated_at: nodeCollaborator.serverUpdatedAt,
+ server_version_id: nodeCollaborator.versionId,
+ })
+ .where('version_id', '=', nodeCollaborator.versionId),
+ )
+ .execute();
+ }
+
+ private async updateNodeCollaborator(
+ userId: string,
+ nodeCollaborator: ServerNodeCollaborator,
+ ): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ const existingNodeCollaborator = await workspaceDatabase
+ .selectFrom('node_collaborators')
+ .selectAll()
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', nodeCollaborator.nodeId),
+ eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
+ ]),
+ )
+ .executeTakeFirst();
+
+ if (
+ existingNodeCollaborator.server_version_id === nodeCollaborator.versionId
+ ) {
+ return;
+ }
+
+ if (existingNodeCollaborator.updated_at) {
+ if (!nodeCollaborator.updatedAt) {
+ return;
+ }
+ const localUpdatedAt = new Date(existingNodeCollaborator.updated_at);
+ const serverUpdatedAt = new Date(nodeCollaborator.updatedAt);
+ if (localUpdatedAt > serverUpdatedAt) {
+ return;
+ }
+ }
+
+ await workspaceDatabase
+ .updateTable('node_collaborators')
+ .set({
+ role: nodeCollaborator.role,
+ updated_at: nodeCollaborator.updatedAt,
+ updated_by: nodeCollaborator.updatedBy,
+ version_id: nodeCollaborator.versionId,
+ server_created_at: nodeCollaborator.serverCreatedAt,
+ server_updated_at: nodeCollaborator.serverUpdatedAt,
+ server_version_id: nodeCollaborator.versionId,
+ })
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', nodeCollaborator.nodeId),
+ eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
+ ]),
+ )
+ .execute();
+ }
+
+ private async deleteNodeReaction(
+ userId: string,
+ nodeCollaborator: ServerNodeCollaborator,
+ ): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ await workspaceDatabase
+ .deleteFrom('node_collaborators')
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', nodeCollaborator.nodeId),
+ eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
+ ]),
+ )
+ .execute();
+ }
+}
diff --git a/desktop/src/main/mutations/node-collaborator-update.ts b/desktop/src/main/mutations/node-collaborator-update.ts
index 1bc1f2af..63c43c9b 100644
--- a/desktop/src/main/mutations/node-collaborator-update.ts
+++ b/desktop/src/main/mutations/node-collaborator-update.ts
@@ -13,10 +13,6 @@ export class NodeCollaboratorUpdateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
await workspaceDatabase
.updateTable('node_collaborators')
.set({
@@ -37,7 +33,7 @@ export class NodeCollaboratorUpdateMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'node_collaborators',
diff --git a/desktop/src/main/mutations/node-delete.ts b/desktop/src/main/mutations/node-delete.ts
index ddd888aa..93d1d9ca 100644
--- a/desktop/src/main/mutations/node-delete.ts
+++ b/desktop/src/main/mutations/node-delete.ts
@@ -12,10 +12,6 @@ export class NodeDeleteMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
await workspaceDatabase
.deleteFrom('nodes')
.where('id', '=', input.nodeId)
@@ -25,7 +21,7 @@ export class NodeDeleteMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/node-reaction-create.ts b/desktop/src/main/mutations/node-reaction-create.ts
index b1233e64..ebbd144b 100644
--- a/desktop/src/main/mutations/node-reaction-create.ts
+++ b/desktop/src/main/mutations/node-reaction-create.ts
@@ -13,10 +13,6 @@ export class NodeReactionCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
await workspaceDatabase
.insertInto('node_reactions')
.values({
@@ -32,7 +28,7 @@ export class NodeReactionCreateMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'node_reactions',
diff --git a/desktop/src/main/mutations/node-reaction-delete.ts b/desktop/src/main/mutations/node-reaction-delete.ts
index 2f9f5eec..c0b37100 100644
--- a/desktop/src/main/mutations/node-reaction-delete.ts
+++ b/desktop/src/main/mutations/node-reaction-delete.ts
@@ -12,10 +12,6 @@ export class NodeReactionDeleteMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
await workspaceDatabase
.deleteFrom('node_reactions')
.where((eb) =>
@@ -31,7 +27,7 @@ export class NodeReactionDeleteMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'node_reactions',
diff --git a/desktop/src/main/mutations/node-reaction-sync.ts b/desktop/src/main/mutations/node-reaction-sync.ts
new file mode 100644
index 00000000..7453d5f2
--- /dev/null
+++ b/desktop/src/main/mutations/node-reaction-sync.ts
@@ -0,0 +1,94 @@
+import { databaseContext } from '@/main/data/database-context';
+import { MutationHandler, MutationResult } from '@/types/mutations';
+import { NodeReactionSyncMutationInput } from '@/types/mutations/node-reaction-sync';
+import { ServerNodeReaction } from '@/types/nodes';
+
+export class NodeReactionSyncMutationHandler
+ implements MutationHandler<NodeReactionSyncMutationInput>
+{
+ public async handleMutation(
+ input: NodeReactionSyncMutationInput,
+ ): Promise<MutationResult<NodeReactionSyncMutationInput>> {
+ const workspace = await databaseContext.appDatabase
+ .selectFrom('workspaces')
+ .selectAll()
+ .where((eb) =>
+ eb.and([
+ eb('account_id', '=', input.accountId),
+ eb('workspace_id', '=', input.workspaceId),
+ ]),
+ )
+ .executeTakeFirst();
+
+ if (!workspace) {
+ return {
+ output: {
+ success: false,
+ },
+ };
+ }
+
+ const userId = workspace.user_id;
+ if (input.action === 'insert' && input.after) {
+ await this.insertNodeReaction(userId, input.after);
+ } else if (input.action === 'delete' && input.before) {
+ await this.deleteNodeReaction(userId, input.before);
+ }
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'node_reactions',
+ userId: userId,
+ },
+ ],
+ };
+ }
+
+ private async insertNodeReaction(
+ userId: string,
+ nodeReaction: ServerNodeReaction,
+ ): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ await workspaceDatabase
+ .insertInto('node_reactions')
+ .values({
+ node_id: nodeReaction.nodeId,
+ actor_id: nodeReaction.actorId,
+ reaction: nodeReaction.reaction,
+ created_at: nodeReaction.createdAt,
+ server_created_at: nodeReaction.serverCreatedAt,
+ })
+ .onConflict((ob) =>
+ ob.doUpdateSet({
+ server_created_at: nodeReaction.serverCreatedAt,
+ }),
+ )
+ .execute();
+ }
+
+ private async deleteNodeReaction(
+ userId: string,
+ nodeReaction: ServerNodeReaction,
+ ): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ await workspaceDatabase
+ .deleteFrom('node_reactions')
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', nodeReaction.nodeId),
+ eb('actor_id', '=', nodeReaction.actorId),
+ eb('reaction', '=', nodeReaction.reaction),
+ ]),
+ )
+ .execute();
+ }
+}
diff --git a/desktop/src/main/mutations/node-sync.ts b/desktop/src/main/mutations/node-sync.ts
new file mode 100644
index 00000000..608f1e16
--- /dev/null
+++ b/desktop/src/main/mutations/node-sync.ts
@@ -0,0 +1,137 @@
+import { databaseContext } from '@/main/data/database-context';
+import { MutationHandler, MutationResult } from '@/types/mutations';
+import { NodeSyncMutationInput } from '@/types/mutations/node-sync';
+import { ServerNode } from '@/types/nodes';
+import { fromUint8Array, toUint8Array } from 'js-base64';
+import * as Y from 'yjs';
+
+export class NodeSyncMutationHandler
+ implements MutationHandler<NodeSyncMutationInput>
+{
+ public async handleMutation(
+ input: NodeSyncMutationInput,
+ ): Promise<MutationResult<NodeSyncMutationInput>> {
+ const workspace = await databaseContext.appDatabase
+ .selectFrom('workspaces')
+ .selectAll()
+ .where((eb) =>
+ eb.and([
+ eb('account_id', '=', input.accountId),
+ eb('workspace_id', '=', input.workspaceId),
+ ]),
+ )
+ .executeTakeFirst();
+
+ if (!workspace) {
+ return {
+ output: {
+ success: false,
+ },
+ };
+ }
+
+ const userId = workspace.user_id;
+ if (input.action === 'insert' && input.after) {
+ await this.insertNode(userId, input.after);
+ } else if (input.action === 'update' && input.after) {
+ await this.updateNode(userId, input.after);
+ } else if (input.action === 'delete' && input.before) {
+ await this.deleteNode(userId, input.before);
+ }
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'nodes',
+ userId: userId,
+ },
+ ],
+ };
+ }
+
+ private async insertNode(userId: string, node: ServerNode): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ await workspaceDatabase
+ .insertInto('nodes')
+ .values({
+ id: node.id,
+ attributes: JSON.stringify(node.attributes),
+ state: node.state,
+ created_at: node.createdAt,
+ created_by: node.createdBy,
+ updated_by: node.updatedBy,
+ updated_at: node.updatedAt,
+ version_id: node.versionId,
+ server_created_at: node.serverCreatedAt,
+ server_updated_at: node.serverUpdatedAt,
+ server_version_id: node.versionId,
+ })
+ .onConflict((cb) =>
+ cb
+ .doUpdateSet({
+ server_created_at: node.serverCreatedAt,
+ server_updated_at: node.serverUpdatedAt,
+ server_version_id: node.versionId,
+ })
+ .where('version_id', '=', node.versionId),
+ )
+ .execute();
+ }
+
+ private async updateNode(userId: string, node: ServerNode): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ const existingNode = await workspaceDatabase
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', node.id)
+ .executeTakeFirst();
+
+ if (existingNode.version_id === node.versionId) {
+ return;
+ }
+
+ const doc = new Y.Doc({
+ guid: node.id,
+ });
+
+ Y.applyUpdate(doc, toUint8Array(existingNode.state));
+ Y.applyUpdate(doc, toUint8Array(node.state));
+
+ const attributesMap = doc.getMap('attributes');
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ await workspaceDatabase
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: node.updatedAt,
+ updated_by: node.updatedBy,
+ version_id: node.versionId,
+ server_created_at: node.serverCreatedAt,
+ server_updated_at: node.serverUpdatedAt,
+ server_version_id: node.versionId,
+ })
+ .where('id', '=', node.id)
+ .execute();
+ }
+
+ private async deleteNode(userId: string, node: ServerNode): Promise<void> {
+ const workspaceDatabase =
+ await databaseContext.getWorkspaceDatabase(userId);
+
+ await workspaceDatabase
+ .deleteFrom('nodes')
+ .where('id', '=', node.id)
+ .execute();
+ }
+}
diff --git a/desktop/src/main/mutations/page-create.ts b/desktop/src/main/mutations/page-create.ts
index 0d7291b2..e17a33d2 100644
--- a/desktop/src/main/mutations/page-create.ts
+++ b/desktop/src/main/mutations/page-create.ts
@@ -16,10 +16,6 @@ export class PageCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const siblings = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
@@ -55,7 +51,7 @@ export class PageCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/record-create.ts b/desktop/src/main/mutations/record-create.ts
index fc68e9cd..1fcaf868 100644
--- a/desktop/src/main/mutations/record-create.ts
+++ b/desktop/src/main/mutations/record-create.ts
@@ -15,10 +15,6 @@ export class RecordCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const lastChild = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
@@ -56,7 +52,7 @@ export class RecordCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/select-option-create.ts b/desktop/src/main/mutations/select-option-create.ts
index 1240c42d..1c5e67da 100644
--- a/desktop/src/main/mutations/select-option-create.ts
+++ b/desktop/src/main/mutations/select-option-create.ts
@@ -15,10 +15,6 @@ export class SelectOptionCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const lastChild = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
@@ -57,7 +53,7 @@ export class SelectOptionCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/server-create.ts b/desktop/src/main/mutations/server-create.ts
index a22ec93c..71acb73c 100644
--- a/desktop/src/main/mutations/server-create.ts
+++ b/desktop/src/main/mutations/server-create.ts
@@ -30,7 +30,7 @@ export class ServerCreateMutationHandler
output: {
success: true,
},
- changedTables: [
+ changes: [
{
type: 'app',
table: 'servers',
diff --git a/desktop/src/main/mutations/space-create.ts b/desktop/src/main/mutations/space-create.ts
index 1155b154..98048492 100644
--- a/desktop/src/main/mutations/space-create.ts
+++ b/desktop/src/main/mutations/space-create.ts
@@ -15,10 +15,6 @@ export class SpaceCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const spaceId = NeuronId.generate(NeuronId.Type.Space);
const pageId = NeuronId.generate(NeuronId.Type.Page);
const channelId = NeuronId.generate(NeuronId.Type.Channel);
@@ -71,7 +67,7 @@ export class SpaceCreateMutationHandler
output: {
id: spaceId,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/table-view-create.ts b/desktop/src/main/mutations/table-view-create.ts
index cb38f76a..b2a6c440 100644
--- a/desktop/src/main/mutations/table-view-create.ts
+++ b/desktop/src/main/mutations/table-view-create.ts
@@ -16,10 +16,6 @@ export class TableViewCreateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const siblings = await workspaceDatabase
.selectFrom('nodes')
.where((eb) =>
@@ -60,7 +56,7 @@ export class TableViewCreateMutationHandler
output: {
id: id,
},
- changedTables: [
+ changes: [
{
type: 'workspace',
table: 'nodes',
diff --git a/desktop/src/main/mutations/workspace-account-role-update.ts b/desktop/src/main/mutations/workspace-account-role-update.ts
index bdd433f7..54a07ff0 100644
--- a/desktop/src/main/mutations/workspace-account-role-update.ts
+++ b/desktop/src/main/mutations/workspace-account-role-update.ts
@@ -74,10 +74,6 @@ export class WorkspaceAccountRoleUpdateMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
await workspaceDatabase
.updateTable('nodes')
.set({
@@ -103,7 +99,7 @@ export class WorkspaceAccountRoleUpdateMutationHandler
output: {
success: true,
},
- changedTables: changedTables,
+ changes: changedTables,
};
}
}
diff --git a/desktop/src/main/mutations/workspace-accounts-invite.ts b/desktop/src/main/mutations/workspace-accounts-invite.ts
index 59dab2e6..af6d6da3 100644
--- a/desktop/src/main/mutations/workspace-accounts-invite.ts
+++ b/desktop/src/main/mutations/workspace-accounts-invite.ts
@@ -75,10 +75,6 @@ export class WorkspaceAccountsInviteMutationHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const usersToCreate: CreateNode[] = data.users.map((user) => {
return {
id: user.id,
@@ -113,7 +109,7 @@ export class WorkspaceAccountsInviteMutationHandler
output: {
success: true,
},
- changedTables: changedTables,
+ changes: changedTables,
};
}
}
diff --git a/desktop/src/main/mutations/workspace-create.ts b/desktop/src/main/mutations/workspace-create.ts
index 01a78e5e..37bc3f99 100644
--- a/desktop/src/main/mutations/workspace-create.ts
+++ b/desktop/src/main/mutations/workspace-create.ts
@@ -76,7 +76,7 @@ export class WorkspaceCreateMutationHandler
output: {
id: data.id,
},
- changedTables: changedTables,
+ changes: changedTables,
};
}
}
diff --git a/desktop/src/main/mutations/workspace-update.ts b/desktop/src/main/mutations/workspace-update.ts
index d307bee2..3129a8ef 100644
--- a/desktop/src/main/mutations/workspace-update.ts
+++ b/desktop/src/main/mutations/workspace-update.ts
@@ -72,7 +72,7 @@ export class WorkspaceUpdateMutationHandler
output: {
success: true,
},
- changedTables: changedTables,
+ changes: changedTables,
};
}
}
diff --git a/desktop/src/main/queries/breadcrumb-list.ts b/desktop/src/main/queries/breadcrumb-list.ts
index 89a8fcbb..16b3ebce 100644
--- a/desktop/src/main/queries/breadcrumb-list.ts
+++ b/desktop/src/main/queries/breadcrumb-list.ts
@@ -66,10 +66,6 @@ export class BreadcrumbListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<SelectNode>`
WITH RECURSIVE breadcrumb_nodes AS (
SELECT *
diff --git a/desktop/src/main/queries/database-get.ts b/desktop/src/main/queries/database-get.ts
index a2423a94..a00c6f0a 100644
--- a/desktop/src/main/queries/database-get.ts
+++ b/desktop/src/main/queries/database-get.ts
@@ -73,10 +73,6 @@ export class DatabaseGetQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<SelectNode>`
WITH database_node AS (
SELECT *
diff --git a/desktop/src/main/queries/database-view-list.ts b/desktop/src/main/queries/database-view-list.ts
index fe44ecd2..1f452781 100644
--- a/desktop/src/main/queries/database-view-list.ts
+++ b/desktop/src/main/queries/database-view-list.ts
@@ -72,10 +72,6 @@ export class DatabaseViewListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const rows = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
diff --git a/desktop/src/main/queries/document-get.ts b/desktop/src/main/queries/document-get.ts
index f97e9623..e2e8cb89 100644
--- a/desktop/src/main/queries/document-get.ts
+++ b/desktop/src/main/queries/document-get.ts
@@ -71,10 +71,6 @@ export class DocumentGetQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<SelectNode>`
WITH RECURSIVE document_nodes AS (
SELECT *
diff --git a/desktop/src/main/queries/message-list.ts b/desktop/src/main/queries/message-list.ts
index 54ee95b6..77173804 100644
--- a/desktop/src/main/queries/message-list.ts
+++ b/desktop/src/main/queries/message-list.ts
@@ -75,10 +75,6 @@ export class MessageListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const offset = input.page * input.count;
const query = sql<MessageRow>`
WITH message_nodes AS (
diff --git a/desktop/src/main/queries/node-collaborator-list.tsx b/desktop/src/main/queries/node-collaborator-list.tsx
index 0404238a..1f0d2af4 100644
--- a/desktop/src/main/queries/node-collaborator-list.tsx
+++ b/desktop/src/main/queries/node-collaborator-list.tsx
@@ -79,10 +79,6 @@ export class NodeCollaboratorListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<NodeCollaboratorRow>`
WITH RECURSIVE ancestors(id, parent_id, level) AS (
SELECT id, parent_id, 0 AS level
diff --git a/desktop/src/main/queries/node-collaborator-search.ts b/desktop/src/main/queries/node-collaborator-search.ts
index 0ef9fe3e..498538a8 100644
--- a/desktop/src/main/queries/node-collaborator-search.ts
+++ b/desktop/src/main/queries/node-collaborator-search.ts
@@ -66,10 +66,6 @@ export class NodeCollaboratorSearchQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<SelectNode>`
SELECT n.*
FROM nodes n
diff --git a/desktop/src/main/queries/node-get.ts b/desktop/src/main/queries/node-get.ts
index df78e1d8..d9716a94 100644
--- a/desktop/src/main/queries/node-get.ts
+++ b/desktop/src/main/queries/node-get.ts
@@ -60,10 +60,6 @@ export class NodeGetQueryHandler implements QueryHandler<NodeGetQueryInput> {
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const row = await workspaceDatabase
.selectFrom('nodes')
.selectAll()
diff --git a/desktop/src/main/queries/record-get.ts b/desktop/src/main/queries/record-get.ts
index 8a02d4c5..bf3d2703 100644
--- a/desktop/src/main/queries/record-get.ts
+++ b/desktop/src/main/queries/record-get.ts
@@ -65,10 +65,6 @@ export class RecordGetQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<SelectNode>`
WITH record_node AS (
SELECT *
diff --git a/desktop/src/main/queries/record-list.ts b/desktop/src/main/queries/record-list.ts
index 13c52b88..5b4f2015 100644
--- a/desktop/src/main/queries/record-list.ts
+++ b/desktop/src/main/queries/record-list.ts
@@ -88,10 +88,6 @@ export class RecordListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const database = await this.fetchDatabase(input.userId, input.databaseId);
const filterQuery = this.buildFiltersQuery(input.filters, database.fields);
const orderByQuery = `ORDER BY ${input.sorts.length > 0 ? this.buildSortOrdersQuery(input.sorts, database.fields) : 'n."index" ASC'}`;
diff --git a/desktop/src/main/queries/sidebar-chat-list.ts b/desktop/src/main/queries/sidebar-chat-list.ts
index 791cb7b0..ea2bfed8 100644
--- a/desktop/src/main/queries/sidebar-chat-list.ts
+++ b/desktop/src/main/queries/sidebar-chat-list.ts
@@ -71,10 +71,6 @@ export class SidebarChatListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<ChatRow>`
WITH chat_nodes AS (
SELECT *
diff --git a/desktop/src/main/queries/sidebar-space-list.ts b/desktop/src/main/queries/sidebar-space-list.ts
index c1c4dd63..b1e67fdd 100644
--- a/desktop/src/main/queries/sidebar-space-list.ts
+++ b/desktop/src/main/queries/sidebar-space-list.ts
@@ -68,10 +68,6 @@ export class SidebarSpaceListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<SelectNode>`
WITH space_nodes AS (
SELECT *
diff --git a/desktop/src/main/queries/user-search.ts b/desktop/src/main/queries/user-search.ts
index f7f8672e..38872c30 100644
--- a/desktop/src/main/queries/user-search.ts
+++ b/desktop/src/main/queries/user-search.ts
@@ -73,10 +73,6 @@ export class UserSearchQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const query = sql<SelectNode>`
SELECT n.*
FROM nodes n
diff --git a/desktop/src/main/queries/workspace-user-list.ts b/desktop/src/main/queries/workspace-user-list.ts
index 7e151ea7..048fcedb 100644
--- a/desktop/src/main/queries/workspace-user-list.ts
+++ b/desktop/src/main/queries/workspace-user-list.ts
@@ -66,10 +66,6 @@ export class WorkspaceUserListQueryHandler
input.userId,
);
- if (workspaceDatabase === null) {
- throw new Error('Workspace database not found.');
- }
-
const offset = input.page * input.count;
const rows = await workspaceDatabase
.selectFrom('nodes')
diff --git a/desktop/src/main/sockets/socket-connection.ts b/desktop/src/main/sockets/socket-connection.ts
index b110c516..0ee9204a 100644
--- a/desktop/src/main/sockets/socket-connection.ts
+++ b/desktop/src/main/sockets/socket-connection.ts
@@ -3,18 +3,14 @@ import { Account } from '@/types/accounts';
import { Server } from '@/types/servers';
import { buildSynapseUrl } from '@/lib/servers';
import { BackoffCalculator } from '@/lib/backoff-calculator';
-
-export type SocketMessage = {
- type: string;
- payload: any;
-};
+import { MessageInput } from '@/operations/messages';
+import { mediator } from '@/main/mediator';
export class SocketConnection {
private readonly server: Server;
private readonly account: Account;
private socket: WebSocket | null;
private backoffCalculator: BackoffCalculator;
- private listeners: Map<string, ((payload: any) => void)[]>;
private closingCount: number;
constructor(server: Server, account: Account) {
@@ -22,7 +18,6 @@ export class SocketConnection {
this.account = account;
this.socket = null;
this.backoffCalculator = new BackoffCalculator();
- this.listeners = new Map();
this.closingCount = 0;
}
@@ -39,7 +34,7 @@ export class SocketConnection {
buildSynapseUrl(this.server, this.account.deviceId),
);
- this.socket.onmessage = (event) => {
+ this.socket.onmessage = async (event) => {
let data: string;
if (typeof event.data === 'string') {
@@ -51,11 +46,8 @@ export class SocketConnection {
return;
}
- const message: SocketMessage = JSON.parse(data);
- const listeners = this.listeners.get(message.type) || [];
- for (const listener of listeners) {
- listener(message.payload);
- }
+ const message: MessageInput = JSON.parse(data);
+ await mediator.executeMessage(message);
};
this.socket.onopen = () => {
@@ -104,16 +96,4 @@ export class SocketConnection {
}
}
}
-
- public send(message: SocketMessage): void {
- if (this.isConnected()) {
- this.socket?.send(JSON.stringify(message));
- }
- }
-
- public on(type: string, listener: (payload: any) => void): void {
- const listeners = this.listeners.get(type) || [];
- listeners.push(listener);
- this.listeners.set(type, listeners);
- }
}
diff --git a/desktop/src/main/synchronizer.ts b/desktop/src/main/synchronizer.ts
new file mode 100644
index 00000000..57369212
--- /dev/null
+++ b/desktop/src/main/synchronizer.ts
@@ -0,0 +1,7 @@
+class Synchronizer {
+ public init() {
+ console.log('init synchronizer');
+ }
+}
+
+export const synchronizer = new Synchronizer();
diff --git a/desktop/src/operations/messages/index.ts b/desktop/src/operations/messages/index.ts
new file mode 100644
index 00000000..5374a880
--- /dev/null
+++ b/desktop/src/operations/messages/index.ts
@@ -0,0 +1,7 @@
+export interface MessageMap {}
+
+export type MessageInput = MessageMap[keyof MessageMap];
+
+export interface MessageHandler<T extends MessageInput> {
+ handleMessage: (input: T) => Promise<void>;
+}
diff --git a/desktop/src/operations/messages/server-mutation.ts b/desktop/src/operations/messages/server-mutation.ts
new file mode 100644
index 00000000..2509b116
--- /dev/null
+++ b/desktop/src/operations/messages/server-mutation.ts
@@ -0,0 +1,13 @@
+import { ServerMutation } from '@/types/mutations';
+
+export type ServerMutationMessageInput = {
+ type: 'server_mutation';
+ accountId: string;
+ mutation: ServerMutation;
+};
+
+declare module '@/operations/messages' {
+ interface MessageMap {
+ server_mutation: ServerMutationMessageInput;
+ }
+}
diff --git a/desktop/src/types/mutations/index.ts b/desktop/src/types/mutations/index.ts
index 6f13ecfa..1a5b83de 100644
--- a/desktop/src/types/mutations/index.ts
+++ b/desktop/src/types/mutations/index.ts
@@ -11,7 +11,7 @@ export interface MutationHandler<T extends MutationInput> {
export type MutationResult<T extends MutationInput> = {
output: MutationMap[T['type']]['output'];
- changedTables?: MutationChange[];
+ changes?: MutationChange[];
};
export type MutationChange = AppMutationChange | WorkspaceMutationChange;
@@ -31,7 +31,7 @@ export type ServerMutation = {
id: string;
table: string;
action: string;
- workspaceId: string;
+ workspaceId: string | null;
before: any | null;
after: any | null;
};
diff --git a/desktop/src/types/mutations/node-collaborator-sync.ts b/desktop/src/types/mutations/node-collaborator-sync.ts
new file mode 100644
index 00000000..df6859cf
--- /dev/null
+++ b/desktop/src/types/mutations/node-collaborator-sync.ts
@@ -0,0 +1,22 @@
+export type NodeCollaboratorSyncMutationInput = {
+ type: 'node_collaborator_sync';
+ accountId: string;
+ workspaceId: string;
+ id: string;
+ action: string;
+ before: any;
+ after: any;
+};
+
+export type NodeCollaboratorSyncMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/types/mutations' {
+ interface MutationMap {
+ node_collaborator_sync: {
+ input: NodeCollaboratorSyncMutationInput;
+ output: NodeCollaboratorSyncMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/types/mutations/node-reaction-sync.ts b/desktop/src/types/mutations/node-reaction-sync.ts
new file mode 100644
index 00000000..fae0275f
--- /dev/null
+++ b/desktop/src/types/mutations/node-reaction-sync.ts
@@ -0,0 +1,22 @@
+export type NodeReactionSyncMutationInput = {
+ type: 'node_reaction_sync';
+ accountId: string;
+ workspaceId: string;
+ id: string;
+ action: string;
+ before: any;
+ after: any;
+};
+
+export type NodeReactionSyncMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/types/mutations' {
+ interface MutationMap {
+ node_reaction_sync: {
+ input: NodeReactionSyncMutationInput;
+ output: NodeReactionSyncMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/types/mutations/node-sync.ts b/desktop/src/types/mutations/node-sync.ts
new file mode 100644
index 00000000..1ff8f884
--- /dev/null
+++ b/desktop/src/types/mutations/node-sync.ts
@@ -0,0 +1,22 @@
+export type NodeSyncMutationInput = {
+ type: 'node_sync';
+ accountId: string;
+ workspaceId: string;
+ id: string;
+ action: string;
+ before: any;
+ after: any;
+};
+
+export type NodeSyncMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/types/mutations' {
+ interface MutationMap {
+ node_sync: {
+ input: NodeSyncMutationInput;
+ output: NodeSyncMutationOutput;
+ };
+ }
+}