diff --git a/packages/core/src/memdb.ts b/packages/core/src/memdb.ts index f689dba805..40c4e6591a 100644 --- a/packages/core/src/memdb.ts +++ b/packages/core/src/memdb.ts @@ -32,6 +32,7 @@ export abstract class MemDb extends TxProcessor implements Storage { private readonly objectById = new Map, Doc>() private readonly accountByPersonId = new Map, Account[]>() + private readonly accountByEmail = new Map() constructor (protected readonly hierarchy: Hierarchy) { super() @@ -81,6 +82,10 @@ export abstract class MemDb extends TxProcessor implements Storage { return this.accountByPersonId.get(ref) ?? [] } + getAccountByEmail (email: Account['email']): Account | undefined { + return this.accountByEmail.get(email) + } + findObject(_id: Ref): T | undefined { const doc = this.objectById.get(_id) return doc as T @@ -227,6 +232,7 @@ export abstract class MemDb extends TxProcessor implements Storage { }) if (this.hierarchy.isDerived(doc._class, core.class.Account)) { const account = doc as Account + this.accountByEmail.set(account.email, account) if (account.person !== undefined) { this.accountByPersonId.set(account.person, [...(this.accountByPersonId.get(account.person) ?? []), account]) } @@ -245,6 +251,7 @@ export abstract class MemDb extends TxProcessor implements Storage { }) if (this.hierarchy.isDerived(doc._class, core.class.Account)) { const account = doc as Account + this.accountByEmail.delete(account.email) if (account.person !== undefined) { const acc = this.accountByPersonId.get(account.person) ?? [] this.accountByPersonId.set( diff --git a/server/collaborator/src/platform.ts b/server/collaborator/src/platform.ts index d075ba5b8f..0c60a20998 100644 --- a/server/collaborator/src/platform.ts +++ b/server/collaborator/src/platform.ts @@ -23,7 +23,7 @@ async function connect (token: string): Promise { } async function getTxOperations (client: Client, token: Token, isDerived: boolean = false): Promise { - const account = await client.findOne(core.class.Account, { email: token.email }) + const account = client.getModel().getAccountByEmail(token.email) const accountId = account?._id ?? core.account.System return new TxOperations(client, accountId, isDerived) diff --git a/server/core/src/utils.ts b/server/core/src/utils.ts index fc3d6e5edb..03ed26e7ae 100644 --- a/server/core/src/utils.ts +++ b/server/core/src/utils.ts @@ -140,7 +140,7 @@ export function getUser (modelDb: ModelDb, userEmail: string | undefined, admin? if (userEmail === undefined) { throw new PlatformError(new Status(Severity.ERROR, platform.status.Forbidden, {})) } - const account = modelDb.findAllSync(core.class.Account, { email: userEmail })[0] + const account = modelDb.getAccountByEmail(userEmail) if (account === undefined) { if (userEmail === systemAccountEmail || admin === true) { return { @@ -181,7 +181,7 @@ export class SessionDataImpl implements SessionData { } getAccount (account: Ref): Account | undefined { - return this.modelDb.findAllSync(core.class.Account, { _id: account })[0] + return this.modelDb.findObject(account) } } diff --git a/server/ws/src/client.ts b/server/ws/src/client.ts index 3aae5f0811..f7be721c3d 100644 --- a/server/ws/src/client.ts +++ b/server/ws/src/client.ts @@ -113,12 +113,10 @@ export class ClientSession implements Session { } async getAccount (ctx: ClientSessionCtx): Promise { - const account = this._pipeline.context.modelDb.findAllSync(core.class.Account, { email: this.token.email }) - if (account.length === 0 && this.token.extra?.admin === 'true') { - const systemAccount = this._pipeline.context.modelDb.findAllSync(core.class.Account, { - _id: this.token.email as Ref - }) - if (systemAccount.length === 0) { + const account = this._pipeline.context.modelDb.getAccountByEmail(this.token.email) + if (account === undefined && this.token.extra?.admin === 'true') { + const systemAccount = this._pipeline.context.modelDb.findObject(this.token.email as Ref) + if (systemAccount === undefined) { // Generate account for admin user const factory = new TxFactory(core.account.System) const email = `system:${this.token.email}` @@ -152,11 +150,11 @@ export class ClientSession implements Session { await ctx.sendResponse(acc) return } else { - await ctx.sendResponse(systemAccount[0]) + await ctx.sendResponse(systemAccount) return } } - await ctx.sendResponse(account[0]) + await ctx.sendResponse(account) } findAllRaw( diff --git a/server/ws/src/server.ts b/server/ws/src/server.ts index 3f26cffe14..6a9567ac00 100644 --- a/server/ws/src/server.ts +++ b/server/ws/src/server.ts @@ -629,15 +629,7 @@ class TSessionManager implements SessionManager { workspaceId: WorkspaceId ): Promise { try { - const user = ( - await session.pipeline().context.modelDb.findAll( - core.class.Account, - { - email: session.getUser() - }, - { limit: 1 } - ) - )[0] + const user = session.pipeline().context.modelDb.getAccountByEmail(session.getUser()) if (user === undefined) return const clientCtx: ClientSessionCtx = { diff --git a/services/calendar/pod-calendar/src/workspaceClient.ts b/services/calendar/pod-calendar/src/workspaceClient.ts index 90accd098f..5a675a0a3e 100644 --- a/services/calendar/pod-calendar/src/workspaceClient.ts +++ b/services/calendar/pod-calendar/src/workspaceClient.ts @@ -16,6 +16,7 @@ import calendar, { Event, ExternalCalendar } from '@hcengineering/calendar' import contact, { Channel, Contact, type Employee, type PersonAccount } from '@hcengineering/contact' import core, { + TxOperations, TxProcessor, toIdMap, type Account, @@ -25,17 +26,16 @@ import core, { type Tx, type TxCreateDoc, type TxRemoveDoc, - type TxUpdateDoc, - TxOperations + type TxUpdateDoc } from '@hcengineering/core' +import { generateToken } from '@hcengineering/server-token' import setting, { Integration } from '@hcengineering/setting' import { Collection, type Db } from 'mongodb' import { CalendarClient } from './calendar' +import { CalendarController } from './calendarController' import { getClient } from './client' import config from './config' import { SyncHistory, type ProjectCredentials, type User } from './types' -import { CalendarController } from './calendarController' -import { generateToken } from '@hcengineering/server-token' export class WorkspaceClient { private readonly txHandlers: ((...tx: Tx[]) => Promise)[] = [] @@ -109,9 +109,7 @@ export class WorkspaceClient { } async getUserId (email: string): Promise> { - const user = await this.client.findOne(core.class.Account, { - email - }) + const user = this.client.getModel().getAccountByEmail(email) if (user === undefined) { throw new Error('User not found') } diff --git a/services/github/pod-github/src/platform.ts b/services/github/pod-github/src/platform.ts index 3ddc0a3908..626f1237fd 100644 --- a/services/github/pod-github/src/platform.ts +++ b/services/github/pod-github/src/platform.ts @@ -408,7 +408,7 @@ export class PlatformWorker { } // We need to re-bind previously created github:login account to a proper person. - const account = (await client.findOne(core.class.Account, { _id: payload.accountId })) as PersonAccount + const account = client.getModel().getObject(payload.accountId) as PersonAccount const person = (await client.findOne(contact.class.Person, { _id: account.person })) as Person if (person !== undefined) { if (!revoke) { @@ -424,9 +424,7 @@ export class PlatformWorker { }) } - const githubAccount = (await client.findOne(core.class.Account, { - email: 'github:' + update.login - })) as PersonAccount + const githubAccount = client.getModel().getAccountByEmail('github:' + update.login) as PersonAccount if (githubAccount !== undefined && githubAccount.person !== account.person) { const dummyPerson = githubAccount.person // To add activity entry to dummy person. diff --git a/services/gmail/pod-gmail/src/workspaceClient.ts b/services/gmail/pod-gmail/src/workspaceClient.ts index a3088ac192..d18c7c78c5 100644 --- a/services/gmail/pod-gmail/src/workspaceClient.ts +++ b/services/gmail/pod-gmail/src/workspaceClient.ts @@ -19,14 +19,14 @@ import core, { type AttachedDoc, type Client, type Doc, + MeasureContext, type Ref, type Tx, type TxCollectionCUD, type TxCreateDoc, TxProcessor, type TxRemoveDoc, - type TxUpdateDoc, - MeasureContext + type TxUpdateDoc } from '@hcengineering/core' import gmailP, { type NewMessage } from '@hcengineering/gmail' import type { StorageAdapter } from '@hcengineering/server-core' @@ -92,9 +92,7 @@ export class WorkspaceClient { } async getUserId (email: string): Promise> { - const user = await this.client.findOne(core.class.Account, { - email - }) + const user = this.client.getModel().getAccountByEmail(email) if (user === undefined) { throw new Error('User not found') } diff --git a/services/telegram/pod-telegram/src/workspace.ts b/services/telegram/pod-telegram/src/workspace.ts index 7833eb4766..21b6e31e91 100644 --- a/services/telegram/pod-telegram/src/workspace.ts +++ b/services/telegram/pod-telegram/src/workspace.ts @@ -179,7 +179,7 @@ export class WorkspaceWorker { // #region Users async addUser ({ email, phone, conn }: TgUser): Promise { - const user = (await this.client.findAll(core.class.Account, { email }, { limit: 1 }))[0] + const user = this.client.getModel().getAccountByEmail(email) if (user === undefined) { throw Error(`Unable to find user by email: ${email}`)