From 143cf905b5ddfeb07273f0cc7223860e1aa1d46e Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Fri, 28 Feb 2025 12:46:46 +0500 Subject: [PATCH] Fix pushes for new clients in calendar (#8115) Signed-off-by: Denis Bykhov --- .../calendar/pod-calendar/src/calendar.ts | 1 + .../pod-calendar/src/calendarController.ts | 10 +- .../calendar/pod-calendar/src/googleClient.ts | 158 +++++++++--------- .../pod-calendar/src/workspaceClient.ts | 1 + 4 files changed, 91 insertions(+), 79 deletions(-) diff --git a/services/calendar/pod-calendar/src/calendar.ts b/services/calendar/pod-calendar/src/calendar.ts index 6e70604c22..390e783e5d 100644 --- a/services/calendar/pod-calendar/src/calendar.ts +++ b/services/calendar/pod-calendar/src/calendar.ts @@ -241,6 +241,7 @@ export class CalendarClient { } close (): void { + clearTimeout(this.inactiveTimer) this.googleClient.close() for (const watch of this.dummyWatches) { clearTimeout(watch.timer) diff --git a/services/calendar/pod-calendar/src/calendarController.ts b/services/calendar/pod-calendar/src/calendarController.ts index 563a4240a1..2bf4ce7e48 100644 --- a/services/calendar/pod-calendar/src/calendarController.ts +++ b/services/calendar/pod-calendar/src/calendarController.ts @@ -108,6 +108,12 @@ export class CalendarController { } } + pushWorkspaceByEmail (email: string, workspace: string): void { + const arr = this.workspacesByEmail.get(email) ?? [] + arr.push(workspace) + this.workspacesByEmail.set(email, arr) + } + async startWorkspace (workspace: string, tokens: Token[]): Promise { const workspaceClient = await this.getWorkspaceClient(workspace) for (const token of tokens) { @@ -117,9 +123,7 @@ export class CalendarController { }, 60000) console.log('init client', token.workspace, token.userId) await workspaceClient.createCalendarClient(token, true) - const arr = this.workspacesByEmail.get(token.email) ?? [] - arr.push(token.workspace) - this.workspacesByEmail.set(token.email, arr) + this.pushWorkspaceByEmail(token.email, token.workspace) clearTimeout(timeout) } catch (err) { console.error(`Couldn't create client for ${workspace} ${token.userId} ${token.email}`) diff --git a/services/calendar/pod-calendar/src/googleClient.ts b/services/calendar/pod-calendar/src/googleClient.ts index 50738131cc..5f4e2e7207 100644 --- a/services/calendar/pod-calendar/src/googleClient.ts +++ b/services/calendar/pod-calendar/src/googleClient.ts @@ -212,42 +212,46 @@ export class GoogleClient { workspace: this.user.workspace, calendarId: null }) - if (current != null) { - await this.rateLimiter.take(1) - try { - await this.calendar.channels.stop({ requestBody: { id: current.channelId, resourceId: current.resourceId } }) - } catch {} - } - const channelId = generateId() - const me = await this.getMe() - const body = { id: channelId, address: config.WATCH_URL, type: 'webhook', token: `user=${me}&mode=calendar` } - await this.rateLimiter.take(1) - const res = await this.calendar.calendarList.watch({ requestBody: body }) - if (res.data.expiration != null && res.data.resourceId !== null) { + if (current == null || current.expired < Date.now() + 24 * 60 * 60 * 1000) { if (current != null) { - await this.watches.updateOne( - { - userId: this.user.userId, - workspace: this.user.workspace, - calendarId: null - }, - { - $set: { - channelId, - expired: Number.parseInt(res.data.expiration), - resourceId: res.data.resourceId ?? '' + await this.rateLimiter.take(1) + try { + await this.calendar.channels.stop({ + requestBody: { id: current.channelId, resourceId: current.resourceId } + }) + } catch {} + } + const channelId = generateId() + const me = await this.getMe() + const body = { id: channelId, address: config.WATCH_URL, type: 'webhook', token: `user=${me}&mode=calendar` } + await this.rateLimiter.take(1) + const res = await this.calendar.calendarList.watch({ requestBody: body }) + if (res.data.expiration != null && res.data.resourceId !== null) { + if (current != null) { + await this.watches.updateOne( + { + userId: this.user.userId, + workspace: this.user.workspace, + calendarId: null + }, + { + $set: { + channelId, + expired: Number.parseInt(res.data.expiration), + resourceId: res.data.resourceId ?? '' + } } - } - ) - } else { - await this.watches.insertOne({ - calendarId: null, - channelId, - expired: Number.parseInt(res.data.expiration), - resourceId: res.data.resourceId ?? '', - userId: this.user.userId, - workspace: this.user.workspace - }) + ) + } else { + await this.watches.insertOne({ + calendarId: null, + channelId, + expired: Number.parseInt(res.data.expiration), + resourceId: res.data.resourceId ?? '', + userId: this.user.userId, + workspace: this.user.workspace + }) + } } } } catch (err: any) { @@ -262,49 +266,51 @@ export class GoogleClient { workspace: this.user.workspace, calendarId }) - if (current != null) { - await this.rateLimiter.take(1) - try { - await this.calendar.channels.stop({ - requestBody: { id: current.channelId, resourceId: current.resourceId } - }) - } catch {} - } - const channelId = generateId() - const me = await this.getMe() - const body = { - id: channelId, - address: config.WATCH_URL, - type: 'webhook', - token: `user=${me}&mode=events&calendarId=${calendarId}` - } - await this.rateLimiter.take(1) - const res = await this.calendar.events.watch({ calendarId, requestBody: body }) - if (res.data.expiration != null && res.data.resourceId != null) { + if (current == null || current.expired < Date.now() + 24 * 60 * 60 * 1000) { if (current != null) { - await this.watches.updateOne( - { - userId: this.user.userId, - workspace: this.user.workspace, - calendarId - }, - { - $set: { - channelId, - expired: Number.parseInt(res.data.expiration), - resourceId: res.data.resourceId ?? '' + await this.rateLimiter.take(1) + try { + await this.calendar.channels.stop({ + requestBody: { id: current.channelId, resourceId: current.resourceId } + }) + } catch {} + } + const channelId = generateId() + const me = await this.getMe() + const body = { + id: channelId, + address: config.WATCH_URL, + type: 'webhook', + token: `user=${me}&mode=events&calendarId=${calendarId}` + } + await this.rateLimiter.take(1) + const res = await this.calendar.events.watch({ calendarId, requestBody: body }) + if (res.data.expiration != null && res.data.resourceId != null) { + if (current != null) { + await this.watches.updateOne( + { + userId: this.user.userId, + workspace: this.user.workspace, + calendarId + }, + { + $set: { + channelId, + expired: Number.parseInt(res.data.expiration), + resourceId: res.data.resourceId ?? '' + } } - } - ) - } else { - await this.watches.insertOne({ - calendarId, - channelId, - expired: Number.parseInt(res.data.expiration), - resourceId: res.data.resourceId ?? '', - userId: this.user.userId, - workspace: this.user.workspace - }) + ) + } else { + await this.watches.insertOne({ + calendarId, + channelId, + expired: Number.parseInt(res.data.expiration), + resourceId: res.data.resourceId ?? '', + userId: this.user.userId, + workspace: this.user.workspace + }) + } } } return true @@ -312,7 +318,7 @@ export class GoogleClient { if (err?.errors?.[0]?.reason === 'pushNotSupportedForRequestedResource') { return false } else { - console.error('Watch error', err.message) + console.error('Watch error', err) await this.checkError(err) return false } diff --git a/services/calendar/pod-calendar/src/workspaceClient.ts b/services/calendar/pod-calendar/src/workspaceClient.ts index 29c1d26f93..18339ea311 100644 --- a/services/calendar/pod-calendar/src/workspaceClient.ts +++ b/services/calendar/pod-calendar/src/workspaceClient.ts @@ -116,6 +116,7 @@ export class WorkspaceClient { throw new Error('Client already exist') } this.clients.set(email, newClient) + this.serviceController.pushWorkspaceByEmail(email, user.workspace) return newClient }