Skip to content

Commit cb240cb

Browse files
authored
feat: add config switch to use cananry graph (#2802)
1 parent f2a9197 commit cb240cb

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

.yarn/install-state.gz

0 Bytes
Binary file not shown.

packages/mgt-chat/src/statefulClient/GraphConfig.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,27 @@ export class GraphConfig {
55

66
public static version = 'v1.0';
77

8-
// public static subscriptionConnectionVersion = 'testprodv1.0e2ewebsockets';
8+
public static canarySubscriptionVersion = 'testprodv1.0e2ewebsockets';
9+
10+
public static webSocketsPrefix = 'websockets:';
911

1012
public static get graphEndpoint(): GraphEndpoint {
11-
// return GraphConfig.useCanary ? 'https://canary.graph.microsoft.com' :
12-
return 'https://graph.microsoft.com';
13+
return GraphConfig.useCanary ? 'https://canary.graph.microsoft.com' : 'https://graph.microsoft.com';
14+
}
15+
16+
public static get baseCanaryUrl(): string {
17+
return `${GraphConfig.graphEndpoint}/${this.canarySubscriptionVersion}`;
1318
}
1419

1520
public static get subscriptionEndpoint(): string {
16-
return '/subscriptions';
21+
return GraphConfig.useCanary ? `${GraphConfig.baseCanaryUrl}/subscriptions` : '/subscriptions';
1722
}
1823

19-
// public static adjustNotificationUrl(url: string): string {
20-
// let temp = url;
21-
// if (GraphConfig.useCanary && url) {
22-
// temp = url.replace('https://graph.microsoft.com/1.0', GraphConfig.baseCanaryUrl);
23-
// temp = temp.replace('https://graph.microsoft.com/beta', GraphConfig.baseCanaryUrl);
24-
// }
25-
// temp = temp.replace(GraphConfig.version, GraphConfig.subscriptionConnectionVersion);
26-
// return temp.replace('wss:', '');
27-
// }
24+
public static adjustNotificationUrl(url: string): string {
25+
if (GraphConfig.useCanary && url) {
26+
url = url.replace('https://graph.microsoft.com/1.0', GraphConfig.baseCanaryUrl);
27+
url = url.replace('https://graph.microsoft.com/beta', GraphConfig.baseCanaryUrl);
28+
}
29+
return url.replace(GraphConfig.webSocketsPrefix, '');
30+
}
2831
}

packages/mgt-chat/src/statefulClient/GraphNotificationClient.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { BetaGraph, IGraph, Providers, error, log } from '@microsoft/mgt-element';
8+
import { BetaGraph, IGraph, Providers, createFromProvider, error, log } from '@microsoft/mgt-element';
99
import { HubConnection, HubConnectionBuilder, IHttpConnectionOptions, LogLevel } from '@microsoft/signalr';
1010
import { ThreadEventEmitter } from './ThreadEventEmitter';
1111
import type {
@@ -45,8 +45,6 @@ const isMessageNotification = (o: Notification<Entity>): o is Notification<ChatM
4545
const isMembershipNotification = (o: Notification<Entity>): o is Notification<AadUserConversationMember> =>
4646
o.resource.includes('/members');
4747

48-
const stripWssScheme = (notificationUrl: string): string => notificationUrl.replace('websockets:', '');
49-
5048
export class GraphNotificationClient {
5149
private connection?: HubConnection = undefined;
5250
private renewalInterval = -1;
@@ -60,6 +58,11 @@ export class GraphNotificationClient {
6058
private get beta() {
6159
return BetaGraph.fromGraph(this._graph);
6260
}
61+
private get subscriptionGraph() {
62+
return GraphConfig.useCanary
63+
? createFromProvider(Providers.globalProvider, GraphConfig.canarySubscriptionVersion, 'mgt-chat')
64+
: this.beta;
65+
}
6366

6467
/**
6568
*
@@ -168,21 +171,21 @@ export class GraphNotificationClient {
168171
).toISOString();
169172
const subscriptionDefinition: Subscription = {
170173
changeType: changeTypes.join(','),
171-
notificationUrl: 'websockets:',
174+
notificationUrl: GraphConfig.webSocketsPrefix,
172175
resource: resourcePath,
173176
expirationDateTime,
174177
includeResourceData: true,
175178
clientState: 'wsssecret'
176179
};
177180

178181
log('subscribing to changes for ' + resourcePath);
182+
const subscriptionEndpoint = GraphConfig.subscriptionEndpoint;
179183
// send subscription POST to Graph
180-
const subscription: Subscription = (await this.beta
181-
.api(GraphConfig.subscriptionEndpoint)
184+
const subscription: Subscription = (await this.subscriptionGraph
185+
.api(subscriptionEndpoint)
182186
.post(subscriptionDefinition)) as Subscription;
183187
if (!subscription?.notificationUrl) throw new Error('Subscription not created');
184188
log(subscription);
185-
// subscription.notificationUrl = GraphConfig.adjustNotificationUrl(subscription.notificationUrl);
186189

187190
const awaits: Promise<void>[] = [];
188191
// Cache the subscription in storage for re-hydration on page refreshes
@@ -262,7 +265,7 @@ export class GraphNotificationClient {
262265
withCredentials: false
263266
};
264267
const connection = new HubConnectionBuilder()
265-
.withUrl(stripWssScheme(notificationUrl), connectionOptions)
268+
.withUrl(GraphConfig.adjustNotificationUrl(notificationUrl), connectionOptions)
266269
.withAutomaticReconnect()
267270
.configureLogging(LogLevel.Information)
268271
.build();

packages/mgt-element/src/Graph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class Graph implements IGraph {
147147
* @returns {Graph}
148148
* @memberof Graph
149149
*/
150-
export const createFromProvider = (provider: IProvider, version?: string, component?: Element): Graph => {
150+
export const createFromProvider = (provider: IProvider, version?: string, component?: Element | string): Graph => {
151151
const middleware: Middleware[] = [
152152
new AuthenticationHandler(provider),
153153
new RetryHandler(new RetryHandlerOptions()),

0 commit comments

Comments
 (0)