From 954a3bb9e75909fc423152993ffd18e15299abbc Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Wed, 7 Aug 2024 11:40:50 +0500 Subject: [PATCH] Fix love connection (#6275) Signed-off-by: Denis Bykhov --- models/love/src/index.ts | 2 ++ packages/core/src/memdb.ts | 13 +++++++++---- packages/presentation/src/plugin.ts | 3 ++- plugins/client-resources/src/connection.ts | 2 +- plugins/guest-resources/src/connect.ts | 5 ++++- .../love-resources/src/components/ControlExt.svelte | 1 - plugins/love-resources/src/components/Room.svelte | 7 ++++++- plugins/love-resources/src/utils.ts | 11 +++++++---- plugins/love/src/index.ts | 1 + plugins/workbench-resources/src/connect.ts | 3 +++ server-plugins/love-resources/src/index.ts | 5 +++-- 11 files changed, 38 insertions(+), 15 deletions(-) diff --git a/models/love/src/index.ts b/models/love/src/index.ts index b71ee1ef3e..e92ef5a5a9 100644 --- a/models/love/src/index.ts +++ b/models/love/src/index.ts @@ -87,6 +87,8 @@ export class TParticipantInfo extends TDoc implements ParticipantInfo { x!: number y!: number + + sessionId!: string | null } @Model(love.class.JoinRequest, core.class.Doc, DOMAIN_TRANSIENT) diff --git a/packages/core/src/memdb.ts b/packages/core/src/memdb.ts index 5fe5dc5200..42cbfb9c87 100644 --- a/packages/core/src/memdb.ts +++ b/packages/core/src/memdb.ts @@ -337,13 +337,18 @@ export class ModelDb extends MemDb { } protected async txUpdateDoc (tx: TxUpdateDoc): Promise { - const doc = this.getObject(tx.objectId) as any - TxProcessor.updateDoc2Doc(doc, tx) - return tx.retrieve === true ? { object: doc } : {} + try { + const doc = this.getObject(tx.objectId) as any + TxProcessor.updateDoc2Doc(doc, tx) + return tx.retrieve === true ? { object: doc } : {} + } catch (err: any) {} + return {} } protected async txRemoveDoc (tx: TxRemoveDoc): Promise { - this.delDoc(tx.objectId) + try { + this.delDoc(tx.objectId) + } catch (err: any) {} return {} } diff --git a/packages/presentation/src/plugin.ts b/packages/presentation/src/plugin.ts index 05efb3e116..93adbf2540 100644 --- a/packages/presentation/src/plugin.ts +++ b/packages/presentation/src/plugin.ts @@ -136,7 +136,8 @@ export default plugin(presentationId, { Endpoint: '' as Metadata, FrontUrl: '' as Asset, PreviewConfig: '' as Metadata, - ClientHook: '' as Metadata + ClientHook: '' as Metadata, + SessionId: '' as Metadata }, status: { FileTooLarge: '' as StatusCode diff --git a/plugins/client-resources/src/connection.ts b/plugins/client-resources/src/connection.ts index 249496e044..138ad75cc3 100644 --- a/plugins/client-resources/src/connection.ts +++ b/plugins/client-resources/src/connection.ts @@ -261,7 +261,7 @@ class Connection implements ClientConnection { void this.opt?.onConnect?.( (resp as HelloResponse).reconnect === true ? ClientConnectEvent.Reconnected : ClientConnectEvent.Connected, - null + this.sessionId ) this.schedulePing(socketId) return diff --git a/plugins/guest-resources/src/connect.ts b/plugins/guest-resources/src/connect.ts index c9ef545952..69901c048e 100644 --- a/plugins/guest-resources/src/connect.ts +++ b/plugins/guest-resources/src/connect.ts @@ -72,9 +72,12 @@ export async function connect (title: string): Promise { }) }, // We need to refresh all active live queries and clear old queries. - onConnect: (event: ClientConnectEvent) => { + onConnect: (event: ClientConnectEvent, data: any) => { console.log('WorkbenchClient: onConnect', event) try { + if (event === ClientConnectEvent.Connected) { + setMetadata(presentation.metadata.SessionId, data) + } if ((_clientSet && event === ClientConnectEvent.Connected) || event === ClientConnectEvent.Refresh) { void refreshClient(true) } diff --git a/plugins/love-resources/src/components/ControlExt.svelte b/plugins/love-resources/src/components/ControlExt.svelte index a08fce5cfe..5cf23602a9 100644 --- a/plugins/love-resources/src/components/ControlExt.svelte +++ b/plugins/love-resources/src/components/ControlExt.svelte @@ -57,7 +57,6 @@ rooms } from '../stores' import { - connectRoom, disconnect, getRoomName, isCameraEnabled, diff --git a/plugins/love-resources/src/components/Room.svelte b/plugins/love-resources/src/components/Room.svelte index e06d5d4725..3f0bb1e38a 100644 --- a/plugins/love-resources/src/components/Room.svelte +++ b/plugins/love-resources/src/components/Room.svelte @@ -43,6 +43,7 @@ } from '../utils' import ControlBar from './ControlBar.svelte' import ParticipantView from './ParticipantView.svelte' + import presentation from '@hcengineering/presentation' export let withVideo: boolean export let room: TypeRoom @@ -223,7 +224,11 @@ await $storePromise - if (!$isConnected && !$isCurrentInstanceConnected) { + if ( + !$isConnected && + !$isCurrentInstanceConnected && + $myInfo?.sessionId === getMetadata(presentation.metadata.SessionId) + ) { const info = $infos.filter((p) => p.room === room._id) await tryConnect($personByIdStore, $myInfo, room, info, $myRequests, $invites) } diff --git a/plugins/love-resources/src/utils.ts b/plugins/love-resources/src/utils.ts index 2c3d7ff42f..0fa2f60d4b 100644 --- a/plugins/love-resources/src/utils.ts +++ b/plugins/love-resources/src/utils.ts @@ -485,14 +485,16 @@ async function moveToRoom ( y: number, currentInfo: ParticipantInfo | undefined, currentPerson: Person, - room: Room + room: Room, + sessionId: string | null ): Promise { const client = getClient() if (currentInfo !== undefined) { await client.diffUpdate(currentInfo, { x, y, - room: room._id + room: room._id, + sessionId }) } else { await client.createDoc(love.class.ParticipantInfo, core.space.Workspace, { @@ -500,7 +502,8 @@ async function moveToRoom ( y, room: room._id, person: currentPerson._id, - name: currentPerson.name + name: currentPerson.name, + sessionId }) } const loc = getCurrentLocation() @@ -529,7 +532,7 @@ export async function connectRoom ( room: Room ): Promise { await disconnect() - await moveToRoom(x, y, currentInfo, currentPerson, room) + await moveToRoom(x, y, currentInfo, currentPerson, room, getMetadata(presentation.metadata.SessionId) ?? null) await connectLK(currentPerson, room) } diff --git a/plugins/love/src/index.ts b/plugins/love/src/index.ts index 95816ec231..62eada1e03 100644 --- a/plugins/love/src/index.ts +++ b/plugins/love/src/index.ts @@ -51,6 +51,7 @@ export interface ParticipantInfo extends Doc { room: Ref x: number y: number + sessionId: string | null } export interface RoomInfo extends Doc { diff --git a/plugins/workbench-resources/src/connect.ts b/plugins/workbench-resources/src/connect.ts index 62faafa29a..df9a1e25ac 100644 --- a/plugins/workbench-resources/src/connect.ts +++ b/plugins/workbench-resources/src/connect.ts @@ -168,6 +168,9 @@ export async function connect (title: string): Promise { return } try { + if (event === ClientConnectEvent.Connected) { + setMetadata(presentation.metadata.SessionId, data) + } if ((_clientSet && event === ClientConnectEvent.Connected) || event === ClientConnectEvent.Refresh) { void ctx.with('refresh client', {}, async () => { await refreshClient(tokenChanged) diff --git a/server-plugins/love-resources/src/index.ts b/server-plugins/love-resources/src/index.ts index 4a9d1483d5..4f973b29b1 100644 --- a/server-plugins/love-resources/src/index.ts +++ b/server-plugins/love-resources/src/index.ts @@ -84,7 +84,8 @@ async function createUserInfo (acc: Ref, control: TriggerControl): Prom name: person !== undefined ? getName(control.hierarchy, person, control.branding?.lastNameFirst) : account.email, room: room?._id ?? love.ids.Reception, x: 0, - y: 0 + y: 0, + sessionId: null }) const ptx = control.txFactory.createTxApplyIf( core.space.Workspace, @@ -93,7 +94,7 @@ async function createUserInfo (acc: Ref, control: TriggerControl): Prom [ { _class: love.class.ParticipantInfo, - query: { person } + query: { person: personId } } ], [tx],