diff --git a/server-plugins/love-resources/src/index.ts b/server-plugins/love-resources/src/index.ts index 753625d909..9917a80f36 100644 --- a/server-plugins/love-resources/src/index.ts +++ b/server-plugins/love-resources/src/index.ts @@ -73,6 +73,11 @@ async function createUserInfo (acc: Ref, control: TriggerControl): Prom const account = control.modelDb.findAllSync(contact.class.PersonAccount, { _id: acc as Ref })[0] if (account === undefined) return [] const personId = account.person + + // we already have participantInfo for this person + const infos = await control.findAll(love.class.ParticipantInfo, { person: personId }) + if (infos.length > 0) return [] + const person = (await control.findAll(contact.class.Person, { _id: personId }))[0] const room = (await control.findAll(love.class.Office, { person: personId }))[0] const tx = control.txFactory.createTxCreateDoc(love.class.ParticipantInfo, love.space.Rooms, { @@ -98,15 +103,19 @@ async function createUserInfo (acc: Ref, control: TriggerControl): Prom return [] } -async function removeUserInfo (acc: Ref, control: TriggerControl): Promise { +async function removeUserInfo (acc: Ref, control: TriggerControl): Promise { const account = control.modelDb.findAllSync(contact.class.PersonAccount, { _id: acc as Ref })[0] - if (account === undefined) return [] + if (account === undefined) return + + // recheck that user is still offline + const status = (await control.findAll(core.class.UserStatus, { user: acc }))[0] + if (status !== undefined && status.online) return + const person = account.person const infos = await control.findAll(love.class.ParticipantInfo, { person }) for (const info of infos) { await control.apply([control.txFactory.createTxRemoveDoc(info._class, info.space, info._id)], true) } - return [] } export async function OnUserStatus (tx: Tx, control: TriggerControl): Promise { @@ -125,7 +134,10 @@ export async function OnUserStatus (tx: Tx, control: TriggerControl): Promise { + void removeUserInfo(status.user, control) + }, 5000) + return [] } } }