Skip to content

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

Implement node reactions sync

Hakan Shehu38bbc9707bf9
diff --git a/desktop/src/data/workspace-manager.ts b/desktop/src/data/workspace-manager.ts
index 4e6b98e5..048e9260 100644
--- a/desktop/src/data/workspace-manager.ts
+++ b/desktop/src/data/workspace-manager.ts
@@ -20,7 +20,7 @@ import { ServerMutation } from '@/types/mutations';
import { eventBus } from '@/lib/event-bus';
import { AxiosInstance } from 'axios';
import { debounce, isEqual } from 'lodash';
-import { ServerNode } from '@/types/nodes';
+import { ServerNode, ServerNodeReaction } from '@/types/nodes';
import { SelectNode } from '@/data/schemas/workspace';
export class WorkspaceManager {
@@ -254,6 +254,10 @@ export class WorkspaceManager {
await this.syncNodeFromServer(node);
}
+ for (const nodeReaction of data.nodeReactions) {
+ await this.syncNodeReactionFromServer(nodeReaction);
+ }
+
this.workspace.synced = true;
return true;
}
@@ -307,6 +311,26 @@ export class WorkspaceManager {
}
}
+ private async syncNodeReactionFromServer(
+ nodeReaction: ServerNodeReaction,
+ ): Promise<void> {
+ await this.database
+ .insertInto('node_reactions')
+ .values({
+ node_id: nodeReaction.nodeId,
+ reactor_id: nodeReaction.reactorId,
+ reaction: nodeReaction.reaction,
+ created_at: nodeReaction.createdAt,
+ server_created_at: nodeReaction.serverCreatedAt,
+ })
+ .onConflict((ob) =>
+ ob.doUpdateSet({
+ server_created_at: nodeReaction.serverCreatedAt,
+ }),
+ )
+ .execute();
+ }
+
public shouldUpdateNodeFromServer(
localNode: SelectNode,
serverNode: ServerNode,
diff --git a/desktop/src/types/nodes.ts b/desktop/src/types/nodes.ts
index 10bc5438..385a79c9 100644
--- a/desktop/src/types/nodes.ts
+++ b/desktop/src/types/nodes.ts
@@ -62,6 +62,15 @@ export type ServerNodeAttributes = {
[key: string]: any;
};
+export type ServerNodeReaction = {
+ nodeId: string;
+ reactorId: string;
+ reaction: string;
+ workspaceId: string;
+ createdAt: string;
+ serverCreatedAt: string;
+};
+
export type NodeInsertInput = {
id: string;
attributes: LocalNodeAttributes;
diff --git a/desktop/src/types/workspaces.ts b/desktop/src/types/workspaces.ts
index b1769169..2a1d320a 100644
--- a/desktop/src/types/workspaces.ts
+++ b/desktop/src/types/workspaces.ts
@@ -1,4 +1,4 @@
-import { ServerNode } from '@/types/nodes';
+import { ServerNode, ServerNodeReaction } from '@/types/nodes';
export enum WorkspaceRole {
Owner = 1,
@@ -21,6 +21,7 @@ export type Workspace = {
export type WorkspaceSyncData = {
nodes: ServerNode[];
+ nodeReactions: ServerNodeReaction[];
};
export type SidebarNode = {
diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.ts
index e8298ea9..a1aabca7 100644
--- a/server/src/routes/sync.ts
+++ b/server/src/routes/sync.ts
@@ -1,7 +1,7 @@
import { NeuronRequest, NeuronResponse } from '@/types/api';
import { database } from '@/data/database';
import { Router } from 'express';
-import { ServerNode } from '@/types/nodes';
+import { ServerNode, ServerNodeReaction } from '@/types/nodes';
export const syncRouter = Router();
@@ -15,6 +15,12 @@ syncRouter.get(
.where('workspace_id', '=', workspaceId)
.execute();
+ const nodeReactionRows = await database
+ .selectFrom('node_reactions')
+ .selectAll()
+ .where('workspace_id', '=', workspaceId)
+ .execute();
+
const nodes: ServerNode[] = nodeRows.map((node) => {
return {
id: node.id,
@@ -34,8 +40,22 @@ syncRouter.get(
};
});
+ const nodeReactions: ServerNodeReaction[] = nodeReactionRows.map(
+ (nodeReaction) => {
+ return {
+ nodeId: nodeReaction.node_id,
+ reactorId: nodeReaction.reactor_id,
+ reaction: nodeReaction.reaction,
+ workspaceId: nodeReaction.workspace_id,
+ createdAt: nodeReaction.created_at.toISOString(),
+ serverCreatedAt: nodeReaction.server_created_at.toISOString(),
+ };
+ },
+ );
+
res.status(200).json({
nodes,
+ nodeReactions,
});
},
);