16 Commits

Author SHA1 Message Date
Sharon Kennedy
a2a48771e1 2.1.1 2020-10-15 12:25:08 -04:00
Sharon Kennedy
7bd6cf2466 package component 2020-10-15 12:24:59 -04:00
Sharon Kennedy
ac9247fecb 2.1.0 2020-10-15 11:40:38 -04:00
Sharon Kennedy
8439a1d100 export chatbox as react component 2020-10-15 11:40:12 -04:00
Sharon Kennedy
b709245b46 2.0.2 2020-09-11 16:32:33 -04:00
Sharon Kennedy
52e30336ad handle chat offline signal 2020-09-11 16:32:20 -04:00
Sharon Kennedy
bee884c52f 2.0.1 2020-09-08 08:20:29 -04:00
Sharon Kennedy
2ffa63d583 remove loader when chat is closed 2020-09-08 08:20:23 -04:00
Sharon Kennedy
a0d08f8f88 2.0.0 2020-09-06 15:34:45 -04:00
Sharon Kennedy
09cc934fbd latest build 2020-09-06 15:34:31 -04:00
Sharon
5a0ed5d36d Merge pull request #1 from nomadic-labs/bot_signal
Bot signal
2020-09-06 15:25:31 -04:00
Sharon Kennedy
dbbe188adc update tests 2020-09-06 15:24:41 -04:00
Sharon Kennedy
91bec23c48 handle bot signal 2020-09-06 14:13:04 -04:00
Sharon Kennedy
90815f361a 1.2.2 2020-07-01 00:50:53 -04:00
Sharon Kennedy
4f0abfed09 wait for initial sync before adding listeners 2020-07-01 00:50:34 -04:00
Sharon Kennedy
ad28e4acc5 update readme with new configs 2020-06-27 11:42:25 -04:00
14 changed files with 239 additions and 114 deletions

View File

@@ -40,7 +40,14 @@ Options:
| `confirmationMessage` (optional) | Text to show to ask for agreement to continue | `Waiting for a facilitator to join the chat...` | | `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.` | | `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` | | `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 ## Feature list

View File

@@ -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

File diff suppressed because one or more lines are too long

38
dist/chatbox.js vendored

File diff suppressed because one or more lines are too long

36
dist/component.js vendored Normal file

File diff suppressed because one or more lines are too long

2
dist/index.html vendored
View File

@@ -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
View File

@@ -1,6 +1,6 @@
{ {
"name": "private-safesupport-chatbox", "name": "private-safesupport-chatbox",
"version": "1.2.1", "version": "2.1.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,8 +1,8 @@
{ {
"name": "private-safesupport-chatbox", "name": "private-safesupport-chatbox",
"version": "1.2.1", "version": "2.1.1",
"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/component.js",
"scripts": { "scripts": {
"build": "NODE_ENV=production webpack-cli --mode production", "build": "NODE_ENV=production webpack-cli --mode production",
"build:profile": "webpack --mode production --config webpack.config.profile.js", "build:profile": "webpack --mode production --config webpack.config.profile.js",
@@ -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"
} }
} }

View File

@@ -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);

View File

@@ -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
} }
this.state.localStorage.clear() if (this.state.localStorage) {
this.state.localStorage.clear()
}
if (resetState) { if (resetState) {
this.setState(this.initialState) this.setState(this.initialState)
@@ -232,12 +235,6 @@ class ChatBox extends React.Component {
this.setState({ ready: false }) this.setState({ ready: false })
const client = await this.createClientWithAccount() const client = await this.createClientWithAccount()
this.setState({
client: client
})
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
try { try {
await client.initCrypto() await client.initCrypto()
} catch(err) { } catch(err) {
@@ -245,7 +242,15 @@ class ChatBox extends React.Component {
} }
await client.startClient() await client.startClient()
await this.createRoom(client)
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 () => { restartWithoutCrypto = async () => {
@@ -277,35 +282,35 @@ class ChatBox extends React.Component {
let client; let client;
client = matrix.createClient(opts) client = matrix.createClient(opts)
this.setState({ await client.startClient()
client: client,
})
try { client.once('sync', async (state, prevState, data) => {
this.setMatrixListeners(client) if (state === "PREPARED") {
client.setDisplayName(this.props.anonymousDisplayName) try {
await this.createRoom(client) this.setState({ client })
await client.startClient() client.setDisplayName(this.props.anonymousDisplayName)
this.displayBotMessage({ body: UNENCRYPTION_NOTICE }) this.setMatrixListeners(client)
} catch(err) { await this.createRoom(client)
console.log("error", err) this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
this.handleInitError(err) } catch(err) {
} console.log("error", err)
this.handleInitError(err)
}
}
})
} }
initializeUnencryptedChat = async () => { initializeUnencryptedChat = async () => {
this.setState({ ready: false }) this.setState({ ready: false })
const client = await this.createClientWithAccount() const client = await this.createClientWithAccount()
this.setState({
client: client
})
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
await client.startClient() await client.startClient()
await this.createRoom(client)
client.once('sync', async (state, prevState, data) => {
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
await this.createRoom(client)
})
} }
handleInitError = (err) => { handleInitError = (err) => {
@@ -456,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:
@@ -504,6 +484,21 @@ class ChatBox extends React.Component {
} }
setMatrixListeners = client => { 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) => { client.on("Room.timeline", (event, room) => {
const eventType = event.getType() const eventType = event.getType()
const content = event.getContent() const content = event.getContent()
@@ -529,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)
} }
}); });
@@ -551,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) {
@@ -607,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 = () => {

View File

@@ -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 () => {

3
src/outputs/component.js Normal file
View File

@@ -0,0 +1,3 @@
import Chatbox from '../components/chatbox';
export default Chatbox;

3
src/setupTests.js Normal file
View File

@@ -0,0 +1,3 @@
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });

View File

@@ -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"
}
}
}]; }];