Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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...` |
|
| `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
|
||||||
|
|
||||||
|
|||||||
@@ -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');
|
||||||
});
|
});
|
||||||
|
|||||||
10
dist/bookmarklet.js
vendored
10
dist/bookmarklet.js
vendored
File diff suppressed because one or more lines are too long
10
dist/chatbox.js
vendored
10
dist/chatbox.js
vendored
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.1",
|
"version": "2.0.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "private-safesupport-chatbox",
|
"name": "private-safesupport-chatbox",
|
||||||
"version": "1.2.1",
|
"version": "2.0.0",
|
||||||
"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": {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -167,9 +167,12 @@ class ChatBox extends React.Component {
|
|||||||
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 })
|
||||||
|
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) => {
|
||||||
@@ -485,8 +490,6 @@ class ChatBox extends React.Component {
|
|||||||
|
|
||||||
handleChatOffline = () => {
|
handleChatOffline = () => {
|
||||||
this.exitChat(false) // close the chat connection but keep chatbox state
|
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
|
this.setState({ ready: true }) // no more loading animation
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,6 +507,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 +547,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 +568,23 @@ 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps, prevState) {
|
componentDidUpdate(prevProps, prevState) {
|
||||||
@@ -607,14 +641,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 = () => {
|
||||||
|
|||||||
@@ -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/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() });
|
||||||
Reference in New Issue
Block a user