UBERF-11383: Fix gmail push processing (#9127)

Signed-off-by: Artem Savchenko <armisav@gmail.com>
This commit is contained in:
Artyom Savchenko
2025-05-28 15:05:19 +07:00
committed by GitHub
parent 919a8bb62f
commit ffee889d59
5 changed files with 25 additions and 13 deletions
+3 -3
View File
@@ -83,7 +83,7 @@ async function wait (sec: number): Promise<void> {
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,
@@ -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<WorkspaceUuid, GmailClient>
>()
private readonly personIdByEmail = new Map<string, PersonId>()
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<WorkspaceUuid, GmailClient>()
this.clients.set(socialId, userClients)
}
this.personIdByEmail.set(normalizeEmail(email), socialId)
const existingClient = userClients.get(workspace)
if (existingClient != null) {
+2 -2
View File
@@ -163,8 +163,8 @@ export const main = async (): Promise<void> => {
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()
}
}
+1 -4
View File
@@ -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()
}
+4
View File
@@ -127,3 +127,7 @@ export function parseNameFromEmailHeader (headerValue: string | undefined): Emai
lastName: ''
}
}
export function normalizeEmail (email: string): string {
return email.toLowerCase().trim()
}