Skip to content

Commit

Permalink
Pass userID prop to chat adapter setUserId() (#3544)
Browse files Browse the repository at this point in the history
* default sending user id to DLJS

* Bump botframework-directlinejs@0.14.0

* Add CHANGELOG and comments

* Bump to botframework-directlinejs@0.14.1

* Add test

* Add eslint for test

* Call setUserId only if needed

Co-authored-by: William Wong <compulim@hotmail.com>
Co-authored-by: William Wong <compulim@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 17, 2021
1 parent e274fa9 commit ec6963d
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 67 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Resolves [#3666](https://github.com/microsoft/BotFramework-WebChat/issues/3666). Added support of sovereign clouds when using Direct Line Speech, by [@compulim](https://github.com/compulim) in PR [#3694](https://github.com/microsoft/BotFramework-WebChat/pull/3694)
- Please refer to [`DIRECT_LINE_SPEECH.md`](https://github.com/microsoft/BotFramework-WebChat/blob/master/docs/DIRECT_LINE_SPEECH.md#directlinespeechcredentials) for instructions
- Resolves [#2996](https://github.com/microsoft/BotFramework-WebChat/issues/2996). Added transcript navigation by keyboard navigation keys, by [@compulim](https://github.com/compulim) in PR [#3703](https://github.com/microsoft/BotFramework-WebChat/pull/3703)
- Resolves [#3544](https://github.com/microsoft/BotFramework-WebChat/issues/3544). Send user ID from props to chat adapter, by [@timenick](https://github.com/timenick) in PR [#3544)(https://github.com/microsoft/BotFramework-WebChat/issues/3544).

### Fixed

Expand All @@ -58,7 +59,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- Bumped all dependencies to the latest versions, by [@compulim](https://github.com/compulim) in PR [#3594](https://github.com/microsoft/BotFramework-WebChat/pull/3594) and PR [#3694](https://github.com/microsoft/BotFramework-WebChat/pull/3694)
- Bumped all dependencies to the latest versions, by [@compulim](https://github.com/compulim) in PR [#3594](https://github.com/microsoft/BotFramework-WebChat/pull/3594), PR [#3694](https://github.com/microsoft/BotFramework-WebChat/pull/3694), and PR [#3544](https://github.com/microsoft/BotFramework-WebChat/pull/3544)
- Development dependencies
- [`@babel/cli@7.12.1`](https://npmjs.com/package/@babel/cli)
- [`@babel/core@7.12.3`](https://npmjs.com/package/@babel/core)
Expand Down Expand Up @@ -96,6 +97,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [`webpack@4.44.2`](https://npmjs.com/package/webpack)
- Production dependencies
- [`@babel/runtime@7.12.5`](https://npmjs.com/package/@babel/runtime)
- [`botframework-directlinejs@0.14.1`](https://npmjs.com/package/botframework-directlinejs)
- [`globalize@1.6.0`](https://npmjs.com/package/globalize)
- [`markdown-it@12.0.2`](https://npmjs.com/package/markdown-it)
- [`microsoft-cognitiveservices-speech-sdk@1.15.1`](https://npmjs.com/package/microsoft-cognitiveservices-speech-sdk)
Expand Down
2 changes: 2 additions & 0 deletions __tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
globals:
runHTMLTest: readonly
ignorePatterns:
- "**/*.js"
- "!/html/__jest__/*.js"
Expand Down
52 changes: 52 additions & 0 deletions __tests__/html/directLine.setUserId.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<script crossorigin="anonymous" src="/__dist__/testharness.js"></script>
<script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
</head>
<body>
<div id="webchat"></div>
<script type="text/babel" data-presets="env,stage-3,react">
const {
WebChatTest: {
conditions,
createDirectLineWithTranscript,
createStore,
expect,
host,
pageObjects,
timeouts,
token
}
} = window;

(async function () {
let userId;
const directLine = createDirectLineWithTranscript([]);

directLine.setUserId = nextUserId => {
userId = nextUserId;
};

window.WebChat.renderWebChat(
{
directLine,
store: createStore(),
userID: 'u-12345'
},
document.getElementById('webchat')
);

await pageObjects.wait(conditions.uiConnected(), timeouts.directLine);

expect(userId).toBe('u-12345');

await host.done();
})().catch(async err => {
console.error(err);

await host.error(err);
});
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions __tests__/html/directLine.setUserId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @jest-environment ./__tests__/html/__jest__/WebChatEnvironment.js
*/

describe('DirectLine', () => {
test('should set user ID if setUserID function is provided', () => runHTMLTest('directLine.setUserId.html'));
});
26 changes: 16 additions & 10 deletions packages/bundle/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"@babel/runtime": "7.12.5",
"adaptivecards": "2.5.0",
"botframework-directlinejs": "0.13.1",
"botframework-directlinejs": "0.14.1",
"botframework-directlinespeech-sdk": "0.0.0-0",
"botframework-webchat-component": "0.0.0-0",
"botframework-webchat-core": "0.0.0-0",
Expand Down
29 changes: 18 additions & 11 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@types/node": "^14.14.6",
"babel-plugin-istanbul": "^6.0.0",
"babel-plugin-transform-inline-environment-variables": "^0.4.3",
"botframework-directlinejs": "0.13.0",
"botframework-directlinejs": "0.14.1",
"concurrently": "^5.3.0",
"eslint": "^7.12.1",
"eslint-plugin-node": "^11.1.0",
Expand Down
30 changes: 22 additions & 8 deletions packages/core/src/sagas/connectSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,38 @@ function rectifyUserID(directLine, userIDFromAction) {
const { token } = directLine;
const { user: userIDFromToken } = decode(token) || {};

const result = {
fromAction: userIDFromAction,
fromToken: userIDFromToken
};

if (userIDFromToken) {
if (userIDFromAction && userIDFromAction !== userIDFromToken) {
console.warn(
'Web Chat: user ID is both specified in the Direct Line token and passed in, will use the user ID from the token.'
);
}

return userIDFromToken;
result.final = userIDFromToken;
} else if (userIDFromAction) {
if (typeof userIDFromAction !== 'string') {
console.warn('Web Chat: user ID must be a string.');

return randomUserID();
result.final = randomUserID();
} else if (/^dl_/u.test(userIDFromAction)) {
console.warn(
'Web Chat: user ID prefixed with "dl_" is reserved and must be embedded into the Direct Line token to prevent forgery.'
);

return randomUserID();
result.final = randomUserID();
} else {
result.final = userIDFromAction;
}
} else {
return randomUserID();
result.final = randomUserID();
}

return userIDFromAction;
return result;
}

// We could make this a Promise instead of saga (function generator) to make the code cleaner, if:
Expand Down Expand Up @@ -160,21 +167,28 @@ function runAsyncEffectUntilDisconnect(baseAction, callEffectFactory) {
});
}

export default function*() {
export default function* () {
for (;;) {
const {
payload: { directLine, userID: userIDFromAction, username }
} = yield take(CONNECT);

const updateConnectionStatusTask = yield fork(observeAndPutConnectionStatusUpdate, directLine);
let disconnectMeta;
const rectifiedUserID = rectifyUserID(directLine, userIDFromAction);

// TODO: [P2] Checks if this attached subtask will get killed if the parent task is complete (peacefully), errored out, or cancelled.
const meta = {
userID: rectifyUserID(directLine, userIDFromAction),
userID: rectifiedUserID.final,
username
};

// Send user ID to DirectLineJS if it was specified from props of <API.Composer>.
// However, DirectLineJS may still prefer the user ID from token if it is burnt into the token.
// To prevent DirectLineJS giving false warnings, we will only call setUserId() if it is different than the token.
directLine.setUserId && rectifiedUserID.fromToken !== meta.userID && directLine.setUserId(meta.userID);

let disconnectMeta;

// We will dispatch CONNECT_PENDING, wait for connect completed, errored, or cancelled (thru disconnect).
// Then dispatch CONNECT_FULFILLED/CONNECT_REJECTED as needed.
try {
Expand Down
Loading

0 comments on commit ec6963d

Please sign in to comment.