feat: Reintroduce collaboration feature (#10602)

This commit is contained in:
Tomi Turtiainen
2024-09-03 17:52:12 +03:00
committed by GitHub
parent 35e6a87cba
commit 2ea2bfe762
22 changed files with 1046 additions and 23 deletions

View File

@@ -15,11 +15,13 @@ import { OrchestrationService } from '@/services/orchestration.service';
import { SSEPush } from './sse.push';
import { WebSocketPush } from './websocket.push';
import type { PushResponse, SSEPushRequest, WebSocketPushRequest } from './types';
import type { OnPushMessage, PushResponse, SSEPushRequest, WebSocketPushRequest } from './types';
import { TypedEmitter } from '@/typed-emitter';
import type { User } from '@/databases/entities/user';
type PushEvents = {
editorUiConnected: string;
message: OnPushMessage;
};
const useWebSockets = config.getEnv('push.backend') === 'websocket';
@@ -33,16 +35,21 @@ const useWebSockets = config.getEnv('push.backend') === 'websocket';
*/
@Service()
export class Push extends TypedEmitter<PushEvents> {
public isBidirectional = useWebSockets;
private backend = useWebSockets ? Container.get(WebSocketPush) : Container.get(SSEPush);
constructor(private readonly orchestrationService: OrchestrationService) {
super();
if (useWebSockets) this.backend.on('message', (msg) => this.emit('message', msg));
}
handleRequest(req: SSEPushRequest | WebSocketPushRequest, res: PushResponse) {
const {
ws,
query: { pushRef },
user,
} = req;
if (!pushRef) {
@@ -55,9 +62,9 @@ export class Push extends TypedEmitter<PushEvents> {
}
if (req.ws) {
(this.backend as WebSocketPush).add(pushRef, req.ws);
(this.backend as WebSocketPush).add(pushRef, user.id, req.ws);
} else if (!useWebSockets) {
(this.backend as SSEPush).add(pushRef, { req, res });
(this.backend as SSEPush).add(pushRef, user.id, { req, res });
} else {
res.status(401).send('Unauthorized');
return;
@@ -90,6 +97,10 @@ export class Push extends TypedEmitter<PushEvents> {
return this.backend;
}
sendToUsers(type: IPushDataType, data: unknown, userIds: Array<User['id']>) {
this.backend.sendToUsers(type, data, userIds);
}
@OnShutdown()
onShutdown() {
this.backend.closeAllConnections();