diff --git a/services/gmail/pod-gmail/src/gmail.ts b/services/gmail/pod-gmail/src/gmail.ts index 628bcdda07..a768a1e1e6 100644 --- a/services/gmail/pod-gmail/src/gmail.ts +++ b/services/gmail/pod-gmail/src/gmail.ts @@ -83,7 +83,7 @@ async function wait (sec: number): Promise { export class GmailClient { private readonly account: AccountUuid - private email: string | undefined = undefined + private email: string private readonly tokenStorage: TokenStorage private readonly client: TxOperations private watchTimer: NodeJS.Timeout | undefined = undefined @@ -374,7 +374,7 @@ export class GmailClient { const profile = await this.gmail.getProfile({ userId: 'me' }) - this.email = profile.data.emailAddress ?? undefined + this.email = profile.data.emailAddress ?? this.email if (this.email !== undefined) return this.email await wait(5) } @@ -394,7 +394,7 @@ export class GmailClient { try { this.ctx.info('Register client', { socialId: this.socialId._id, email: this.email }) const controller = GmailController.getGmailController() - controller.addClient(this.socialId._id, this.user.workspace, this) + controller.addClient(this.socialId._id, this.user.workspace, this.email, this) } catch (err) { this.ctx.error('Add client error', { workspaceUuid: this.user.workspace, diff --git a/services/gmail/pod-gmail/src/gmailController.ts b/services/gmail/pod-gmail/src/gmailController.ts index 40a03e8926..09404e31b0 100644 --- a/services/gmail/pod-gmail/src/gmailController.ts +++ b/services/gmail/pod-gmail/src/gmailController.ts @@ -22,6 +22,7 @@ import { type PersonId } from '@hcengineering/core' import type { StorageAdapter } from '@hcengineering/server-core' +import { normalizeEmail } from '@hcengineering/mail-common' import { decode64 } from './base64' import config from './config' @@ -44,6 +45,8 @@ export class GmailController { Map >() + private readonly personIdByEmail = new Map() + private readonly initLimitter = new RateLimiter(config.InitLimit) private readonly authProvider @@ -162,22 +165,30 @@ export class GmailController { push (message: string): void { const data = JSON.parse(decode64(message)) const email = data.emailAddress - const clients = this.clients.get(email) - if (clients === undefined) { - this.ctx.info('No clients found', { email }) + const socialId = this.personIdByEmail.get(normalizeEmail(email)) + if (socialId === undefined) { + this.ctx.warn('No socialId found for email', { email }) return } + + const clients = this.clients.get(socialId) + if (clients === undefined) { + this.ctx.info('No clients found', { email, socialId }) + return + } + this.ctx.info('Processing push', { clients: clients.size, email }) for (const client of clients.values()) { void client.sync() } } - addClient (socialId: PersonId, workspace: WorkspaceUuid, client: GmailClient): void { + addClient (socialId: PersonId, workspace: WorkspaceUuid, email: string, client: GmailClient): void { let userClients = this.clients.get(socialId) if (userClients === undefined) { userClients = new Map() this.clients.set(socialId, userClients) } + this.personIdByEmail.set(normalizeEmail(email), socialId) const existingClient = userClients.get(workspace) if (existingClient != null) { diff --git a/services/gmail/pod-gmail/src/main.ts b/services/gmail/pod-gmail/src/main.ts index 82a2e51745..cbf3db624e 100644 --- a/services/gmail/pod-gmail/src/main.ts +++ b/services/gmail/pod-gmail/src/main.ts @@ -163,8 +163,8 @@ export const main = async (): Promise => { gmailController.push(data) res.send() - } catch (err) { - ctx.error('Push request failed', { message: JSON.stringify(err) }) + } catch (err: any) { + ctx.error('Push request failed', { message: err.message }) res.status(500).send() } } diff --git a/services/mail/mail-common/src/channel.ts b/services/mail/mail-common/src/channel.ts index b0405c46b5..5e40907338 100644 --- a/services/mail/mail-common/src/channel.ts +++ b/services/mail/mail-common/src/channel.ts @@ -18,6 +18,7 @@ import chat from '@hcengineering/chat' import mail from '@hcengineering/mail' import { PersonSpace } from '@hcengineering/contact' import { SyncMutex } from './mutex' +import { normalizeEmail } from './utils' const createMutex = new SyncMutex() @@ -191,7 +192,3 @@ export const ChannelCacheFactory = { return ChannelCacheFactory.instances.size } } - -function normalizeEmail (email: string): string { - return email.toLowerCase().trim() -} diff --git a/services/mail/mail-common/src/utils.ts b/services/mail/mail-common/src/utils.ts index d2b9071326..d707da101c 100644 --- a/services/mail/mail-common/src/utils.ts +++ b/services/mail/mail-common/src/utils.ts @@ -127,3 +127,7 @@ export function parseNameFromEmailHeader (headerValue: string | undefined): Emai lastName: '' } } + +export function normalizeEmail (email: string): string { + return email.toLowerCase().trim() +}