Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Add the case of adding new users in socket connections
Hakan Shehu38c64967b359
diff --git a/server/src/consumers/changes-subcriber.ts b/server/src/consumers/changes-subcriber.tsindex ceca04b6..43e6fadc 100644--- a/server/src/consumers/changes-subcriber.ts+++ b/server/src/consumers/changes-subcriber.ts@@ -1,5 +1,6 @@import { database } from '@/data/database';import { redis, CHANNEL_NAMES } from '@/data/redis';+import { getIdType, IdType } from '@/lib/id';import { socketManager } from '@/sockets/socket-manager';import { ServerNodeChangeEvent } from '@/types/sync';@@ -11,6 +12,39 @@ export const initChangesSubscriber = async () => {const handleEvent = async (event: string) => {const data: ServerNodeChangeEvent = JSON.parse(event);+ if (data.type === 'node_create') {+ const id = data.nodeId;+ const idType = getIdType(id);+ if (idType === IdType.User) {+ const workspaceUser = await database+ .selectFrom('workspace_users')+ .selectAll()+ .where('id', '=', id)+ .executeTakeFirst();++ if (!workspaceUser) {+ return;+ }++ const devices = await database+ .selectFrom('devices')+ .selectAll()+ .where('account_id', '=', workspaceUser.account_id)+ .execute();++ for (const device of devices) {+ const socketConnection = socketManager.getConnection(device.id);+ if (socketConnection === undefined) {+ continue;+ }++ socketConnection.addWorkspaceUser({+ workspaceId: data.workspaceId,+ userId: id,+ });+ }+ }+ }const userDevices = new Map<string, string[]>();for (const connection of socketManager.getConnections()) {diff --git a/server/src/queues/events.ts b/server/src/queues/events.tsindex 7d17401e..cb7817b7 100644--- a/server/src/queues/events.ts+++ b/server/src/queues/events.ts@@ -99,7 +99,7 @@ const handleNodeCreatedEvent = async (.onConflict((cb) => cb.doNothing()).execute();- await publishChange(event.id, event.workspaceId);+ await publishChange(event.id, event.workspaceId, 'node_create');};const handleNodeUpdatedEvent = async (@@ -174,7 +174,7 @@ const handleNodeUpdatedEvent = async (.execute();}- await publishChange(event.id, event.workspaceId);+ await publishChange(event.id, event.workspaceId, 'node_update');};const extractCollaboratorIds = (collaborators: ServerNodeAttributes) => {@@ -188,16 +188,18 @@ const extractCollaboratorIds = (collaborators: ServerNodeAttributes) => {const handleNodeDeletedEvent = async (event: NodeDeletedEvent,): Promise<void> => {- await publishChange(event.id, event.workspaceId);+ await publishChange(event.id, event.workspaceId, 'node_delete');};const publishChange = async (nodeId: string,workspaceId: string,+ type: 'node_create' | 'node_update' | 'node_delete',): Promise<void> => {const changeJson = JSON.stringify({nodeId,workspaceId,+ type,});await redis.publish(CHANNEL_NAMES.CHANGES, changeJson);diff --git a/server/src/sockets/socket-connection.ts b/server/src/sockets/socket-connection.tsindex 9ca3632d..715be31b 100644--- a/server/src/sockets/socket-connection.ts+++ b/server/src/sockets/socket-connection.ts@@ -43,6 +43,15 @@ export class SocketConnection {return this.workspaceUsers;}+ public addWorkspaceUser(workspaceUser: WorkspaceUser): void {+ if (this.workspaceUsers.find((wu) => wu.userId === workspaceUser.userId)) {+ return;+ }++ this.workspaceUsers.push(workspaceUser);+ this.sendPendingChanges();+ }+private async fetchWorkspaceUsers(): Promise<void> {const workspaceUsers = await database.selectFrom('workspace_users')diff --git a/server/src/types/sync.ts b/server/src/types/sync.tsindex 06e418cb..7ab7e949 100644--- a/server/src/types/sync.ts+++ b/server/src/types/sync.ts@@ -52,4 +52,5 @@ export type LocalNodeChangeData =export type ServerNodeChangeEvent = {workspaceId: string;nodeId: string;+ type: 'node_create' | 'node_update' | 'node_delete';};