Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee30b14cba | ||
|
|
80c00149d2 | ||
|
|
a2a48771e1 | ||
|
|
7bd6cf2466 | ||
|
|
ac9247fecb | ||
|
|
8439a1d100 | ||
|
|
b709245b46 | ||
|
|
52e30336ad | ||
|
|
bee884c52f | ||
|
|
2ffa63d583 | ||
|
|
a0d08f8f88 | ||
|
|
09cc934fbd | ||
|
|
5a0ed5d36d | ||
|
|
dbbe188adc | ||
|
|
91bec23c48 |
@@ -24,7 +24,13 @@ export const mockInitCrypto = jest.fn()
|
|||||||
export const mockStartClient = jest.fn(() => {
|
export const mockStartClient = jest.fn(() => {
|
||||||
return Promise.resolve('value');
|
return Promise.resolve('value');
|
||||||
});
|
});
|
||||||
export const mockOnce = jest.fn()
|
export const mockOnce = jest
|
||||||
|
.fn()
|
||||||
|
.mockImplementation((event, callback) => {
|
||||||
|
if (event === 'sync') {
|
||||||
|
callback('PREPARED')
|
||||||
|
}
|
||||||
|
})
|
||||||
export const mockStopClient = jest.fn(() => {
|
export const mockStopClient = jest.fn(() => {
|
||||||
return Promise.resolve('value');
|
return Promise.resolve('value');
|
||||||
});
|
});
|
||||||
|
|||||||
38
dist/bookmarklet.js
vendored
38
dist/bookmarklet.js
vendored
File diff suppressed because one or more lines are too long
38
dist/chatbox.js
vendored
38
dist/chatbox.js
vendored
File diff suppressed because one or more lines are too long
36
dist/component.js
vendored
Normal file
36
dist/component.js
vendored
Normal file
File diff suppressed because one or more lines are too long
2
dist/index.html
vendored
2
dist/index.html
vendored
@@ -25,7 +25,7 @@
|
|||||||
anonymousDisplayName: 'Anonymous',
|
anonymousDisplayName: 'Anonymous',
|
||||||
position: 'bottom right',
|
position: 'bottom right',
|
||||||
size: 'large',
|
size: 'large',
|
||||||
maxWaitTime: 6000*3,
|
maxWaitTime: 1000*60*3, // 3 minutes
|
||||||
}
|
}
|
||||||
|
|
||||||
EmbeddableChatbox.mount(config);
|
EmbeddableChatbox.mount(config);
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "private-safesupport-chatbox",
|
"name": "private-safesupport-chatbox",
|
||||||
"version": "1.2.2",
|
"version": "2.1.2",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "private-safesupport-chatbox",
|
"name": "private-safesupport-chatbox",
|
||||||
"version": "1.2.2",
|
"version": "2.1.2",
|
||||||
"description": "A secure and private embeddable chatbox that connects to Riot",
|
"description": "A secure and private embeddable chatbox that connects to Riot",
|
||||||
"main": "dist/chatbox.js",
|
"main": "dist/chatbox.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -118,6 +118,8 @@
|
|||||||
"node-sass": "^4.13.1",
|
"node-sass": "^4.13.1",
|
||||||
"postcss-increase-specificity": "0.6.0",
|
"postcss-increase-specificity": "0.6.0",
|
||||||
"postcss-loader": "3.0.0",
|
"postcss-loader": "3.0.0",
|
||||||
|
"react": "^16.8.6",
|
||||||
|
"react-dom": "^16.8.6",
|
||||||
"sass-loader": "8.0.0",
|
"sass-loader": "8.0.0",
|
||||||
"style-loader": "1.1.2",
|
"style-loader": "1.1.2",
|
||||||
"wait-for-expect": "^3.0.2",
|
"wait-for-expect": "^3.0.2",
|
||||||
@@ -137,11 +139,13 @@
|
|||||||
"node-localstorage": "^2.1.5",
|
"node-localstorage": "^2.1.5",
|
||||||
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
||||||
"prop-types": "^15.6.2",
|
"prop-types": "^15.6.2",
|
||||||
"react": "^16.8.6",
|
|
||||||
"react-dom": "^16.8.6",
|
|
||||||
"react-onclickoutside": "^6.9.0",
|
"react-onclickoutside": "^6.9.0",
|
||||||
"react-test-renderer": "^16.13.0",
|
"react-test-renderer": "^16.13.0",
|
||||||
"react-transition-group": "^4.0.0",
|
"react-transition-group": "^4.0.0",
|
||||||
"uuidv4": "^6.0.2"
|
"uuidv4": "^6.0.2"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": "^16.8.6",
|
||||||
|
"react-dom": "^16.8.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
anonymousDisplayName: 'Anonymous',
|
anonymousDisplayName: 'Anonymous',
|
||||||
position: 'bottom right',
|
position: 'bottom right',
|
||||||
size: 'large',
|
size: 'large',
|
||||||
maxWaitTime: 6000*3,
|
maxWaitTime: 1000*60*3, // 3 minutes
|
||||||
}
|
}
|
||||||
|
|
||||||
EmbeddableChatbox.mount(config);
|
EmbeddableChatbox.mount(config);
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ const DEFAULT_POSITION = 'bottom right'
|
|||||||
const DEFAULT_SIZE = 'large'
|
const DEFAULT_SIZE = 'large'
|
||||||
const DEFAULT_MAX_WAIT_MS = 600000 // 10 minutes
|
const DEFAULT_MAX_WAIT_MS = 600000 // 10 minutes
|
||||||
const DEFAULT_WAIT_INTERVAL_MS = 120000 // 2 minutes
|
const DEFAULT_WAIT_INTERVAL_MS = 120000 // 2 minutes
|
||||||
|
const DEFAULT_DOCK_LABEL = 'Start a new chat'
|
||||||
|
|
||||||
class ChatBox extends React.Component {
|
class ChatBox extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@@ -166,10 +166,13 @@ class ChatBox extends React.Component {
|
|||||||
await this.state.client.deactivateAccount(auth, true)
|
await this.state.client.deactivateAccount(auth, true)
|
||||||
await this.state.client.stopClient()
|
await this.state.client.stopClient()
|
||||||
await this.state.client.clearStores()
|
await this.state.client.clearStores()
|
||||||
this.setState({ client: null })
|
this.setState({ client: null, ready: true }) // no more loading animation
|
||||||
|
window.clearInterval(this.state.waitIntervalId) // no more waiting messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.state.localStorage) {
|
||||||
this.state.localStorage.clear()
|
this.state.localStorage.clear()
|
||||||
|
}
|
||||||
|
|
||||||
if (resetState) {
|
if (resetState) {
|
||||||
this.setState(this.initialState)
|
this.setState(this.initialState)
|
||||||
@@ -458,40 +461,15 @@ class ChatBox extends React.Component {
|
|||||||
const decryptionErrors = {...this.state.decryptionErrors}
|
const decryptionErrors = {...this.state.decryptionErrors}
|
||||||
delete decryptionErrors[message.id]
|
delete decryptionErrors[message.id]
|
||||||
|
|
||||||
const isOfflineNotice = message.content.msgtype === "m.notice" && message.content.body === CHAT_IS_OFFLINE_NOTICE
|
|
||||||
|
|
||||||
let newMessage = message
|
|
||||||
|
|
||||||
// when the bot sends a notice that the chat is offline
|
|
||||||
// replace the message with the client-configured message
|
|
||||||
// for now we're treating m.notice and m.text messages the same
|
|
||||||
if (isOfflineNotice) {
|
|
||||||
newMessage = {
|
|
||||||
...message,
|
|
||||||
content: {
|
|
||||||
...message.content,
|
|
||||||
body: this.props.chatOfflineMessage
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.handleChatOffline()
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
messages: {
|
messages: {
|
||||||
...this.state.messages,
|
...this.state.messages,
|
||||||
[message.id]: newMessage,
|
[message.id]: message,
|
||||||
},
|
},
|
||||||
decryptionErrors
|
decryptionErrors
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
handleChatOffline = () => {
|
|
||||||
this.exitChat(false) // close the chat connection but keep chatbox state
|
|
||||||
window.clearInterval(this.state.waitIntervalId) // no more waiting messages
|
|
||||||
window.clearInterval(this.state.waitTimeoutId) // no more waiting messages
|
|
||||||
this.setState({ ready: true }) // no more loading animation
|
|
||||||
}
|
|
||||||
|
|
||||||
handleKeyDown = (e) => {
|
handleKeyDown = (e) => {
|
||||||
switch (e.keyCode) {
|
switch (e.keyCode) {
|
||||||
case 27:
|
case 27:
|
||||||
@@ -546,7 +524,6 @@ class ChatBox extends React.Component {
|
|||||||
this.verifyAllRoomDevices(client, room)
|
this.verifyAllRoomDevices(client, room)
|
||||||
this.setState({ facilitatorId: sender, ready: true })
|
this.setState({ facilitatorId: sender, ready: true })
|
||||||
window.clearInterval(this.state.waitIntervalId)
|
window.clearInterval(this.state.waitIntervalId)
|
||||||
window.clearInterval(this.state.waitTimeoutId)
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -568,6 +545,26 @@ class ChatBox extends React.Component {
|
|||||||
this.setState({ typingStatus: null })
|
this.setState({ typingStatus: null })
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.on("event", (event) => {
|
||||||
|
const eventType = event.getType()
|
||||||
|
const content = event.getContent()
|
||||||
|
|
||||||
|
if (eventType === 'm.bot.signal') {
|
||||||
|
this.handleBotSignal(content.signal)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
handleBotSignal = (signal) => {
|
||||||
|
switch (signal) {
|
||||||
|
case 'END_CHAT':
|
||||||
|
this.displayBotMessage({ body: this.props.exitMessage })
|
||||||
|
return this.exitChat(false); // keep chat state
|
||||||
|
case 'CHAT_OFFLINE':
|
||||||
|
this.displayBotMessage({ body: this.props.chatOfflineMessage })
|
||||||
|
return this.exitChat(false); // keep chat state
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps, prevState) {
|
componentDidUpdate(prevProps, prevState) {
|
||||||
@@ -624,14 +621,7 @@ class ChatBox extends React.Component {
|
|||||||
}
|
}
|
||||||
}, this.props.waitInterval)
|
}, this.props.waitInterval)
|
||||||
|
|
||||||
const waitTimeoutId = window.setTimeout(() => {
|
this.setState({ waitIntervalId })
|
||||||
if (!this.state.facilitatorId && !this.state.ready) {
|
|
||||||
this.displayBotMessage({ body: this.props.chatUnavailableMessage })
|
|
||||||
this.handleChatOffline()
|
|
||||||
}
|
|
||||||
}, this.props.maxWaitTime)
|
|
||||||
|
|
||||||
this.setState({ waitIntervalId, waitTimeoutId })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRejectTerms = () => {
|
handleRejectTerms = () => {
|
||||||
@@ -766,7 +756,7 @@ class ChatBox extends React.Component {
|
|||||||
)}
|
)}
|
||||||
}
|
}
|
||||||
</Transition>
|
</Transition>
|
||||||
{showDock && !roomId && <Dock handleToggleOpen={this.handleToggleOpen} size={this.props.size} />}
|
{showDock && !roomId && <Dock handleToggleOpen={this.handleToggleOpen} size={this.props.size} label={this.props.dockLabel} />}
|
||||||
{showDock && roomId && <Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />}
|
{showDock && roomId && <Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -792,6 +782,7 @@ ChatBox.propTypes = {
|
|||||||
size: PropTypes.oneOf(['small', 'large']),
|
size: PropTypes.oneOf(['small', 'large']),
|
||||||
maxWaitTime: PropTypes.number,
|
maxWaitTime: PropTypes.number,
|
||||||
waitInterval: PropTypes.number,
|
waitInterval: PropTypes.number,
|
||||||
|
dockLabel: PropTypes.string,
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatBox.defaultProps = {
|
ChatBox.defaultProps = {
|
||||||
@@ -812,6 +803,7 @@ ChatBox.defaultProps = {
|
|||||||
size: DEFAULT_SIZE,
|
size: DEFAULT_SIZE,
|
||||||
maxWaitTime: DEFAULT_MAX_WAIT_MS,
|
maxWaitTime: DEFAULT_MAX_WAIT_MS,
|
||||||
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
|
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
|
||||||
|
dockLabel: DEFAULT_DOCK_LABEL,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ChatBox;
|
export default ChatBox;
|
||||||
|
|||||||
@@ -115,9 +115,7 @@ describe('Chatbox', () => {
|
|||||||
expect(createClient).toHaveBeenCalled()
|
expect(createClient).toHaveBeenCalled()
|
||||||
expect(mockInitCrypto).toHaveBeenCalled()
|
expect(mockInitCrypto).toHaveBeenCalled()
|
||||||
expect(mockStartClient).toHaveBeenCalled()
|
expect(mockStartClient).toHaveBeenCalled()
|
||||||
expect(mockCreateRoom).toHaveBeenCalled()
|
expect(mockOnce).toHaveBeenCalled()
|
||||||
expect(mockSetPowerLevel).toHaveBeenCalled()
|
|
||||||
expect(mockOn).toHaveBeenCalled()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('rejecting terms should not start chat', async () => {
|
test('rejecting terms should not start chat', async () => {
|
||||||
@@ -146,6 +144,10 @@ describe('Chatbox', () => {
|
|||||||
|
|
||||||
acceptButton.simulate('click')
|
acceptButton.simulate('click')
|
||||||
|
|
||||||
|
await waitForExpect(() => {
|
||||||
|
expect(mockOnce).toHaveBeenCalled()
|
||||||
|
});
|
||||||
|
|
||||||
await waitForExpect(() => {
|
await waitForExpect(() => {
|
||||||
expect(mockCreateRoom).toHaveBeenCalled()
|
expect(mockCreateRoom).toHaveBeenCalled()
|
||||||
});
|
});
|
||||||
@@ -169,7 +171,7 @@ describe('Chatbox', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
test('decryption failure should lead to a new unencrypted chat', async () => {
|
test('decryption failure should handle the message event and save the event ID in state', async () => {
|
||||||
const chatbox = mount(<Chatbox {...testConfig} />)
|
const chatbox = mount(<Chatbox {...testConfig} />)
|
||||||
const dock = chatbox.find('button.dock')
|
const dock = chatbox.find('button.dock')
|
||||||
const instance = chatbox.instance()
|
const instance = chatbox.instance()
|
||||||
@@ -183,25 +185,25 @@ describe('Chatbox', () => {
|
|||||||
acceptButton.simulate('click')
|
acceptButton.simulate('click')
|
||||||
|
|
||||||
await waitForExpect(() => {
|
await waitForExpect(() => {
|
||||||
expect(mockCreateRoom).toHaveBeenCalled()
|
expect(mockOnce).toHaveBeenCalled()
|
||||||
});
|
});
|
||||||
|
|
||||||
jest.spyOn(instance, 'initializeUnencryptedChat')
|
jest.spyOn(instance, 'handleMessageEvent')
|
||||||
instance.handleDecryptionError()
|
|
||||||
|
instance.handleDecryptionError({
|
||||||
|
getId: () => 'test_event_id',
|
||||||
|
getType: () => 'm.message',
|
||||||
|
getSender: () => 'sender',
|
||||||
|
getRoomId: () => 'room id',
|
||||||
|
getContent: () => ({ body: 'test msg' }),
|
||||||
|
getTs: () => '123',
|
||||||
|
})
|
||||||
|
|
||||||
await waitForExpect(() => {
|
await waitForExpect(() => {
|
||||||
expect(mockLeave).toHaveBeenCalled()
|
expect(instance.handleMessageEvent).toHaveBeenCalled()
|
||||||
});
|
})
|
||||||
|
|
||||||
await waitForExpect(() => {
|
expect(chatbox.state().decryptionErrors).toEqual({ 'test_event_id': true })
|
||||||
expect(mockStopClient).toHaveBeenCalled()
|
|
||||||
});
|
|
||||||
|
|
||||||
await waitForExpect(() => {
|
|
||||||
expect(mockClearStores).toHaveBeenCalled()
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(instance.initializeUnencryptedChat).toHaveBeenCalled()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('creating an unencrypted chat', async () => {
|
test('creating an unencrypted chat', async () => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { Fragment } from "react"
|
import React, { Fragment } from "react"
|
||||||
import PropTypes from "prop-types"
|
import PropTypes from "prop-types"
|
||||||
|
|
||||||
const Dock = ({ handleToggleOpen, size }) => {
|
const Dock = ({ handleToggleOpen, size, label }) => {
|
||||||
return(
|
return(
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@@ -13,11 +13,11 @@ const Dock = ({ handleToggleOpen, size }) => {
|
|||||||
{
|
{
|
||||||
size === 'small' ?
|
size === 'small' ?
|
||||||
<div id="open-chatbox-label">
|
<div id="open-chatbox-label">
|
||||||
<span>Chat</span><span className="icon">+</span>
|
<span>{label}</span><span className="icon">+</span>
|
||||||
</div>
|
</div>
|
||||||
:
|
:
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<div id="open-chatbox-label">Start a new chat</div>
|
<div id="open-chatbox-label">{label}</div>
|
||||||
<div className="label-icon">
|
<div className="label-icon">
|
||||||
<div className={`btn-icon`} aria-label={`Open support chat window`}>+</div>
|
<div className={`btn-icon`} aria-label={`Open support chat window`}>+</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
3
src/outputs/component.js
Normal file
3
src/outputs/component.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import Chatbox from '../components/chatbox';
|
||||||
|
|
||||||
|
export default Chatbox;
|
||||||
3
src/setupTests.js
Normal file
3
src/setupTests.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { configure } from 'enzyme';
|
||||||
|
import Adapter from 'enzyme-adapter-react-16';
|
||||||
|
configure({ adapter: new Adapter() });
|
||||||
@@ -94,4 +94,29 @@ module.exports = [{
|
|||||||
publicPath: '/',
|
publicPath: '/',
|
||||||
filename: 'bookmarklet.js',
|
filename: 'bookmarklet.js',
|
||||||
},
|
},
|
||||||
|
}, {
|
||||||
|
...defaultConfig,
|
||||||
|
entry: './src/outputs/component.js',
|
||||||
|
output: {
|
||||||
|
path: distDir,
|
||||||
|
publicPath: '/',
|
||||||
|
filename: 'component.js',
|
||||||
|
library: 'Chatbox',
|
||||||
|
libraryExport: 'default',
|
||||||
|
libraryTarget: 'commonjs2',
|
||||||
|
},
|
||||||
|
externals: {
|
||||||
|
react: {
|
||||||
|
commonjs: "react",
|
||||||
|
commonjs2: "react",
|
||||||
|
amd: "React",
|
||||||
|
root: "React"
|
||||||
|
},
|
||||||
|
"react-dom": {
|
||||||
|
commonjs: "react-dom",
|
||||||
|
commonjs2: "react-dom",
|
||||||
|
amd: "ReactDOM",
|
||||||
|
root: "ReactDOM"
|
||||||
|
}
|
||||||
|
}
|
||||||
}];
|
}];
|
||||||
|
|||||||
Reference in New Issue
Block a user