Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch-based Realtime Collaboration #60

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"tough-cookie": "4.1.3",
"postcss": "8.4.31",
"crypto-js": "4.2.0"
},
"dependencies": {
"fast-json-patch": "^3.1.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @ts-ignore
import WebSocket from 'ws';
import { randomString } from '../../utils';
import { DiagramFileStorageService } from '../diagram-storage/diagram-file-storage-service';
import { Collaborator } from 'shared/src/main/collaborator-dto';
import { updateSelectedByArray, removeDisconnectedCollaborator } from 'shared/src/main/services/collaborator-highlight';
import { SelectionChange } from 'shared/src/main/selection-dto';
import { applyPatch } from 'fast-json-patch';
import { Patch } from '@ls1intum/apollon';

type Client = { token: string; collaborators: Collaborator };
type Client = { token: string; collaborator: Collaborator };

export class CollaborationService {
private wsServer: any;
Expand Down Expand Up @@ -33,17 +34,19 @@ export class CollaborationService {
socket.isAlive = true;
});
socket.on('message', (message: any) => {
const { token, collaborators, diagram, selectedElements } = JSON.parse(message);
const { token, collaborator, patch, selection } = JSON.parse(message);
if (token) {
if (diagram) {
this.onDiagramUpdate(socket, token, collaborators, diagram, selectedElements);
if (patch) {
this.onDiagramPatch(socket, token, patch, collaborator);
} else if (selection) {
this.onSelection(socket, token, selection, collaborator);
} else {
this.onConnection(socket, token, collaborators);
this.onConnection(socket, token, collaborator);
}
} else {
// Case where only collaborator object is updated
if (collaborators?.name !== '') {
this.onCollaboratorUpdate(socket, collaborators);
if (collaborator?.name !== '') {
this.onCollaboratorUpdate(socket, collaborator);
}
}
});
Expand All @@ -69,7 +72,6 @@ export class CollaborationService {

onConnectionLost = (socket: any) => {
const token = this.clients[socket.apollonId]?.token;
const prevTokenClients = this.getTokenClients(socket.apollonId, false);
const tokenClients = this.getTokenClients(socket.apollonId, true);

this.wsServer.clients.forEach((clientSocket: any) => {
Expand All @@ -78,83 +80,85 @@ export class CollaborationService {
clientSocket.readyState === WebSocket.OPEN &&
this.clients[clientSocket.apollonId]?.token === token
) {
// Remove disconnected collaborators from selectedBy Array
const disconnectedClient = prevTokenClients.filter((x) => !tokenClients.includes(x));
this.diagramService.getDiagramByLink(token).then((diagram) => {
const updatedElement = removeDisconnectedCollaborator(
disconnectedClient[0].collaborators,
diagram?.model.elements,
);
if (diagram && diagram.model && diagram.model.elements) {
diagram.model.elements = updatedElement!;
const diagramService = new DiagramFileStorageService();
diagramService.saveDiagram(diagram, token, true);
}
clientSocket.send(
JSON.stringify({
token,
collaborators: tokenClients.map((client) => client.collaborators),
diagram,
}),
);
});
clientSocket.send(
JSON.stringify({
token,
collaborators: tokenClients.map((client) => client.collaborator),
}),
);
}
});
};

onCollaboratorUpdate = (socket: any, collaborators: Collaborator) => {
this.clients[socket.apollonId] = { ...this.clients[socket.apollonId], collaborators };
onCollaboratorUpdate = (socket: any, collaborator: Collaborator) => {
this.clients[socket.apollonId] = { ...this.clients[socket.apollonId], collaborator };
const token = this.clients[socket.apollonId]?.token;
const tokenClients = this.getTokenClients(socket.apollonId, false);
this.wsServer.clients.forEach((clientSocket: any) => {
if (clientSocket.readyState === WebSocket.OPEN && this.clients[clientSocket.apollonId].token === token) {
clientSocket.send(JSON.stringify({ collaborators: tokenClients.map((client) => client.collaborators) }));
clientSocket.send(JSON.stringify({ collaborators: tokenClients.map((client) => client.collaborator) }));
}
});
};

onConnection = (socket: any, token: string, collaborators: Collaborator) => {
this.clients[socket.apollonId] = { token, collaborators };
onConnection = (socket: any, token: string, collaborator: Collaborator) => {
this.clients[socket.apollonId] = { token, collaborator };
const tokenClients = this.getTokenClients(socket.apollonId, false);
this.wsServer.clients.forEach((clientSocket: any) => {
if (clientSocket.readyState === WebSocket.OPEN && this.clients[clientSocket.apollonId]?.token === token) {
if (clientSocket === socket) {
this.diagramService.getDiagramByLink(token).then((diagram) => {
clientSocket.send(
JSON.stringify({ collaborators: tokenClients.map((client) => client.collaborators), diagram }),
JSON.stringify({ collaborators: tokenClients.map((client) => client.collaborator), diagram }),
);
});
} else {
clientSocket.send(JSON.stringify({ collaborators: tokenClients.map((client) => client.collaborators) }));
clientSocket.send(JSON.stringify({ collaborators: tokenClients.map((client) => client.collaborator) }));
}
}
});
};

onDiagramUpdate = (socket: any, token: string, collaborators: Collaborator, diagram: any, selectedElements: any) => {
const diagramService = new DiagramFileStorageService();
diagramService.saveDiagram(diagram, token, true);
this.clients[socket.apollonId] = { token, collaborators };
onDiagramPatch = async (socket: any, token: string, patch: Patch, collaborator: Collaborator) => {
const diagram = await this.diagramService.getDiagramByLink(token);
diagram!.model = applyPatch(diagram!.model, patch).newDocument;
this.diagramService.saveDiagram(diagram!, token, true);

const tokenClients = this.getTokenClients(socket.apollonId, false);
this.clients[socket.apollonId] = { token, collaborator };

this.wsServer.clients.forEach((clientSocket: any) => {
if (
clientSocket !== socket &&
clientSocket.readyState === WebSocket.OPEN &&
this.clients[clientSocket.apollonId]?.token === token
) {
if (selectedElements) {
const selElemIds = selectedElements;
const elements = diagram.model.elements;
const updatedElement = updateSelectedByArray(selElemIds, elements, collaborators.name, collaborators.color);
clientSocket.send(
JSON.stringify({
collaborators: tokenClients.map((client) => client.collaborator),
patch,
originator: collaborator
}),
);
}
});
};

if (diagram && diagram.model && diagram.model.elements) {
diagram.model.elements = updatedElement!;
}
}
onSelection = async (socket: any, token: string, selection: SelectionChange, collaborator: Collaborator) => {
const tokenClients = this.getTokenClients(socket.apollonId, false);
this.clients[socket.apollonId] = { token, collaborator };

this.wsServer.clients.forEach((clientSocket: any) => {
if (
clientSocket !== socket &&
clientSocket.readyState === WebSocket.OPEN &&
this.clients[clientSocket.apollonId]?.token === token
) {
clientSocket.send(
JSON.stringify({ collaborators: tokenClients.map((client) => client.collaborators), diagram }),
JSON.stringify({
collaborators: tokenClients.map((client) => client.collaborator),
selection,
originator: collaborator
}),
);
}
});
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/src/main/collaborator-dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export class Collaborator {
name: string;
export interface CollaboratorType {
color: string;
name: string;
}

constructor(name: string, color: string) {
this.name = name;
this.color = color;
export class Collaborator implements CollaboratorType {
constructor(public name: string, public color: string) {
}
}
9 changes: 9 additions & 0 deletions packages/shared/src/main/selection-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface SelectionChangeType {
selected: string[];
deselected: string[];
}

export class SelectionChange implements SelectionChangeType {
constructor(public selected: string[], public deselected: string[]) {
}
}
74 changes: 0 additions & 74 deletions packages/shared/src/main/services/collaborator-highlight.ts

This file was deleted.

Loading