Skip to content

Commit 3fe325b

Browse files
authored
Move calendar users from kvs to local (#9631)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent d32beb8 commit 3fe325b

File tree

9 files changed

+46
-65
lines changed

9 files changed

+46
-65
lines changed

services/calendar/pod-calendar/src/auth.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ import { Credentials, OAuth2Client } from 'google-auth-library'
3131
import { calendar_v3, google } from 'googleapis'
3232
import { encode64 } from './base64'
3333
import { getClient } from './client'
34-
import { addUserByEmail, removeUserByEmail, setSyncHistory } from './kvsUtils'
34+
import { setSyncHistory } from './kvsUtils'
3535
import { lock } from './mutex'
3636
import { IncomingSyncManager } from './sync'
3737
import { CALENDAR_INTEGRATION, GoogleEmail, SCOPES, State, Token, User } from './types'
38-
import { getGoogleClient, removeIntegrationSecret } from './utils'
38+
import { addUserByEmail, getGoogleClient, removeIntegrationSecret, removeUserByEmail } from './utils'
3939
import { WatchController } from './watch'
4040

4141
interface AuthResult {
@@ -122,7 +122,7 @@ export class AuthController {
122122
if (integration !== undefined) {
123123
await this.client.remove(integration)
124124
}
125-
await removeUserByEmail(this.user, value)
125+
removeUserByEmail(this.user, value)
126126
const data = {
127127
kind: CALENDAR_INTEGRATION,
128128
workspaceUuid: this.user.workspace,
@@ -283,7 +283,7 @@ export class AuthController {
283283
} else {
284284
await this.accountClient.addIntegrationSecret(data)
285285
}
286-
await addUserByEmail(_token, email)
286+
addUserByEmail(_token, email)
287287
} catch (err) {
288288
this.ctx.error('update token error', { workspace: this.user.workspace, user: this.user.userId, err })
289289
}

services/calendar/pod-calendar/src/calendar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { areEqualMarkups, htmlToMarkup, isEmptyMarkup, jsonToHTML, markupToJSON
2020
import { deepEqual } from 'fast-equals'
2121
import { OAuth2Client } from 'google-auth-library'
2222
import { calendar_v3 } from 'googleapis'
23-
import { removeUserByEmail } from './kvsUtils'
2423
import { getRateLimitter, RateLimiter } from './rateLimiter'
2524
import { CALENDAR_INTEGRATION, type Token } from './types'
2625
import {
@@ -32,6 +31,7 @@ import {
3231
parseEventDate,
3332
parseRecurrenceStrings,
3433
removeIntegrationSecret,
34+
removeUserByEmail,
3535
setCredentials
3636
} from './utils'
3737
import type { WorkspaceClient } from './workspaceClient'
@@ -66,7 +66,7 @@ export class CalendarClient {
6666
const calendarClient = new CalendarClient(ctx, accountClient, user, client, workspace)
6767
const authSucces = await setCredentials(calendarClient.oAuth2Client, user)
6868
if (!authSucces) {
69-
await removeUserByEmail(user, user.email)
69+
removeUserByEmail(user, user.email)
7070
await removeIntegrationSecret(ctx, calendarClient.accountClient, {
7171
socialId: user.userId,
7272
kind: CALENDAR_INTEGRATION,

services/calendar/pod-calendar/src/calendarController.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
import config from './config'
2626
import { getIntegrations } from './integrations'
2727
import { WorkspaceClient } from './workspaceClient'
28-
import { cleanUserByEmail } from './kvsUtils'
2928

3029
interface WorkspaceStateInfo {
3130
shouldStart: boolean
@@ -72,7 +71,6 @@ export class CalendarController {
7271
}
7372

7473
private async runAll (groups: Map<WorkspaceUuid, Integration[]>): Promise<void> {
75-
await cleanUserByEmail()
7674
const ids = [...groups.keys()]
7775
if (ids.length === 0) return
7876
const limiter = new RateLimiter(config.InitLimit)

services/calendar/pod-calendar/src/kvsUtils.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { WorkspaceUuid } from '@hcengineering/core'
22
import { KeyValueClient, getClient as getKeyValueClient } from '@hcengineering/kvs-client'
33
import config from './config'
4-
import { CALENDAR_INTEGRATION, GoogleEmail, Token, User } from './types'
4+
import { CALENDAR_INTEGRATION, GoogleEmail, User } from './types'
55
import { getServiceToken } from './utils'
66

77
let keyValueClient: KeyValueClient | undefined
@@ -59,42 +59,3 @@ export async function setEventHistory (
5959
const client = getKvsClient()
6060
await client.setValue(eventHistoryKey(user, email, calendarId), historyId)
6161
}
62-
63-
export async function getUserByEmail (email: GoogleEmail): Promise<Token[]> {
64-
const client = getKvsClient()
65-
const key = `${CALENDAR_INTEGRATION}:users:${email}`
66-
return (await client.getValue<Token[]>(key)) ?? []
67-
}
68-
69-
export async function addUserByEmail (user: Token, email: GoogleEmail): Promise<void> {
70-
const client = getKvsClient()
71-
const key = `${CALENDAR_INTEGRATION}:users:${email}`
72-
const curr = (await client.getValue<Token[]>(key)) ?? []
73-
const exists = curr.find((p) => p.userId === user.userId && p.workspace === user.workspace)
74-
if (exists !== undefined) {
75-
return
76-
}
77-
curr.push(user)
78-
await client.setValue<Token[]>(key, curr)
79-
}
80-
81-
export async function removeUserByEmail (user: User, email: GoogleEmail): Promise<void> {
82-
const client = getKvsClient()
83-
const key = `${CALENDAR_INTEGRATION}:users:${email}`
84-
const curr = (await client.getValue<User[]>(key)) ?? []
85-
const newCurr = curr.filter((p) => p.userId !== user.userId || p.workspace !== user.workspace)
86-
if (newCurr.length === 0) {
87-
await client.deleteKey(key)
88-
} else {
89-
await client.setValue<User[]>(key, newCurr)
90-
}
91-
}
92-
93-
export async function cleanUserByEmail (): Promise<void> {
94-
const client = getKvsClient()
95-
const keys = await client.listKeys(`${CALENDAR_INTEGRATION}:users:`)
96-
if (keys?.keys == null) return
97-
for (const key of keys.keys) {
98-
await client.deleteKey(key)
99-
}
100-
}

services/calendar/pod-calendar/src/outcomingClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { deepEqual } from 'fast-equals'
2222
import { OAuth2Client } from 'google-auth-library'
2323
import { calendar_v3 } from 'googleapis'
2424
import { getClient } from './client'
25-
import { removeUserByEmail, setSyncHistory } from './kvsUtils'
25+
import { setSyncHistory } from './kvsUtils'
2626
import { lock, synced } from './mutex'
2727
import { getRateLimitter, RateLimiter } from './rateLimiter'
2828
import { CALENDAR_INTEGRATION, Token } from './types'
@@ -35,6 +35,7 @@ import {
3535
parseEventDate,
3636
parseRecurrenceStrings,
3737
removeIntegrationSecret,
38+
removeUserByEmail,
3839
setCredentials
3940
} from './utils'
4041

@@ -417,7 +418,7 @@ export class OutcomingClient {
417418
workspace: user.workspace,
418419
email: user.email
419420
})
420-
await removeUserByEmail(user, user.email)
421+
removeUserByEmail(user, user.email)
421422
await removeIntegrationSecret(ctx, accountClient, {
422423
socialId: user.userId,
423424
kind: CALENDAR_INTEGRATION,

services/calendar/pod-calendar/src/pushHandler.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
import { AccountClient } from '@hcengineering/account-client'
1717
import { MeasureContext, TxOperations } from '@hcengineering/core'
1818
import { getClient } from './client'
19-
import { getUserByEmail, removeUserByEmail } from './kvsUtils'
2019
import { IncomingSyncManager } from './sync'
2120
import { CALENDAR_INTEGRATION, GoogleEmail, Token } from './types'
22-
import { getGoogleClient, removeIntegrationSecret, setCredentials } from './utils'
21+
import { getGoogleClient, getUserByEmail, removeIntegrationSecret, removeUserByEmail, setCredentials } from './utils'
2322

2423
export class PushHandler {
2524
constructor (
@@ -37,7 +36,7 @@ export class PushHandler {
3736
const res = getGoogleClient()
3837
const authSuccess = await setCredentials(res.auth, token)
3938
if (!authSuccess) {
40-
await removeUserByEmail(token, token.email)
39+
removeUserByEmail(token, token.email)
4140
await removeIntegrationSecret(this.ctx, this.accountClient, {
4241
kind: CALENDAR_INTEGRATION,
4342
workspaceUuid: token.workspace,
@@ -63,7 +62,7 @@ export class PushHandler {
6362
}
6463

6564
async push (email: GoogleEmail, mode: 'events' | 'calendar', calendarId?: string): Promise<void> {
66-
const tokens = await getUserByEmail(email)
65+
const tokens = getUserByEmail(email)
6766
this.ctx.info('push', { email, mode, calendarId, tokens: tokens.length })
6867
for (const token of tokens) {
6968
await this.sync(token, mode === 'events' ? calendarId ?? null : null)

services/calendar/pod-calendar/src/sync.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ import { htmlToMarkup } from '@hcengineering/text'
4343
import { deepEqual } from 'fast-equals'
4444
import { calendar_v3 } from 'googleapis'
4545
import { getClient } from './client'
46-
import {
47-
getCalendarsSyncHistory,
48-
getEventHistory,
49-
removeUserByEmail,
50-
setCalendarsSyncHistory,
51-
setEventHistory
52-
} from './kvsUtils'
46+
import { getCalendarsSyncHistory, getEventHistory, setCalendarsSyncHistory, setEventHistory } from './kvsUtils'
5347
import { lock } from './mutex'
5448
import { getRateLimitter, RateLimiter } from './rateLimiter'
5549
import { CALENDAR_INTEGRATION, GoogleEmail, Token, User } from './types'
@@ -58,6 +52,7 @@ import {
5852
parseEventDate,
5953
parseRecurrenceStrings,
6054
removeIntegrationSecret,
55+
removeUserByEmail,
6156
setCredentials
6257
} from './utils'
6358
import { WatchController } from './watch'
@@ -104,7 +99,7 @@ export class IncomingSyncManager {
10499
try {
105100
const authSucces = await setCredentials(google.auth, user)
106101
if (!authSucces) {
107-
await removeUserByEmail(user, user.email)
102+
removeUserByEmail(user, user.email)
108103
await removeIntegrationSecret(ctx, accountClient, {
109104
socialId: user.userId,
110105
kind: CALENDAR_INTEGRATION,

services/calendar/pod-calendar/src/utils.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { generateToken } from '@hcengineering/server-token'
2929
import { Credentials, OAuth2Client } from 'google-auth-library'
3030
import { calendar_v3, google } from 'googleapis'
3131
import config from './config'
32-
import { CALENDAR_INTEGRATION, ReccuringData, State, type Token, type User } from './types'
32+
import { CALENDAR_INTEGRATION, GoogleEmail, ReccuringData, State, type Token, type User } from './types'
3333

3434
export class DeferredPromise<T = any> {
3535
public readonly promise: Promise<T>
@@ -363,3 +363,29 @@ export function parseEventDate (date: calendar_v3.Schema$EventDateTime | undefin
363363
}
364364
return 0
365365
}
366+
367+
const users = new Map<GoogleEmail, Token[]>()
368+
369+
export function getUserByEmail (email: GoogleEmail): Token[] {
370+
return users.get(email) ?? []
371+
}
372+
373+
export function addUserByEmail (user: Token, email: GoogleEmail): void {
374+
const curr = getUserByEmail(email)
375+
const exists = curr.find((p) => p.userId === user.userId && p.workspace === user.workspace)
376+
if (exists !== undefined) {
377+
return
378+
}
379+
curr.push(user)
380+
users.set(email, curr)
381+
}
382+
383+
export function removeUserByEmail (user: User, email: GoogleEmail): void {
384+
const curr = getUserByEmail(email)
385+
const newCurr = curr.filter((p) => p.userId !== user.userId || p.workspace !== user.workspace)
386+
if (newCurr.length === 0) {
387+
users.delete(email)
388+
} else {
389+
users.set(email, newCurr)
390+
}
391+
}

services/calendar/pod-calendar/src/workspaceClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ import core, {
2727
import { CalendarClient } from './calendar'
2828
import { getClient } from './client'
2929
import config from './config'
30-
import { addUserByEmail, getSyncHistory, setSyncHistory } from './kvsUtils'
30+
import { getSyncHistory, setSyncHistory } from './kvsUtils'
3131
import { synced } from './mutex'
3232
import { IncomingSyncManager } from './sync'
3333
import { getWorkspaceTokens } from './tokens'
3434
import { GoogleEmail, Token } from './types'
35+
import { addUserByEmail } from './utils'
3536

3637
export class WorkspaceClient {
3738
private readonly clients = new Map<GoogleEmail, CalendarClient>()
@@ -89,7 +90,7 @@ export class WorkspaceClient {
8990
for (const token of tokens) {
9091
if (token.workspaceUuid === null) continue
9192
const parsedToken = JSON.parse(token.secret)
92-
await addUserByEmail(parsedToken, token.key as GoogleEmail)
93+
addUserByEmail(parsedToken, token.key as GoogleEmail)
9394
await this.createCalendarClient(parsedToken)
9495
}
9596
const limiter = new RateLimiter(config.InitLimit)

0 commit comments

Comments
 (0)