8 Commits

Author SHA1 Message Date
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
11 changed files with 119 additions and 74 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...` |
| `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

View File

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

10
dist/bookmarklet.js vendored

File diff suppressed because one or more lines are too long

10
dist/chatbox.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.html vendored
View File

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

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

View File

@@ -1,6 +1,6 @@
{
"name": "private-safesupport-chatbox",
"version": "1.2.1",
"version": "2.0.0",
"description": "A secure and private embeddable chatbox that connects to Riot",
"main": "dist/chatbox.js",
"scripts": {

View File

@@ -25,7 +25,7 @@
anonymousDisplayName: 'Anonymous',
position: 'bottom right',
size: 'large',
maxWaitTime: 6000*3,
maxWaitTime: 1000*60*3, // 3 minutes
}
EmbeddableChatbox.mount(config);

View File

@@ -167,9 +167,12 @@ class ChatBox extends React.Component {
await this.state.client.stopClient()
await this.state.client.clearStores()
this.setState({ client: null })
window.clearInterval(this.state.waitIntervalId) // no more waiting messages
}
this.state.localStorage.clear()
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,7 +242,15 @@ class ChatBox extends React.Component {
}
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 () => {
@@ -277,35 +282,35 @@ class ChatBox extends React.Component {
let client;
client = matrix.createClient(opts)
this.setState({
client: client,
})
await client.startClient()
try {
this.setMatrixListeners(client)
client.setDisplayName(this.props.anonymousDisplayName)
await this.createRoom(client)
await client.startClient()
this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
} catch(err) {
console.log("error", err)
this.handleInitError(err)
}
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
})
client.setDisplayName(this.props.anonymousDisplayName)
this.setMatrixListeners(client)
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) => {
@@ -485,8 +490,6 @@ class ChatBox extends React.Component {
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
}
@@ -504,6 +507,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 +547,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 +568,23 @@ 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
}
}
componentDidUpdate(prevProps, prevState) {
@@ -607,14 +641,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 = () => {

View File

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

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() });