One more calendar fix (#8110)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2025-02-27 20:59:25 +05:00 committed by GitHub
parent 144b11d564
commit b858dd7696
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 22 deletions

View File

@ -29,6 +29,8 @@ export class CalendarController {
WorkspaceClient | Promise<WorkspaceClient> WorkspaceClient | Promise<WorkspaceClient>
>() >()
private readonly workspacesByEmail = new Map<string, string[]>()
private readonly syncedWorkspaces = new Set<string>() private readonly syncedWorkspaces = new Set<string>()
private readonly tokens: Collection<Token> private readonly tokens: Collection<Token>
@ -95,6 +97,7 @@ export class CalendarController {
await limiter.add(async () => { await limiter.add(async () => {
const workspace = await this.startWorkspace(info.workspaceId, tokens) const workspace = await this.startWorkspace(info.workspaceId, tokens)
await workspace.sync() await workspace.sync()
await workspace.close()
this.syncedWorkspaces.add(info.workspaceId) this.syncedWorkspaces.add(info.workspaceId)
}) })
const newProgress = Math.round((i * 100) / infos.length) const newProgress = Math.round((i * 100) / infos.length)
@ -114,6 +117,9 @@ export class CalendarController {
}, 60000) }, 60000)
console.log('init client', token.workspace, token.userId) console.log('init client', token.workspace, token.userId)
await workspaceClient.createCalendarClient(token, true) await workspaceClient.createCalendarClient(token, true)
const arr = this.workspacesByEmail.get(token.email) ?? []
arr.push(token.workspace)
this.workspacesByEmail.set(token.email, arr)
clearTimeout(timeout) clearTimeout(timeout)
} catch (err) { } catch (err) {
console.error(`Couldn't create client for ${workspace} ${token.userId} ${token.email}`) console.error(`Couldn't create client for ${workspace} ${token.userId} ${token.email}`)
@ -123,30 +129,25 @@ export class CalendarController {
} }
async push (email: string, mode: 'events' | 'calendar', calendarId?: string): Promise<void> { async push (email: string, mode: 'events' | 'calendar', calendarId?: string): Promise<void> {
const tokens = await this.tokens.find({ email, access_token: { $exists: true } }).toArray() const workspaces = this.workspacesByEmail.get(email)?.filter((p) => this.syncedWorkspaces.has(p)) ?? []
const token = generateToken(systemAccountEmail, { name: '' })
const workspaces = [...new Set(tokens.map((p) => p.workspace).filter((p) => this.syncedWorkspaces.has(p)))]
if (workspaces.length === 0) return if (workspaces.length === 0) return
const infos = await getWorkspacesInfo(token, workspaces) for (const workspace of workspaces) {
for (const token of tokens) { const workspaceClient = await this.getWorkspaceClient(workspace)
const info = infos.find((p) => p.workspaceId === token.workspace) let calendar = workspaceClient.getCalendarClient(email)
if (info === undefined) { if (calendar !== undefined) {
continue if (calendar instanceof Promise) {
calendar = await calendar
} }
if (isDeletingMode(info.mode)) { } else {
await this.tokens.deleteOne({ userId: token.userId, workspace: token.workspace }) const token = await this.tokens.findOne({ email, workspace, access_token: { $exists: true } })
continue if (token == null) continue
calendar = await workspaceClient.createCalendarClient(token)
} }
if (!isActiveMode(info.mode)) {
continue
}
const workspace = await this.getWorkspaceClient(token.workspace)
const calendarClient = await workspace.createCalendarClient(token)
if (mode === 'calendar') { if (mode === 'calendar') {
await calendarClient.syncCalendars(email) await calendar.syncCalendars(email)
} }
if (mode === 'events' && calendarId !== undefined) { if (mode === 'events' && calendarId !== undefined) {
await calendarClient.sync(calendarId, email) await calendar.sync(calendarId, email)
} }
} }
} }

View File

@ -41,7 +41,7 @@ export class GoogleClient {
private refreshTimer: NodeJS.Timeout | undefined = undefined private refreshTimer: NodeJS.Timeout | undefined = undefined
readonly rateLimiter = new RateLimiter(100, 30) readonly rateLimiter = new RateLimiter(60 * 1000, 600)
constructor ( constructor (
private readonly user: User, private readonly user: User,

View File

@ -143,7 +143,7 @@ export class WatchClient {
export class WatchController { export class WatchController {
private readonly watches: Collection<Watch> private readonly watches: Collection<Watch>
private readonly tokens: Collection<Token> private readonly tokens: Collection<Token>
readonly rateLimiter = new RateLimiter(1000, 500) readonly rateLimiter = new RateLimiter(60 * 1000, 600)
private timer: NodeJS.Timeout | undefined = undefined private timer: NodeJS.Timeout | undefined = undefined
protected static _instance: WatchController protected static _instance: WatchController

View File

@ -174,7 +174,7 @@ export class WorkspaceClient {
}, 20000) }, 20000)
} }
private getCalendarClient (email: string): CalendarClient | Promise<CalendarClient> | undefined { getCalendarClient (email: string): CalendarClient | Promise<CalendarClient> | undefined {
return this.clients.get(email) return this.clients.get(email)
} }
@ -246,6 +246,7 @@ export class WorkspaceClient {
await client.release() await client.release()
}) })
} }
await limiter.waitProcessing()
} }
// #region Events // #region Events