|
|
|
|
@@ -25,7 +25,7 @@ const ENCRYPTION_NOTICE = "Messages in this chat are secured with end-to-end enc
|
|
|
|
|
const UNENCRYPTION_NOTICE = "Messages in this chat are not encrypted."
|
|
|
|
|
const RESTARTING_UNENCRYPTED_CHAT_MESSAGE = "Restarting chat without encryption."
|
|
|
|
|
const WAIT_TIME_MS = 120000 // 2 minutes
|
|
|
|
|
const CHAT_IS_OFFLINE_NOTICE = "Chat is offline"
|
|
|
|
|
const CHAT_IS_OFFLINE_NOTICE = "CHAT_OFFLINE"
|
|
|
|
|
|
|
|
|
|
const DEFAULT_MATRIX_SERVER = "https://matrix.rhok.space/"
|
|
|
|
|
const DEFAULT_BOT_ID = "@help-bot:rhok.space"
|
|
|
|
|
@@ -37,7 +37,9 @@ const DEFAULT_CONFIRMATION_MESSAGE = "Waiting for a facilitator to join the chat
|
|
|
|
|
const DEFAULT_EXIT_MESSAGE = "The chat is closed. You may close this window."
|
|
|
|
|
const DEFAULT_ANONYMOUS_DISPLAY_NAME="Anonymous"
|
|
|
|
|
const DEFAULT_CHAT_UNAVAILABLE_MESSAGE = "The chat service is not available right now. Please try again later."
|
|
|
|
|
const DEFAULT_CHAT_OFFLINE_MESSAGE = "There are no facilitators currently available. For immediate service, please call 123-456-7890."
|
|
|
|
|
const DEFAULT_WAIT_MESSAGE = "Please be patient, our online facilitators are currently responding to other support requests."
|
|
|
|
|
const DEFAULT_ENCRYPTION_DISABLED = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChatBox extends React.Component {
|
|
|
|
|
@@ -52,7 +54,7 @@ class ChatBox extends React.Component {
|
|
|
|
|
userId: null,
|
|
|
|
|
password: null,
|
|
|
|
|
localStorage: null,
|
|
|
|
|
messages: [],
|
|
|
|
|
messages: {},
|
|
|
|
|
inputValue: "",
|
|
|
|
|
errors: [],
|
|
|
|
|
roomId: null,
|
|
|
|
|
@@ -144,27 +146,31 @@ class ChatBox extends React.Component {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exitChat = async () => {
|
|
|
|
|
if (!this.state.client) return null;
|
|
|
|
|
exitChat = async (resetState=true) => {
|
|
|
|
|
if (this.state.client) {
|
|
|
|
|
await this.state.client.leave(this.state.roomId)
|
|
|
|
|
|
|
|
|
|
await this.state.client.leave(this.state.roomId)
|
|
|
|
|
const auth = {
|
|
|
|
|
type: 'm.login.password',
|
|
|
|
|
user: this.state.userId,
|
|
|
|
|
identifier: {
|
|
|
|
|
type: "m.id.user",
|
|
|
|
|
user: this.state.userId,
|
|
|
|
|
},
|
|
|
|
|
password: this.state.password,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auth = {
|
|
|
|
|
type: 'm.login.password',
|
|
|
|
|
user: this.state.userId,
|
|
|
|
|
identifier: {
|
|
|
|
|
type: "m.id.user",
|
|
|
|
|
user: this.state.userId,
|
|
|
|
|
},
|
|
|
|
|
password: this.state.password,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await this.state.client.deactivateAccount(auth, true)
|
|
|
|
|
await this.state.client.stopClient()
|
|
|
|
|
await this.state.client.clearStores()
|
|
|
|
|
await this.state.client.deactivateAccount(auth, true)
|
|
|
|
|
await this.state.client.stopClient()
|
|
|
|
|
await this.state.client.clearStores()
|
|
|
|
|
this.setState({ client: null })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.state.localStorage.clear()
|
|
|
|
|
this.setState(this.initialState)
|
|
|
|
|
|
|
|
|
|
if (resetState) {
|
|
|
|
|
this.setState(this.initialState)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createLocalStorage = async (deviceId, sessionId) => {
|
|
|
|
|
@@ -232,14 +238,14 @@ class ChatBox extends React.Component {
|
|
|
|
|
try {
|
|
|
|
|
await client.initCrypto()
|
|
|
|
|
} catch(err) {
|
|
|
|
|
return this.initializeUnencryptedChat()
|
|
|
|
|
return this.restartWithoutCrypto()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await client.startClient()
|
|
|
|
|
await this.createRoom(client)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initializeUnencryptedChat = async () => {
|
|
|
|
|
restartWithoutCrypto = async () => {
|
|
|
|
|
if (this.state.client) {
|
|
|
|
|
this.state.client.leave(this.state.roomId)
|
|
|
|
|
this.state.client.stopClient()
|
|
|
|
|
@@ -282,7 +288,21 @@ class ChatBox extends React.Component {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleInitError = (err) => {
|
|
|
|
|
@@ -292,17 +312,8 @@ class ChatBox extends React.Component {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleDecryptionError = async (event, err) => {
|
|
|
|
|
if (this.state.client) {
|
|
|
|
|
const isCryptoEnabled = await this.state.client.isCryptoEnabled()
|
|
|
|
|
const isRoomEncrypted = this.state.client.isRoomEncrypted(this.state.roomId)
|
|
|
|
|
|
|
|
|
|
if (!isCryptoEnabled || !isRoomEncrypted) {
|
|
|
|
|
return this.initializeUnencryptedChat()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const eventId = event.getId()
|
|
|
|
|
this.displayFakeMessage({ body: '** Unable to decrypt message **' }, event.getSender(), eventId)
|
|
|
|
|
this.handleMessageEvent(event)
|
|
|
|
|
this.setState({ decryptionErrors: { [eventId]: true }})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -376,31 +387,39 @@ class ChatBox extends React.Component {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayFakeMessage = (content, sender, messageId=uuid()) => {
|
|
|
|
|
const msgList = [...this.state.messages]
|
|
|
|
|
const msg = {
|
|
|
|
|
id: messageId,
|
|
|
|
|
type: 'm.room.message',
|
|
|
|
|
sender: sender,
|
|
|
|
|
roomId: this.state.roomId,
|
|
|
|
|
content: content,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
}
|
|
|
|
|
msgList.push(msg)
|
|
|
|
|
|
|
|
|
|
this.setState({ messages: msgList })
|
|
|
|
|
this.setState({
|
|
|
|
|
messages: {
|
|
|
|
|
...this.state.messages,
|
|
|
|
|
[messageId]: msg
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayBotMessage = (content, roomId) => {
|
|
|
|
|
const msgList = [...this.state.messages]
|
|
|
|
|
displayBotMessage = (content, roomId, messageId=uuid()) => {
|
|
|
|
|
const msg = {
|
|
|
|
|
id: uuid(),
|
|
|
|
|
id: messageId,
|
|
|
|
|
type: 'm.room.message',
|
|
|
|
|
sender: this.props.botId,
|
|
|
|
|
roomId: roomId || this.state.roomId,
|
|
|
|
|
content: content,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
}
|
|
|
|
|
msgList.push(msg)
|
|
|
|
|
|
|
|
|
|
this.setState({ messages: msgList })
|
|
|
|
|
this.setState({
|
|
|
|
|
messages: {
|
|
|
|
|
...this.state.messages,
|
|
|
|
|
[messageId]: msg
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleMessageEvent = event => {
|
|
|
|
|
@@ -410,8 +429,12 @@ class ChatBox extends React.Component {
|
|
|
|
|
sender: event.getSender(),
|
|
|
|
|
roomId: event.getRoomId(),
|
|
|
|
|
content: event.getContent(),
|
|
|
|
|
timestamp: event.getTs(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// there's also event.getClearContent() which only works on encrypted messages
|
|
|
|
|
// but not really sure when it should be used vs event.getContent()
|
|
|
|
|
|
|
|
|
|
if (message.content.showToUser && message.content.showToUser !== this.state.userId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@@ -427,22 +450,41 @@ class ChatBox extends React.Component {
|
|
|
|
|
this.setState({ messagesInFlight })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for decryption error message and replace with decrypted message
|
|
|
|
|
// or push message to messages array
|
|
|
|
|
const messages = [...this.state.messages]
|
|
|
|
|
const decryptionErrors = {...this.state.decryptionErrors}
|
|
|
|
|
delete decryptionErrors[message.id]
|
|
|
|
|
const existingMessageIndex = messages.findIndex(({ id }) => id === message.id)
|
|
|
|
|
|
|
|
|
|
if (existingMessageIndex > -1) {
|
|
|
|
|
messages.splice(existingMessageIndex, 1, message)
|
|
|
|
|
} else {
|
|
|
|
|
messages.push(message)
|
|
|
|
|
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(event.getRoomId())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setState({ messages, decryptionErrors })
|
|
|
|
|
this.setState({
|
|
|
|
|
messages: {
|
|
|
|
|
...this.state.messages,
|
|
|
|
|
[message.id]: newMessage,
|
|
|
|
|
},
|
|
|
|
|
decryptionErrors
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleChatOffline = (roomId) => {
|
|
|
|
|
this.exitChat(false) // close the chat connection but keep chatbox state
|
|
|
|
|
window.clearInterval(this.state.timeoutId) // no more waiting messages
|
|
|
|
|
this.setState({ ready: true }) // no more loading animation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleKeyDown = (e) => {
|
|
|
|
|
switch (e.keyCode) {
|
|
|
|
|
@@ -492,12 +534,6 @@ class ChatBox extends React.Component {
|
|
|
|
|
return this.handleDecryptionError(event, err)
|
|
|
|
|
}
|
|
|
|
|
if (event.getType() === "m.room.message") {
|
|
|
|
|
const content = event.getContent()
|
|
|
|
|
|
|
|
|
|
if (content.msgtype === "m.notice" && content.body === CHAT_IS_OFFLINE_NOTICE) {
|
|
|
|
|
this.setState({ ready: true })
|
|
|
|
|
return window.clearInterval(this.state.timeoutId)
|
|
|
|
|
}
|
|
|
|
|
this.handleMessageEvent(event)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
@@ -513,7 +549,7 @@ class ChatBox extends React.Component {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
|
if (prevState.messages.length !== this.state.messages.length) {
|
|
|
|
|
if ((prevState.messages !== this.state.messages) || (prevState.messagesInFlight !== this.state.messagesInFlight) || (prevState.typingStatus !== this.state.typingStatus)) {
|
|
|
|
|
if (this.messageWindow.current.scrollTo) {
|
|
|
|
|
this.messageWindow.current.scrollTo(0, this.messageWindow.current.scrollHeight)
|
|
|
|
|
}
|
|
|
|
|
@@ -549,7 +585,11 @@ class ChatBox extends React.Component {
|
|
|
|
|
this.setState({ awaitingAgreement: false })
|
|
|
|
|
this.startWaitTimeForFacilitator()
|
|
|
|
|
try {
|
|
|
|
|
this.initializeChat()
|
|
|
|
|
if (this.props.isEncryptionDisabled) {
|
|
|
|
|
this.initializeUnencryptedChat()
|
|
|
|
|
} else {
|
|
|
|
|
this.initializeChat()
|
|
|
|
|
}
|
|
|
|
|
} catch(err) {
|
|
|
|
|
this.handleInitError(err)
|
|
|
|
|
}
|
|
|
|
|
@@ -557,7 +597,7 @@ class ChatBox extends React.Component {
|
|
|
|
|
|
|
|
|
|
startWaitTimeForFacilitator = () => {
|
|
|
|
|
const timeoutId = window.setInterval(() => {
|
|
|
|
|
if (!this.state.facilitatorId) {
|
|
|
|
|
if (!this.state.facilitatorId && !this.state.ready) {
|
|
|
|
|
this.displayBotMessage({ body: this.props.waitMessage })
|
|
|
|
|
}
|
|
|
|
|
}, WAIT_TIME_MS)
|
|
|
|
|
@@ -575,7 +615,7 @@ class ChatBox extends React.Component {
|
|
|
|
|
const message = this.state.inputValue
|
|
|
|
|
if (!Boolean(message)) return null;
|
|
|
|
|
|
|
|
|
|
if (this.state.isCryptoEnabled && !(this.state.client.isRoomEncrypted(this.state.roomId) && this.state.client.isCryptoEnabled())) return null;
|
|
|
|
|
if (this.state.isCryptoEnabled && this.state.client && !(this.state.client.isRoomEncrypted(this.state.roomId) && this.state.client.isCryptoEnabled())) return null;
|
|
|
|
|
|
|
|
|
|
if (this.state.client && this.state.roomId) {
|
|
|
|
|
const messagesInFlight = [...this.state.messagesInFlight]
|
|
|
|
|
@@ -596,6 +636,7 @@ class ChatBox extends React.Component {
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { ready, messages, messagesInFlight, inputValue, userId, roomId, typingStatus, opened, showDock, emojiSelectorOpen, isMobile, decryptionErrors } = this.state;
|
|
|
|
|
const orderedMessages = Object.values(messages).sort((a,b) => a.timestamp - b.timestamp)
|
|
|
|
|
const inputLabel = 'Send a message...'
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
@@ -631,7 +672,7 @@ class ChatBox extends React.Component {
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
messages.map((message, index) => {
|
|
|
|
|
orderedMessages.map((message, index) => {
|
|
|
|
|
return(
|
|
|
|
|
<Message key={message.id} message={message} userId={userId} botId={this.props.botId} client={this.state.client} />
|
|
|
|
|
)
|
|
|
|
|
@@ -656,7 +697,7 @@ class ChatBox extends React.Component {
|
|
|
|
|
<div className={`message from-bot`}>
|
|
|
|
|
<div className="text buttons">
|
|
|
|
|
{`Restart chat without encryption?`}
|
|
|
|
|
<button className="btn" id="accept" onClick={this.initializeUnencryptedChat}>RESTART</button>
|
|
|
|
|
<button className="btn" id="accept" onClick={this.restartWithoutCrypto}>RESTART</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
@@ -715,6 +756,8 @@ ChatBox.propTypes = {
|
|
|
|
|
chatUnavailableMessage: PropTypes.string,
|
|
|
|
|
anonymousDisplayName: PropTypes.string,
|
|
|
|
|
waitMessage: PropTypes.string,
|
|
|
|
|
chatOfflineMessage: PropTypes.string,
|
|
|
|
|
isEncryptionDisabled: PropTypes.bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChatBox.defaultProps = {
|
|
|
|
|
@@ -729,6 +772,8 @@ ChatBox.defaultProps = {
|
|
|
|
|
anonymousDisplayName: DEFAULT_ANONYMOUS_DISPLAY_NAME,
|
|
|
|
|
chatUnavailableMessage: DEFAULT_CHAT_UNAVAILABLE_MESSAGE,
|
|
|
|
|
waitMessage: DEFAULT_WAIT_MESSAGE,
|
|
|
|
|
chatOfflineMessage: DEFAULT_CHAT_OFFLINE_MESSAGE,
|
|
|
|
|
isEncryptionDisabled: DEFAULT_ENCRYPTION_DISABLED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ChatBox;
|
|
|
|
|
|