-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathGraphNotificationClient.ts
More file actions
409 lines (363 loc) · 16.1 KB
/
GraphNotificationClient.ts
File metadata and controls
409 lines (363 loc) · 16.1 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { BetaGraph, IGraph, Providers, createFromProvider, error, log } from '@microsoft/mgt-element';
import { HubConnection, HubConnectionBuilder, IHttpConnectionOptions, LogLevel } from '@microsoft/signalr';
import { ThreadEventEmitter } from './ThreadEventEmitter';
import type {
Entity,
Subscription,
ChatMessage,
Chat,
AadUserConversationMember
} from '@microsoft/microsoft-graph-types';
import { GraphConfig } from './GraphConfig';
import { SubscriptionsCache } from './Caching/SubscriptionCache';
import { Timer } from '../utils/Timer';
import { addPremiumApiSegment } from '../utils/addPremiumApiSegment';
import { getOrGenerateGroupId } from './getOrGenerateGroupId';
export const appSettings = {
defaultSubscriptionLifetimeInMinutes: 10,
renewalThreshold: 75, // The number of seconds before subscription expires it will be renewed
renewalTimerInterval: 20, // The number of seconds between executions of the renewal timer
removalThreshold: 1 * 60, // number of seconds after the last update of a subscription to consider in inactive
removalTimerInterval: 1 * 60, // the number of seconds between executions of the timer to remove inactive subscriptions
useCanary: GraphConfig.useCanary
};
type ChangeTypes = 'created' | 'updated' | 'deleted';
interface Notification<T extends Entity> {
subscriptionId: string;
changeType: ChangeTypes;
resource: string;
resourceData: T & {
id: string;
'@odata.type': string;
'@odata.id': string;
};
EncryptedContent: string;
}
type ReceivedNotification = Notification<Chat> | Notification<ChatMessage> | Notification<AadUserConversationMember>;
const isMessageNotification = (o: Notification<Entity>): o is Notification<ChatMessage> =>
o.resource.includes('/messages(');
const isMembershipNotification = (o: Notification<Entity>): o is Notification<AadUserConversationMember> =>
o.resource.includes('/members');
export class GraphNotificationClient {
private connection?: HubConnection = undefined;
private renewalTimeout?: string;
private cleanupTimeout?: string;
private renewalCount = 0;
private chatId = '';
private sessionId = '';
private readonly subscriptionCache: SubscriptionsCache = new SubscriptionsCache();
private readonly timer = new Timer();
private get graph() {
return this._graph;
}
private get beta() {
return BetaGraph.fromGraph(this._graph);
}
private get subscriptionGraph() {
return GraphConfig.useCanary
? createFromProvider(Providers.globalProvider, GraphConfig.canarySubscriptionVersion, 'mgt-chat')
: this.beta;
}
/**
*
*/
constructor(
private readonly emitter: ThreadEventEmitter,
private readonly _graph: IGraph
) {
// start the cleanup timer when we create the notification client.
this.startCleanupTimer();
}
/**
* Removes any active timers that may exist to prevent memory leaks and perf issues.
* Call this method when the component that depends an instance of this class is being removed from the DOM
*/
public tearDown() {
log('cleaning up graph notification resources');
if (this.cleanupTimeout) this.timer.clearTimeout(this.cleanupTimeout);
if (this.renewalTimeout) this.timer.clearTimeout(this.renewalTimeout);
this.timer.close();
}
private readonly getToken = async () => {
const token = await Providers.globalProvider.getAccessToken();
if (!token) throw new Error('Could not retrieve token for user');
return token;
};
// TODO: understand if this is needed under the native model
private readonly onReconnect = (connectionId: string | undefined) => {
log(`Reconnected. ConnectionId: ${connectionId || 'undefined'}`);
// void this.renewChatSubscriptions();
};
private readonly receiveNotificationMessage = (message: string) => {
if (typeof message !== 'string') throw new Error('Expected string from receivenotificationmessageasync');
const notification: ReceivedNotification = JSON.parse(message) as ReceivedNotification;
log('received notification message', notification);
const emitter: ThreadEventEmitter | undefined = this.emitter;
if (!notification.resourceData) throw new Error('Message did not contain resourceData');
if (isMessageNotification(notification)) {
this.processMessageNotification(notification, emitter);
} else if (isMembershipNotification(notification)) {
this.processMembershipNotification(notification, emitter);
} else {
this.processChatPropertiesNotification(notification, emitter);
}
// Need to return a status code string of 200 so that graph knows the message was received and doesn't re-send the notification
const ackMessage: unknown = { StatusCode: '200' };
return GraphConfig.ackAsString ? JSON.stringify(ackMessage) : ackMessage;
};
private processMessageNotification(notification: Notification<ChatMessage>, emitter: ThreadEventEmitter | undefined) {
const message = notification.resourceData;
switch (notification.changeType) {
case 'created':
emitter?.chatMessageReceived(message);
return;
case 'updated':
emitter?.chatMessageEdited(message);
return;
case 'deleted':
emitter?.chatMessageDeleted(message);
return;
default:
throw new Error('Unknown change type');
}
}
private processMembershipNotification(
notification: Notification<AadUserConversationMember>,
emitter: ThreadEventEmitter | undefined
) {
const member = notification.resourceData;
switch (notification.changeType) {
case 'created':
emitter?.participantAdded(member);
return;
case 'deleted':
emitter?.participantRemoved(member);
return;
default:
throw new Error('Unknown change type');
}
}
private processChatPropertiesNotification(notification: Notification<Chat>, emitter: ThreadEventEmitter | undefined) {
const chat = notification.resourceData;
switch (notification.changeType) {
case 'updated':
emitter?.chatThreadPropertiesUpdated(chat);
return;
case 'deleted':
emitter?.chatThreadDeleted(chat);
return;
default:
throw new Error('Unknown change type');
}
}
private readonly cacheSubscription = async (subscriptionRecord: Subscription): Promise<void> => {
log(subscriptionRecord);
await this.subscriptionCache.cacheSubscription(this.chatId, subscriptionRecord);
// only start timer once. undefined for renewalInterval is semaphore it has stopped.
if (this.renewalTimeout === undefined) this.startRenewalTimer();
};
private async subscribeToResource(resourcePath: string, changeTypes: ChangeTypes[]) {
// build subscription request
const expirationDateTime = new Date(
new Date().getTime() + appSettings.defaultSubscriptionLifetimeInMinutes * 60 * 1000
).toISOString();
const subscriptionDefinition: Subscription = {
changeType: changeTypes.join(','),
notificationUrl: `${GraphConfig.webSocketsPrefix}?groupId=${getOrGenerateGroupId(this.chatId)}`,
resource: resourcePath,
expirationDateTime,
includeResourceData: true,
clientState: 'wsssecret'
};
log('subscribing to changes for ' + resourcePath);
const subscriptionEndpoint = GraphConfig.subscriptionEndpoint;
// send subscription POST to Graph
const subscription: Subscription = (await this.subscriptionGraph
.api(subscriptionEndpoint)
.post(subscriptionDefinition)) as Subscription;
if (!subscription?.notificationUrl) throw new Error('Subscription not created');
log(subscription);
const awaits: Promise<void>[] = [];
// Cache the subscription in storage for re-hydration on page refreshes
awaits.push(this.cacheSubscription(subscription));
// create a connection to the web socket if one does not exist
if (!this.connection) awaits.push(this.createSignalRConnection(subscription.notificationUrl));
log('Invoked CreateSubscription');
return Promise.all(awaits);
}
private readonly startRenewalTimer = () => {
if (this.renewalTimeout) this.timer.clearTimeout(this.renewalTimeout);
this.renewalTimeout = this.timer.setTimeout(this.syncRenewalTimerWrapper, appSettings.renewalTimerInterval * 1000);
log(`Start renewal timer . Id: ${this.renewalTimeout}`);
};
private readonly syncRenewalTimerWrapper = () => void this.renewalTimer();
private readonly renewalTimer = async () => {
log(`running subscription renewal timer for chatId: ${this.chatId} sessionId: ${this.sessionId}`);
const subscriptions = (await this.subscriptionCache.loadSubscriptions(this.chatId))?.subscriptions || [];
if (subscriptions.length === 0) {
log(`No subscriptions found in session state. Stop renewal timer ${this.renewalTimeout}.`);
if (this.renewalTimeout) this.timer.clearTimeout(this.renewalTimeout);
return;
}
for (const subscription of subscriptions) {
if (!subscription.expirationDateTime) continue;
const expirationTime = new Date(subscription.expirationDateTime);
const now = new Date();
const diff = Math.round((expirationTime.getTime() - now.getTime()) / 1000);
if (diff <= appSettings.renewalThreshold) {
this.renewalCount++;
log(`Renewing Graph subscription. RenewalCount: ${this.renewalCount}`);
// stop interval to prevent new invokes until refresh is ready.
if (this.renewalTimeout) this.timer.clearTimeout(this.renewalTimeout);
this.renewalTimeout = undefined;
await this.renewChatSubscriptions();
// There is one subscription that need expiration, all subscriptions will be renewed
break;
}
}
this.renewalTimeout = this.timer.setTimeout(this.syncRenewalTimerWrapper, appSettings.renewalTimerInterval * 1000);
};
public renewChatSubscriptions = async () => {
const expirationTime = new Date(
new Date().getTime() + appSettings.defaultSubscriptionLifetimeInMinutes * 60 * 1000
);
const subscriptionCache = await this.subscriptionCache.loadSubscriptions(this.chatId);
const awaits: Promise<unknown>[] = [];
for (const subscription of subscriptionCache?.subscriptions || []) {
if (!subscription.id) continue;
// the renewSubscription method caches the updated subscription to track the new expiration time
awaits.push(this.renewSubscription(subscription.id, expirationTime.toISOString()));
log(`Invoked RenewSubscription ${subscription.id}`);
}
await Promise.all(awaits);
if (!this.renewalTimeout) {
this.renewalTimeout = this.timer.setTimeout(
this.syncRenewalTimerWrapper,
appSettings.renewalTimerInterval * 1000
);
}
};
public renewSubscription = async (subscriptionId: string, expirationDateTime: string): Promise<void> => {
// PATCH /subscriptions/{id}
const renewedSubscription = (await this.graph.api(`${GraphConfig.subscriptionEndpoint}/${subscriptionId}`).patch({
expirationDateTime
})) as Subscription;
return this.cacheSubscription(renewedSubscription);
};
public async createSignalRConnection(notificationUrl: string) {
const connectionOptions: IHttpConnectionOptions = {
accessTokenFactory: this.getToken,
withCredentials: false
};
const connection = new HubConnectionBuilder()
.withUrl(GraphConfig.adjustNotificationUrl(notificationUrl, this.sessionId), connectionOptions)
.withAutomaticReconnect()
.configureLogging(LogLevel.Information)
.build();
connection.onreconnected(this.onReconnect);
connection.on('receivenotificationmessageasync', this.receiveNotificationMessage);
connection.on('EchoMessage', log);
this.connection = connection;
try {
await connection.start();
log(connection);
} catch (e) {
error('An error occurred connecting to the notification web socket', e);
}
}
private async deleteSubscription(id: string) {
try {
await this.graph.api(`${GraphConfig.subscriptionEndpoint}/${id}`).delete();
} catch (e) {
error(e);
}
}
private async removeSubscriptions(subscriptions: Subscription[]): Promise<unknown[]> {
const tasks: Promise<unknown>[] = [];
for (const s of subscriptions) {
// if there is no id or the subscription is expired, skip
if (!s.id || (s.expirationDateTime && new Date(s.expirationDateTime) <= new Date())) continue;
tasks.push(this.deleteSubscription(s.id));
}
return Promise.all(tasks);
}
private startCleanupTimer() {
this.cleanupTimeout = this.timer.setTimeout(this.cleanupTimerSync, appSettings.removalTimerInterval * 1000);
}
private readonly cleanupTimerSync = () => {
void this.cleanupTimer();
};
private readonly cleanupTimer = async () => {
log(`running cleanup timer`);
const offset = Math.min(
appSettings.removalThreshold * 1000,
appSettings.defaultSubscriptionLifetimeInMinutes * 60 * 1000
);
const threshold = new Date(new Date().getTime() - offset).toISOString();
const inactiveSubs = await this.subscriptionCache.loadInactiveSubscriptions(threshold);
let tasks: Promise<unknown>[] = [];
for (const inactive of inactiveSubs) {
tasks.push(this.removeSubscriptions(inactive.subscriptions));
}
await Promise.all(tasks);
tasks = [];
for (const inactive of inactiveSubs) {
tasks.push(this.subscriptionCache.deleteCachedSubscriptions(inactive.chatId));
}
this.startCleanupTimer();
};
public async closeSignalRConnection() {
// stop the connection and set it to undefined so it will reconnect when next subscription is created.
await this.connection?.stop();
this.connection = undefined;
}
private async unsubscribeFromChatNotifications(chatId: string) {
await this.closeSignalRConnection();
const cacheData = await this.subscriptionCache.loadSubscriptions(chatId);
if (cacheData) {
await Promise.all([
this.removeSubscriptions(cacheData.subscriptions),
this.subscriptionCache.deleteCachedSubscriptions(chatId)
]);
}
}
public async subscribeToChatNotifications(chatId: string, sessionId: string) {
this.chatId = chatId;
this.sessionId = sessionId;
// MGT uses a per-user cache, so no concerns of loading the cached data for another user.
const cacheData = await this.subscriptionCache.loadSubscriptions(chatId);
if (cacheData) {
// check subscription validity & renew if all still valid otherwise recreate
const someExpired = cacheData.subscriptions.some(
s => s.expirationDateTime && new Date(s.expirationDateTime) <= new Date()
);
// for a given user + app + chatId + sessionId they only get one websocket and receive all notifications via that websocket.
const webSocketUrl = cacheData.subscriptions.find(s => s.notificationUrl)?.notificationUrl;
if (!someExpired && webSocketUrl) {
// if we have a websocket url and all the subscriptions are valid, we can reuse the websocket and return before recreating subscriptions.
await this.createSignalRConnection(webSocketUrl);
await this.renewChatSubscriptions();
return;
} else if (someExpired) {
// if some are expired, remove them and continue to recreate the subscription
await this.removeSubscriptions(cacheData.subscriptions);
}
await this.subscriptionCache.deleteCachedSubscriptions(chatId);
}
const promises: Promise<unknown>[] = [];
promises.push(
this.subscribeToResource(addPremiumApiSegment(`/chats/${chatId}/messages`), ['created', 'updated', 'deleted'])
);
promises.push(this.subscribeToResource(`/chats/${chatId}/members`, ['created', 'deleted']));
promises.push(this.subscribeToResource(`/chats/${chatId}`, ['updated', 'deleted']));
await Promise.all(promises).catch((e: Error) => {
this?.emitter.graphNotificationClientError(e);
});
}
}