-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple chat components sharing a subscription (#2913)
makes sessionId to be unique per component makes groupId unique for a given chatId per browser instance
- Loading branch information
1 parent
188b1cb
commit eeea1d6
Showing
11 changed files
with
144 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/mgt-chat/src/statefulClient/chatOperationScopes.tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/mgt-chat/src/statefulClient/getOrGenerateGroupId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { v4 as uuid } from 'uuid'; | ||
|
||
const keyPrefix = 'mgt-chat-group-id'; | ||
|
||
/** | ||
* reads a string from session storage, or if there is no string for the keyName, generate a new uuid and place in storage | ||
*/ | ||
export const getOrGenerateGroupId = (chatId: string) => { | ||
const key = `${keyPrefix}::${chatId}`; | ||
const value = localStorage.getItem(key); | ||
|
||
if (value) { | ||
return value; | ||
} else { | ||
const newValue = uuid(); | ||
localStorage.setItem(key, newValue); | ||
return newValue; | ||
} | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/mgt-chat/src/statefulClient/replaceOrAppendSessionId.tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { replaceOrAppendSessionId } from './replaceOrAppendSessionId'; | ||
import { expect } from '@open-wc/testing'; | ||
|
||
describe('replaceOrAppendSessionId tests', () => { | ||
it('should replace existing sessionid', async () => { | ||
const result = replaceOrAppendSessionId('https://graph.microsoft.com/1.0/me?sessionid=default', '123'); | ||
await expect(result).to.equal('https://graph.microsoft.com/1.0/me?sessionid=123'); | ||
}); | ||
it('should replace existing sessionid when there are query string params after sessionid', async () => { | ||
const result = replaceOrAppendSessionId('https://graph.microsoft.com/1.0/me?sessionid=default&foo=bar', '123'); | ||
await expect(result).to.equal('https://graph.microsoft.com/1.0/me?sessionid=123&foo=bar'); | ||
}); | ||
it('should replace existing sessionid when there are query string params before sessionid', async () => { | ||
const result = replaceOrAppendSessionId('https://graph.microsoft.com/1.0/me?foo=bar&sessionid=default', '123'); | ||
await expect(result).to.equal('https://graph.microsoft.com/1.0/me?foo=bar&sessionid=123'); | ||
}); | ||
it('should append sessionid there is a query string params with sessionid as a substring', async () => { | ||
const result = replaceOrAppendSessionId('https://graph.microsoft.com/1.0/me?foosessionid=bar', '123'); | ||
await expect(result).to.equal('https://graph.microsoft.com/1.0/me?foosessionid=bar&sessionid=123'); | ||
}); | ||
it('should replace sessionid there is also a query string params with sessionid as a substring', async () => { | ||
const result = replaceOrAppendSessionId( | ||
'https://graph.microsoft.com/1.0/me?foosessionid=bar&sessionid=default&bar=too', | ||
'123' | ||
); | ||
await expect(result).to.equal('https://graph.microsoft.com/1.0/me?foosessionid=bar&sessionid=123&bar=too'); | ||
}); | ||
it('should append sessionid with ? when no query params present', async () => { | ||
const result = replaceOrAppendSessionId('https://graph.microsoft.com/1.0/me', '123'); | ||
await expect(result).to.equal('https://graph.microsoft.com/1.0/me?sessionid=123'); | ||
}); | ||
it('should append sessionid with & when query params are present', async () => { | ||
const result = replaceOrAppendSessionId('https://graph.microsoft.com/1.0/me?foo=bar', '123'); | ||
await expect(result).to.equal('https://graph.microsoft.com/1.0/me?foo=bar&sessionid=123'); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/mgt-chat/src/statefulClient/replaceOrAppendSessionId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* ------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. | ||
* See License in the project root for license information. | ||
* ------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
export const replaceOrAppendSessionId = (url: string, sessionId: string) => { | ||
// match on any whole query parameter of sessionid and include the value | ||
const sessionIdRegex = /([?&]{1})sessionid=[^&]*/; | ||
if (url.match(sessionIdRegex)) { | ||
url = url.replace(sessionIdRegex, `$1sessionid=${sessionId}`); | ||
} else { | ||
const paramSeparator = url.indexOf('?') > -1 ? '&' : '?'; | ||
url = `${url}${paramSeparator}sessionid=${sessionId}`; | ||
} | ||
return url; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.