Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee30b14cba | ||
|
|
80c00149d2 | ||
|
|
a2a48771e1 | ||
|
|
7bd6cf2466 | ||
|
|
ac9247fecb | ||
|
|
8439a1d100 | ||
|
|
b709245b46 | ||
|
|
52e30336ad | ||
|
|
bee884c52f | ||
|
|
2ffa63d583 | ||
|
|
a0d08f8f88 | ||
|
|
09cc934fbd | ||
|
|
5a0ed5d36d | ||
|
|
dbbe188adc | ||
|
|
91bec23c48 | ||
|
|
90815f361a | ||
|
|
4f0abfed09 | ||
|
|
ad28e4acc5 |
@@ -40,7 +40,14 @@ Options:
|
||||
| `confirmationMessage` (optional) | Text to show to ask for agreement to continue | `Waiting for a facilitator to join the chat...` |
|
||||
| `exitMessage` (optional) | Text to show if the user rejects the Terms of Use. | `The chat is closed. You may close this window.` |
|
||||
| `anonymousDisplayName` (optional) | The display name for the chat user. | `Anonymous` |
|
||||
| `chatUnavailableMessage` (optional) | Text to show if no-one is available to respond | `The chat service is not available right now. Please try again later.` |
|
||||
| `chatUnavailableMessage` (optional) | Text to show on error or if the service is otherwise unavailable | `The chat service is not available right now. Please try again later.` |
|
||||
| `waitMessage` (optional) | Text to show if there is at least one facilitator online but they do not respond right away | `Please be patient, our online facilitators are currently responding to other support requests.` |
|
||||
| `chatOfflineMessage` (optional) | Text to show if there is no-one online respond | `All of the chat facilitators are currently offline.` |
|
||||
| `size` (optional) | The size of the start button. Can be 'small' or 'large' | `large` |
|
||||
| `position` (optional) | The position of the start button. Can be 'top left', 'top right', 'bottom left', 'bottom right'. | `bottom right` |
|
||||
| `maxWaitTime` (optional) | The maximum time (in ms) the chatbox will wait for someone to join before closing the chat and displaying the chat unavailable message | 600000 |
|
||||
| `waitInterval` (optional) | The interval (in ms) at which the bot sends the wait message | 120000 |
|
||||
|
||||
|
||||
## Feature list
|
||||
|
||||
|
||||
@@ -24,7 +24,13 @@ export const mockInitCrypto = jest.fn()
|
||||
export const mockStartClient = jest.fn(() => {
|
||||
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(() => {
|
||||
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',
|
||||
position: 'bottom right',
|
||||
size: 'large',
|
||||
maxWaitTime: 6000*3,
|
||||
maxWaitTime: 1000*60*3, // 3 minutes
|
||||
}
|
||||
|
||||
EmbeddableChatbox.mount(config);
|
||||
|
||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "private-safesupport-chatbox",
|
||||
"version": "1.2.1",
|
||||
"version": "2.1.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "private-safesupport-chatbox",
|
||||
"version": "1.2.1",
|
||||
"version": "2.1.2",
|
||||
"description": "A secure and private embeddable chatbox that connects to Riot",
|
||||
"main": "dist/chatbox.js",
|
||||
"scripts": {
|
||||
@@ -118,6 +118,8 @@
|
||||
"node-sass": "^4.13.1",
|
||||
"postcss-increase-specificity": "0.6.0",
|
||||
"postcss-loader": "3.0.0",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"sass-loader": "8.0.0",
|
||||
"style-loader": "1.1.2",
|
||||
"wait-for-expect": "^3.0.2",
|
||||
@@ -137,11 +139,13 @@
|
||||
"node-localstorage": "^2.1.5",
|
||||
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
||||
"prop-types": "^15.6.2",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"react-onclickoutside": "^6.9.0",
|
||||
"react-test-renderer": "^16.13.0",
|
||||
"react-transition-group": "^4.0.0",
|
||||
"uuidv4": "^6.0.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
anonymousDisplayName: 'Anonymous',
|
||||
position: 'bottom right',
|
||||
size: 'large',
|
||||
maxWaitTime: 6000*3,
|
||||
maxWaitTime: 1000*60*3, // 3 minutes
|
||||
}
|
||||
|
||||
EmbeddableChatbox.mount(config);
|
||||
|
||||
@@ -43,7 +43,7 @@ const DEFAULT_POSITION = 'bottom right'
|
||||
const DEFAULT_SIZE = 'large'
|
||||
const DEFAULT_MAX_WAIT_MS = 600000 // 10 minutes
|
||||
const DEFAULT_WAIT_INTERVAL_MS = 120000 // 2 minutes
|
||||
|
||||
const DEFAULT_DOCK_LABEL = 'Start a new chat'
|
||||
|
||||
class ChatBox extends React.Component {
|
||||
constructor(props) {
|
||||
@@ -166,10 +166,13 @@ class ChatBox extends React.Component {
|
||||
await this.state.client.deactivateAccount(auth, true)
|
||||
await this.state.client.stopClient()
|
||||
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()
|
||||
}
|
||||
|
||||
if (resetState) {
|
||||
this.setState(this.initialState)
|
||||
@@ -232,12 +235,6 @@ class ChatBox extends React.Component {
|
||||
this.setState({ ready: false })
|
||||
|
||||
const client = await this.createClientWithAccount()
|
||||
this.setState({
|
||||
client: client
|
||||
})
|
||||
client.setDisplayName(this.props.anonymousDisplayName)
|
||||
this.setMatrixListeners(client)
|
||||
|
||||
try {
|
||||
await client.initCrypto()
|
||||
} catch(err) {
|
||||
@@ -245,8 +242,16 @@ class ChatBox extends React.Component {
|
||||
}
|
||||
|
||||
await client.startClient()
|
||||
|
||||
client.once('sync', async (state, prevState, data) => {
|
||||
if (state === "PREPARED") {
|
||||
this.setState({ client })
|
||||
client.setDisplayName(this.props.anonymousDisplayName)
|
||||
this.setMatrixListeners(client)
|
||||
await this.createRoom(client)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
restartWithoutCrypto = async () => {
|
||||
if (this.state.client) {
|
||||
@@ -277,35 +282,35 @@ class ChatBox extends React.Component {
|
||||
|
||||
let client;
|
||||
client = matrix.createClient(opts)
|
||||
this.setState({
|
||||
client: client,
|
||||
})
|
||||
|
||||
try {
|
||||
this.setMatrixListeners(client)
|
||||
client.setDisplayName(this.props.anonymousDisplayName)
|
||||
await this.createRoom(client)
|
||||
await client.startClient()
|
||||
|
||||
client.once('sync', async (state, prevState, data) => {
|
||||
if (state === "PREPARED") {
|
||||
try {
|
||||
this.setState({ client })
|
||||
client.setDisplayName(this.props.anonymousDisplayName)
|
||||
this.setMatrixListeners(client)
|
||||
await this.createRoom(client)
|
||||
this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
|
||||
} catch(err) {
|
||||
console.log("error", err)
|
||||
this.handleInitError(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
initializeUnencryptedChat = async () => {
|
||||
this.setState({ ready: false })
|
||||
|
||||
const client = await this.createClientWithAccount()
|
||||
this.setState({
|
||||
client: client
|
||||
})
|
||||
await client.startClient()
|
||||
|
||||
client.once('sync', async (state, prevState, data) => {
|
||||
client.setDisplayName(this.props.anonymousDisplayName)
|
||||
this.setMatrixListeners(client)
|
||||
|
||||
await client.startClient()
|
||||
await this.createRoom(client)
|
||||
})
|
||||
}
|
||||
|
||||
handleInitError = (err) => {
|
||||
@@ -456,40 +461,15 @@ class ChatBox extends React.Component {
|
||||
const decryptionErrors = {...this.state.decryptionErrors}
|
||||
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({
|
||||
messages: {
|
||||
...this.state.messages,
|
||||
[message.id]: newMessage,
|
||||
[message.id]: message,
|
||||
},
|
||||
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) => {
|
||||
switch (e.keyCode) {
|
||||
case 27:
|
||||
@@ -504,6 +484,21 @@ class ChatBox extends React.Component {
|
||||
}
|
||||
|
||||
setMatrixListeners = client => {
|
||||
client.on("sync", (state, prevState, data) => {
|
||||
switch (state) {
|
||||
case "ERROR":
|
||||
// update UI to say "Connection Lost"
|
||||
break;
|
||||
case "SYNCING":
|
||||
// update UI to remove any "Connection Lost" message
|
||||
break;
|
||||
case "PREPARED":
|
||||
// the client instance is ready to be queried.
|
||||
this.setState({ client: client })
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
client.on("Room.timeline", (event, room) => {
|
||||
const eventType = event.getType()
|
||||
const content = event.getContent()
|
||||
@@ -529,7 +524,6 @@ class ChatBox extends React.Component {
|
||||
this.verifyAllRoomDevices(client, room)
|
||||
this.setState({ facilitatorId: sender, ready: true })
|
||||
window.clearInterval(this.state.waitIntervalId)
|
||||
window.clearInterval(this.state.waitTimeoutId)
|
||||
}
|
||||
});
|
||||
|
||||
@@ -551,6 +545,26 @@ class ChatBox extends React.Component {
|
||||
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) {
|
||||
@@ -607,14 +621,7 @@ class ChatBox extends React.Component {
|
||||
}
|
||||
}, this.props.waitInterval)
|
||||
|
||||
const waitTimeoutId = window.setTimeout(() => {
|
||||
if (!this.state.facilitatorId && !this.state.ready) {
|
||||
this.displayBotMessage({ body: this.props.chatUnavailableMessage })
|
||||
this.handleChatOffline()
|
||||
}
|
||||
}, this.props.maxWaitTime)
|
||||
|
||||
this.setState({ waitIntervalId, waitTimeoutId })
|
||||
this.setState({ waitIntervalId })
|
||||
}
|
||||
|
||||
handleRejectTerms = () => {
|
||||
@@ -749,7 +756,7 @@ class ChatBox extends React.Component {
|
||||
)}
|
||||
}
|
||||
</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} />}
|
||||
</div>
|
||||
</div>
|
||||
@@ -775,6 +782,7 @@ ChatBox.propTypes = {
|
||||
size: PropTypes.oneOf(['small', 'large']),
|
||||
maxWaitTime: PropTypes.number,
|
||||
waitInterval: PropTypes.number,
|
||||
dockLabel: PropTypes.string,
|
||||
}
|
||||
|
||||
ChatBox.defaultProps = {
|
||||
@@ -795,6 +803,7 @@ ChatBox.defaultProps = {
|
||||
size: DEFAULT_SIZE,
|
||||
maxWaitTime: DEFAULT_MAX_WAIT_MS,
|
||||
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
|
||||
dockLabel: DEFAULT_DOCK_LABEL,
|
||||
}
|
||||
|
||||
export default ChatBox;
|
||||
|
||||
@@ -115,9 +115,7 @@ describe('Chatbox', () => {
|
||||
expect(createClient).toHaveBeenCalled()
|
||||
expect(mockInitCrypto).toHaveBeenCalled()
|
||||
expect(mockStartClient).toHaveBeenCalled()
|
||||
expect(mockCreateRoom).toHaveBeenCalled()
|
||||
expect(mockSetPowerLevel).toHaveBeenCalled()
|
||||
expect(mockOn).toHaveBeenCalled()
|
||||
expect(mockOnce).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('rejecting terms should not start chat', async () => {
|
||||
@@ -146,6 +144,10 @@ describe('Chatbox', () => {
|
||||
|
||||
acceptButton.simulate('click')
|
||||
|
||||
await waitForExpect(() => {
|
||||
expect(mockOnce).toHaveBeenCalled()
|
||||
});
|
||||
|
||||
await waitForExpect(() => {
|
||||
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 dock = chatbox.find('button.dock')
|
||||
const instance = chatbox.instance()
|
||||
@@ -183,25 +185,25 @@ describe('Chatbox', () => {
|
||||
acceptButton.simulate('click')
|
||||
|
||||
await waitForExpect(() => {
|
||||
expect(mockCreateRoom).toHaveBeenCalled()
|
||||
expect(mockOnce).toHaveBeenCalled()
|
||||
});
|
||||
|
||||
jest.spyOn(instance, 'initializeUnencryptedChat')
|
||||
instance.handleDecryptionError()
|
||||
jest.spyOn(instance, 'handleMessageEvent')
|
||||
|
||||
instance.handleDecryptionError({
|
||||
getId: () => 'test_event_id',
|
||||
getType: () => 'm.message',
|
||||
getSender: () => 'sender',
|
||||
getRoomId: () => 'room id',
|
||||
getContent: () => ({ body: 'test msg' }),
|
||||
getTs: () => '123',
|
||||
})
|
||||
|
||||
await waitForExpect(() => {
|
||||
expect(mockLeave).toHaveBeenCalled()
|
||||
});
|
||||
expect(instance.handleMessageEvent).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
await waitForExpect(() => {
|
||||
expect(mockStopClient).toHaveBeenCalled()
|
||||
});
|
||||
|
||||
await waitForExpect(() => {
|
||||
expect(mockClearStores).toHaveBeenCalled()
|
||||
});
|
||||
|
||||
expect(instance.initializeUnencryptedChat).toHaveBeenCalled()
|
||||
expect(chatbox.state().decryptionErrors).toEqual({ 'test_event_id': true })
|
||||
})
|
||||
|
||||
test('creating an unencrypted chat', async () => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { Fragment } from "react"
|
||||
import PropTypes from "prop-types"
|
||||
|
||||
const Dock = ({ handleToggleOpen, size }) => {
|
||||
const Dock = ({ handleToggleOpen, size, label }) => {
|
||||
return(
|
||||
<button
|
||||
type="button"
|
||||
@@ -13,11 +13,11 @@ const Dock = ({ handleToggleOpen, size }) => {
|
||||
{
|
||||
size === 'small' ?
|
||||
<div id="open-chatbox-label">
|
||||
<span>Chat</span><span className="icon">+</span>
|
||||
<span>{label}</span><span className="icon">+</span>
|
||||
</div>
|
||||
:
|
||||
<Fragment>
|
||||
<div id="open-chatbox-label">Start a new chat</div>
|
||||
<div id="open-chatbox-label">{label}</div>
|
||||
<div className="label-icon">
|
||||
<div className={`btn-icon`} aria-label={`Open support chat window`}>+</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: '/',
|
||||
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