-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar-client.js
More file actions
328 lines (287 loc) · 12.5 KB
/
calendar-client.js
File metadata and controls
328 lines (287 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const log = require('electron-log');
class CalendarClient {
constructor(credentials, tokens) {
this.credentials = credentials;
this.tokens = tokens;
this.baseUrl = 'https://www.googleapis.com/calendar/v3';
this.calendar = {
events: {
list: this.listEvents.bind(this),
watch: this.watchEvents.bind(this)
},
channels: {
stop: this.stopChannel.bind(this)
},
colors: {
get: this.getColors.bind(this)
}
};
this.auth = {
generateAuthUrl: this.generateAuthUrl.bind(this),
getToken: this.getToken.bind(this)
};
}
async refreshAccessToken() {
const { client_id, client_secret } = this.credentials.installed;
const params = new URLSearchParams({
client_id,
client_secret,
refresh_token: this.tokens.refresh_token,
grant_type: 'refresh_token'
});
const response = await fetch('https://oauth2.googleapis.com/token', {
method: 'POST',
body: params
});
if (!response.ok) {
throw new Error('Failed to refresh token');
}
const data = await response.json();
this.tokens.access_token = data.access_token;
this.tokens.expiry_date = Date.now() + (data.expires_in * 1000);
return this.tokens;
}
async makeRequest(endpoint, options = {}) {
if (Date.now() >= this.tokens.expiry_date) {
log.info('Token expired, refreshing...');
await this.refreshAccessToken();
}
// Use full URL if endpoint starts with https, otherwise prepend baseUrl
const url = endpoint.startsWith('https://') ? endpoint : `${this.baseUrl}${endpoint}`;
log.info('Making API request to:', url);
const response = await fetch(url, {
...options,
headers: {
...options.headers,
'Authorization': `Bearer ${this.tokens.access_token}`,
'Accept': 'application/json',
}
});
if (!response.ok) {
log.error('API request failed:', response.status, response.statusText);
log.error('URL was:', url);
throw new Error(`API request failed: ${response.statusText}`);
}
return response.json();
}
async listEvents(params = {}) {
const searchParams = new URLSearchParams({
calendarId: params.calendarId || 'primary',
timeMin: params.timeMin || new Date().toISOString(),
maxResults: params.maxResults || '50',
singleEvents: params.singleEvents || 'true',
orderBy: params.orderBy || 'startTime',
});
// Add timeMax if provided (for date-specific fetches)
if (params.timeMax) {
searchParams.append('timeMax', params.timeMax);
}
const response = await this.makeRequest(`/calendars/primary/events?${searchParams.toString()}`);
return { data: response }; // Match googleapis response format
}
async watchEvents({ calendarId = 'primary', resource }) {
const response = await this.makeRequest(`/calendars/${calendarId}/events/watch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(resource)
});
return { data: response }; // Match googleapis response format
}
async stopChannel({ requestBody }) {
return this.makeRequest('/channels/stop', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody)
});
}
async getColors() {
const response = await this.makeRequest('/colors');
return { data: response };
}
generateAuthUrl(options) {
const { client_id } = this.credentials.installed;
const params = new URLSearchParams({
client_id,
response_type: 'code',
redirect_uri: options.redirect_uris ? options.redirect_uris[0] : this.credentials.installed.redirect_uris[0],
scope: Array.isArray(options.scope) ? options.scope.join(' ') : options.scope,
access_type: options.access_type,
});
return `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
}
async getToken(code) {
const { client_id, client_secret } = this.credentials.installed;
const params = new URLSearchParams({
client_id,
client_secret,
code,
grant_type: 'authorization_code',
redirect_uri: this.credentials.installed.redirect_uris[0],
});
const response = await fetch('https://oauth2.googleapis.com/token', {
method: 'POST',
body: params
});
if (!response.ok) {
throw new Error('Failed to get token');
}
const tokens = await response.json();
this.tokens = {
...tokens,
expiry_date: Date.now() + (tokens.expires_in * 1000)
};
return { tokens: this.tokens };
}
setCredentials(tokens) {
this.tokens = tokens;
}
// Fetch contacts from multiple sources for comprehensive list
async listConnections(pageToken = '') {
try {
// Try People API first for actual Gmail contacts
log.info('Loading contacts from API...');
const peopleResponse = await this.makeRequest(`https://people.googleapis.com/v1/people/me/connections?personFields=names,emailAddresses&pageSize=1000${pageToken ? '&pageToken=' + pageToken : ''}`);
const emails = new Set();
// Extract from People API connections
if (peopleResponse.connections) {
log.info(`Got ${peopleResponse.connections.length} connections from /me/connections`);
peopleResponse.connections.forEach(person => {
if (person.emailAddresses) {
person.emailAddresses.forEach(email => {
if (email.value && email.value.includes('@')) {
emails.add(email.value);
}
});
}
});
}
// Try to get contacts from other endpoints for more comprehensive results
try {
const otherContactsResponse = await this.makeRequest('https://people.googleapis.com/v1/otherContacts?readMask=emailAddresses&pageSize=1000');
if (otherContactsResponse.otherContacts) {
log.info(`Got ${otherContactsResponse.otherContacts.length} other contacts`);
otherContactsResponse.otherContacts.forEach(contact => {
if (contact.emailAddresses) {
contact.emailAddresses.forEach(email => {
if (email.value && email.value.includes('@')) {
emails.add(email.value);
}
});
}
});
}
} catch (e) {
log.info('Failed to get other contacts:', e.message);
}
// Try directory API for more contacts
try {
const directoryResponse = await this.makeRequest('https://people.googleapis.com/v1/people:searchDirectoryPeople?readMask=emailAddresses&pageSize=1000&query=""');
if (directoryResponse.people) {
log.info(`Got ${directoryResponse.people.length} directory people`);
directoryResponse.people.forEach(person => {
if (person.emailAddresses) {
person.emailAddresses.forEach(email => {
if (email.value && email.value.includes('@')) {
emails.add(email.value);
}
});
}
});
}
} catch (e) {
log.info('Failed to search directory:', e.message);
}
log.info(`Total unique contacts found from People API: ${emails.size}`);
// If we got very few contacts from People API, supplement with calendar events
if (emails.size < 20) {
log.info('Got few contacts from People API, supplementing with calendar events...');
const calendarEmails = await this.getEmailsFromCalendarEvents();
if (calendarEmails.connections) {
calendarEmails.connections.forEach(conn => {
if (conn.emailAddresses) {
conn.emailAddresses.forEach(email => {
if (email.value && email.value.includes('@')) {
emails.add(email.value);
}
});
}
});
}
}
// Convert to People API format
const connections = Array.from(emails).map(email => ({
emailAddresses: [{ value: email }]
}));
log.info(`Total unique contacts found: ${emails.size}`);
log.info('Raw API response:', JSON.stringify(peopleResponse, null, 2));
return { connections };
} catch (error) {
log.error('People API failed, falling back to calendar events');
return await this.getEmailsFromCalendarEvents();
}
}
// Extract emails from calendar attendees + add common suggestions
async getEmailsFromCalendarEvents() {
try {
const response = await this.makeRequest('/calendars/primary/events?maxResults=200&singleEvents=true&orderBy=startTime');
const realEmails = new Set();
// Extract real emails from calendar events FIRST
if (response.items) {
response.items.forEach(event => {
if (event.attendees) {
event.attendees.forEach(attendee => {
if (attendee.email && attendee.email.includes('@') && !attendee.email.includes('calendar.google.com')) {
realEmails.add(attendee.email);
}
});
}
if (event.organizer && event.organizer.email && !event.organizer.email.includes('calendar.google.com')) {
realEmails.add(event.organizer.email);
}
});
}
// Add common email suggestions AFTER real emails (if needed)
const allEmails = Array.from(realEmails);
if (allEmails.length < 10) {
const commonEmails = [
'team@company.com',
'support@company.com',
'hello@startup.com',
'contact@business.com',
'info@organization.org'
];
commonEmails.forEach(email => {
if (!realEmails.has(email)) {
allEmails.push(email);
}
});
}
// Convert to People API format for compatibility
const connections = allEmails.map(email => ({
emailAddresses: [{ value: email }]
}));
log.info(`Extracted ${realEmails.size} real emails from calendar events, total ${connections.length} contacts`);
return { connections };
} catch (error) {
log.error('Calendar fallback also failed:', error);
// Return basic suggestions if everything fails
const fallbackEmails = [
'team@company.com',
'contact@business.com',
'hello@startup.com',
'support@company.com',
'info@organization.org'
];
const connections = fallbackEmails.map(email => ({
emailAddresses: [{ value: email }]
}));
return { connections };
}
}
}
module.exports = CalendarClient;