Skip to content

Commit

Permalink
feat: add config switch to use cananry graph
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinbarron committed Oct 26, 2023
1 parent f2a9197 commit 042dcb1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
29 changes: 16 additions & 13 deletions packages/mgt-chat/src/statefulClient/GraphConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ export class GraphConfig {

public static version = 'v1.0';

// public static subscriptionConnectionVersion = 'testprodv1.0e2ewebsockets';
public static canarySubscriptionVersion = 'testprodv1.0e2ewebsockets';

public static webSocketsPrefix = 'websockets:';

public static get graphEndpoint(): GraphEndpoint {
// return GraphConfig.useCanary ? 'https://canary.graph.microsoft.com' :
return 'https://graph.microsoft.com';
return GraphConfig.useCanary ? 'https://canary.graph.microsoft.com' : 'https://graph.microsoft.com';
}

public static get baseCanaryUrl(): string {
return `${GraphConfig.graphEndpoint}/${this.canarySubscriptionVersion}`;
}

public static get subscriptionEndpoint(): string {
return '/subscriptions';
return GraphConfig.useCanary ? `${GraphConfig.baseCanaryUrl}/subscriptions` : '/subscriptions';
}

// public static adjustNotificationUrl(url: string): string {
// let temp = url;
// if (GraphConfig.useCanary && url) {
// temp = url.replace('https://graph.microsoft.com/1.0', GraphConfig.baseCanaryUrl);
// temp = temp.replace('https://graph.microsoft.com/beta', GraphConfig.baseCanaryUrl);
// }
// temp = temp.replace(GraphConfig.version, GraphConfig.subscriptionConnectionVersion);
// return temp.replace('wss:', '');
// }
public static adjustNotificationUrl(url: string): string {
if (GraphConfig.useCanary && url) {
url = url.replace('https://graph.microsoft.com/1.0', GraphConfig.baseCanaryUrl);
url = url.replace('https://graph.microsoft.com/beta', GraphConfig.baseCanaryUrl);
}
return url.replace(GraphConfig.webSocketsPrefix, '');
}
}
19 changes: 11 additions & 8 deletions packages/mgt-chat/src/statefulClient/GraphNotificationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* -------------------------------------------------------------------------------------------
*/

import { BetaGraph, IGraph, Providers, error, log } from '@microsoft/mgt-element';
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 {
Expand Down Expand Up @@ -45,8 +45,6 @@ const isMessageNotification = (o: Notification<Entity>): o is Notification<ChatM
const isMembershipNotification = (o: Notification<Entity>): o is Notification<AadUserConversationMember> =>
o.resource.includes('/members');

const stripWssScheme = (notificationUrl: string): string => notificationUrl.replace('websockets:', '');

export class GraphNotificationClient {
private connection?: HubConnection = undefined;
private renewalInterval = -1;
Expand All @@ -60,6 +58,11 @@ export class GraphNotificationClient {
private get beta() {
return BetaGraph.fromGraph(this._graph);
}
private get subscriptionGraph() {
return GraphConfig.useCanary
? createFromProvider(Providers.globalProvider, GraphConfig.canarySubscriptionVersion, 'mgt-chat')
: this.beta;
}

/**
*
Expand Down Expand Up @@ -168,21 +171,21 @@ export class GraphNotificationClient {
).toISOString();
const subscriptionDefinition: Subscription = {
changeType: changeTypes.join(','),
notificationUrl: 'websockets:',
notificationUrl: GraphConfig.webSocketsPrefix,
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.beta
.api(GraphConfig.subscriptionEndpoint)
const subscription: Subscription = (await this.subscriptionGraph
.api(subscriptionEndpoint)
.post(subscriptionDefinition)) as Subscription;
if (!subscription?.notificationUrl) throw new Error('Subscription not created');
log(subscription);
// subscription.notificationUrl = GraphConfig.adjustNotificationUrl(subscription.notificationUrl);

const awaits: Promise<void>[] = [];
// Cache the subscription in storage for re-hydration on page refreshes
Expand Down Expand Up @@ -262,7 +265,7 @@ export class GraphNotificationClient {
withCredentials: false
};
const connection = new HubConnectionBuilder()
.withUrl(stripWssScheme(notificationUrl), connectionOptions)
.withUrl(GraphConfig.adjustNotificationUrl(notificationUrl), connectionOptions)
.withAutomaticReconnect()
.configureLogging(LogLevel.Information)
.build();
Expand Down
2 changes: 1 addition & 1 deletion packages/mgt-element/src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class Graph implements IGraph {
* @returns {Graph}
* @memberof Graph
*/
export const createFromProvider = (provider: IProvider, version?: string, component?: Element): Graph => {
export const createFromProvider = (provider: IProvider, version?: string, component?: Element | string): Graph => {
const middleware: Middleware[] = [
new AuthenticationHandler(provider),
new RetryHandler(new RetryHandlerOptions()),
Expand Down

0 comments on commit 042dcb1

Please sign in to comment.