2020-02-01 05:30:58 +00:00
|
|
|
import React from "react"
|
|
|
|
import PropTypes from "prop-types"
|
2020-02-24 04:12:47 +00:00
|
|
|
import { Transition } from 'react-transition-group';
|
2020-02-02 18:36:11 +00:00
|
|
|
import * as util from "util";
|
|
|
|
import * as os from "os";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import { LocalStorage } from "node-localstorage";
|
2020-02-02 04:18:09 +00:00
|
|
|
import * as olm from "olm"
|
|
|
|
global.Olm = olm
|
|
|
|
|
2020-02-02 18:36:11 +00:00
|
|
|
import * as matrix from "matrix-js-sdk";
|
2020-02-01 05:30:58 +00:00
|
|
|
import {uuid} from "uuidv4"
|
|
|
|
|
|
|
|
import Message from "./message";
|
2020-02-24 04:12:47 +00:00
|
|
|
import Dock from "./dock";
|
|
|
|
import Header from "./header";
|
2020-03-25 23:01:29 +00:00
|
|
|
import EmojiSelector from './emoji-selector';
|
2020-02-24 04:12:47 +00:00
|
|
|
|
|
|
|
import './styles.scss';
|
2020-02-01 05:30:58 +00:00
|
|
|
|
2020-02-02 18:36:11 +00:00
|
|
|
|
2020-02-04 05:43:26 +00:00
|
|
|
const ENCRYPTION_CONFIG = { "algorithm": "m.megolm.v1.aes-sha2" };
|
2020-02-23 21:57:16 +00:00
|
|
|
const ENCRYPTION_NOTICE = "Messages in this chat are secured with end-to-end encryption."
|
2020-03-05 03:05:09 +00:00
|
|
|
const UNENCRYPTION_NOTICE = "End-to-end message encryption is not available on this browser."
|
|
|
|
const RESTARTING_UNENCRYPTED_CHAT_MESSAGE = "Restarting chat without encryption."
|
2020-03-05 05:06:24 +00:00
|
|
|
|
2020-03-12 17:08:57 +00:00
|
|
|
const DEFAULT_MATRIX_SERVER = "https://matrix.rhok.space/"
|
2020-03-22 15:07:41 +00:00
|
|
|
const DEFAULT_BOT_ID = "@help-bot:rhok.space"
|
2020-03-12 17:08:57 +00:00
|
|
|
const DEFAULT_TERMS_URL = "https://tosdr.org/"
|
2020-03-05 05:06:24 +00:00
|
|
|
const DEFAULT_ROOM_NAME = "Support Chat"
|
|
|
|
const DEFAULT_INTRO_MESSAGE = "This chat application does not collect any of your personal data or any data from your use of this service."
|
2020-03-12 17:08:57 +00:00
|
|
|
const DEFAULT_AGREEMENT_MESSAGE = "Do you want to continue?"
|
2020-03-05 05:06:24 +00:00
|
|
|
const DEFAULT_CONFIRMATION_MESSAGE = "Waiting for a facilitator to join the chat..."
|
2020-03-12 17:08:57 +00:00
|
|
|
const DEFAULT_EXIT_MESSAGE = "The chat is closed. You may close this window."
|
2020-03-05 05:06:24 +00:00
|
|
|
const DEFAULT_ANONYMOUS_DISPLAY_NAME="Anonymous"
|
|
|
|
const DEFAULT_CHAT_UNAVAILABLE_MESSAGE = "The chat service is not available right now. Please try again later."
|
|
|
|
|
2020-02-24 04:12:47 +00:00
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
class ChatBox extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-03-05 05:06:24 +00:00
|
|
|
this.initialState = {
|
|
|
|
opened: false,
|
|
|
|
showDock: true,
|
|
|
|
client: null,
|
|
|
|
ready: true,
|
|
|
|
accessToken: null,
|
|
|
|
userId: null,
|
|
|
|
password: null,
|
|
|
|
localStorage: null,
|
2020-03-12 17:08:57 +00:00
|
|
|
messages: [],
|
2020-03-05 05:06:24 +00:00
|
|
|
inputValue: "",
|
|
|
|
errors: [],
|
|
|
|
roomId: null,
|
|
|
|
typingStatus: null,
|
|
|
|
awaitingAgreement: true,
|
2020-03-25 23:01:29 +00:00
|
|
|
emojiSelectorOpen: false,
|
2020-03-05 05:06:24 +00:00
|
|
|
}
|
|
|
|
this.state = this.initialState
|
2020-02-01 05:30:58 +00:00
|
|
|
this.chatboxInput = React.createRef();
|
2020-02-25 22:48:30 +00:00
|
|
|
this.messageWindow = React.createRef();
|
2020-03-12 17:08:57 +00:00
|
|
|
this.termsUrl = React.createRef();
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 04:12:47 +00:00
|
|
|
handleToggleOpen = () => {
|
|
|
|
this.setState((prev) => {
|
|
|
|
let { showDock } = prev;
|
|
|
|
if (!prev.opened) {
|
|
|
|
showDock = false;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
showDock,
|
|
|
|
opened: !prev.opened,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:01:29 +00:00
|
|
|
toggleEmojiSelector = () => {
|
|
|
|
this.setState({ emojiSelectorOpen: !this.state.emojiSelectorOpen })
|
|
|
|
}
|
|
|
|
|
|
|
|
closeEmojiSelector = () => {
|
|
|
|
this.setState({ emojiSelectorOpen: false }, () => this.chatboxInput.current.focus())
|
|
|
|
}
|
|
|
|
|
2020-02-24 04:12:47 +00:00
|
|
|
handleWidgetExit = () => {
|
|
|
|
this.setState({
|
|
|
|
showDock: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:16:43 +00:00
|
|
|
handleWidgetEnter = () => {
|
2020-03-12 17:08:57 +00:00
|
|
|
if (this.state.awaitingAgreement) {
|
|
|
|
this.termsUrl.current.focus()
|
|
|
|
} else {
|
|
|
|
this.chatboxInput.current.focus()
|
|
|
|
}
|
2020-02-24 19:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 04:12:47 +00:00
|
|
|
handleExitChat = () => {
|
2020-02-24 19:16:43 +00:00
|
|
|
if (this.state.client) {
|
2020-02-26 05:12:05 +00:00
|
|
|
this.exitChat()
|
2020-02-24 19:16:43 +00:00
|
|
|
} else {
|
2020-03-05 05:06:24 +00:00
|
|
|
this.setState(this.initialState)
|
2020-02-24 19:16:43 +00:00
|
|
|
}
|
2020-02-24 04:12:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:12:05 +00:00
|
|
|
exitChat = () => {
|
|
|
|
if (!this.state.client) return null;
|
2020-02-24 19:16:43 +00:00
|
|
|
return this.state.client.leave(this.state.roomId)
|
2020-02-26 05:12:05 +00:00
|
|
|
.then(() => {
|
|
|
|
const auth = {
|
|
|
|
type: 'm.login.password',
|
|
|
|
user: this.state.userId,
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: this.state.userId,
|
|
|
|
},
|
|
|
|
password: this.state.password,
|
|
|
|
};
|
|
|
|
this.state.client.deactivateAccount(auth, true)
|
|
|
|
})
|
|
|
|
.then(() => this.state.client.stopClient())
|
|
|
|
.then(() => this.state.client.clearStores())
|
|
|
|
.then(() => {
|
|
|
|
this.state.localStorage.clear()
|
2020-03-05 05:06:24 +00:00
|
|
|
this.setState(this.initialState)
|
2020-02-26 05:12:05 +00:00
|
|
|
})
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:16:43 +00:00
|
|
|
initializeChat = () => {
|
|
|
|
this.setState({ ready: false })
|
2020-03-05 05:06:24 +00:00
|
|
|
let client;
|
|
|
|
|
|
|
|
try {
|
|
|
|
client = matrix.createClient(this.props.matrixServerUrl)
|
2020-03-13 04:05:12 +00:00
|
|
|
} catch(error) {
|
|
|
|
console.log("Error creating client", error)
|
2020-03-05 05:06:24 +00:00
|
|
|
return this.handleInitError()
|
|
|
|
}
|
|
|
|
|
2020-03-13 04:05:12 +00:00
|
|
|
// empty registration request to get session
|
2020-02-05 18:42:34 +00:00
|
|
|
return client.registerRequest({})
|
|
|
|
.then(data => {
|
|
|
|
console.log("Empty registration request to get session", data)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
// actual registration request with randomly generated username and password
|
|
|
|
const username = uuid()
|
|
|
|
const password = uuid()
|
|
|
|
const sessionId = err.data.session
|
|
|
|
client.registerRequest({
|
|
|
|
auth: {session: sessionId, type: "m.login.dummy"},
|
|
|
|
inhibit_login: false,
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
x_show_msisdn: true,
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
|
|
|
|
// use node localStorage if window.localStorage is not available
|
|
|
|
let localStorage = global.localStorage;
|
|
|
|
if (typeof localStorage === "undefined" || localStorage === null) {
|
|
|
|
const deviceDesc = `matrix-chat-${data.device_id}-${sessionId}`
|
|
|
|
const localStoragePath = path.resolve(path.join(os.homedir(), ".local-storage", deviceDesc))
|
|
|
|
localStorage = new LocalStorage(localStoragePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
accessToken: data.access_token,
|
|
|
|
userId: data.user_id,
|
2020-02-26 05:12:05 +00:00
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
localStorage: localStorage,
|
2020-03-05 03:05:09 +00:00
|
|
|
sessionId: sessionId,
|
|
|
|
deviceId: data.device_id,
|
2020-02-05 18:42:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// create new client with full options
|
|
|
|
let opts = {
|
|
|
|
baseUrl: this.props.matrixServerUrl,
|
|
|
|
accessToken: data.access_token,
|
|
|
|
userId: data.user_id,
|
|
|
|
deviceId: data.device_id,
|
|
|
|
sessionStore: new matrix.WebStorageSessionStore(localStorage),
|
|
|
|
}
|
|
|
|
|
|
|
|
client = matrix.createClient(opts)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2020-03-05 05:06:24 +00:00
|
|
|
this.handleInitError()
|
2020-02-05 18:42:34 +00:00
|
|
|
})
|
|
|
|
.then(() => client.initCrypto())
|
2020-03-05 05:06:24 +00:00
|
|
|
.catch(err => this.initializeUnencryptedChat())
|
2020-03-15 04:47:19 +00:00
|
|
|
.then(() => client.setDisplayName(this.props.anonymousDisplayName))
|
2020-02-24 19:16:43 +00:00
|
|
|
.then(() => client.startClient())
|
2020-02-05 18:42:34 +00:00
|
|
|
.then(() => {
|
|
|
|
this.setState({
|
|
|
|
client: client
|
|
|
|
})
|
|
|
|
})
|
2020-03-05 05:06:24 +00:00
|
|
|
.catch(err => this.handleInitError())
|
2020-02-05 18:42:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-05 03:05:09 +00:00
|
|
|
initializeUnencryptedChat = () => {
|
|
|
|
this.setState({ ready: false })
|
|
|
|
|
|
|
|
let opts = {
|
|
|
|
baseUrl: this.props.matrixServerUrl,
|
|
|
|
accessToken: this.state.accessToken,
|
|
|
|
userId: this.state.userId,
|
2020-03-05 05:06:24 +00:00
|
|
|
deviceId: this.state.deviceId,
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 05:06:24 +00:00
|
|
|
let client;
|
|
|
|
try {
|
|
|
|
client = matrix.createClient(opts)
|
2020-03-15 04:47:19 +00:00
|
|
|
client.setDisplayName(this.props.anonymousDisplayName)
|
2020-03-05 05:06:24 +00:00
|
|
|
} catch {
|
|
|
|
return this.handleInitError()
|
|
|
|
}
|
2020-03-05 03:05:09 +00:00
|
|
|
return client.startClient()
|
|
|
|
.then(() => {
|
|
|
|
this.setState({
|
|
|
|
client: client,
|
|
|
|
isCryptoEnabled: false,
|
|
|
|
})
|
|
|
|
})
|
2020-03-05 05:06:24 +00:00
|
|
|
.catch(err => this.handleInitError())
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInitError = () => {
|
|
|
|
this.displayBotMessage({ body: this.props.chatUnavailableMessage })
|
|
|
|
this.setState({ ready: true })
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleDecryptionError = () => {
|
|
|
|
this.displayBotMessage({ body: RESTARTING_UNENCRYPTED_CHAT_MESSAGE })
|
|
|
|
|
|
|
|
this.state.client.leave(this.state.roomId)
|
|
|
|
.then(() => this.state.client.stopClient())
|
|
|
|
.then(() => this.state.client.clearStores())
|
|
|
|
.then(() => this.initializeUnencryptedChat())
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:58 +00:00
|
|
|
verifyAllRoomDevices = async function(roomId) {
|
|
|
|
let room = this.state.client.getRoom(roomId);
|
|
|
|
let members = (await room.getEncryptionTargetMembers()).map(x => x["userId"])
|
|
|
|
let memberkeys = await this.state.client.downloadKeys(members);
|
|
|
|
for (const userId in memberkeys) {
|
|
|
|
for (const deviceId in memberkeys[userId]) {
|
|
|
|
await this.state.client.setDeviceVerified(userId, deviceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 03:05:09 +00:00
|
|
|
createRoom = async function() {
|
2020-02-01 05:30:58 +00:00
|
|
|
const currentDate = new Date()
|
|
|
|
const chatDate = currentDate.toLocaleDateString()
|
|
|
|
const chatTime = currentDate.toLocaleTimeString()
|
2020-03-05 03:05:09 +00:00
|
|
|
let roomConfig = {
|
2020-02-01 05:30:58 +00:00
|
|
|
room_alias_name: `private-support-chat-${uuid()}`,
|
2020-03-22 15:07:41 +00:00
|
|
|
invite: [this.props.botId],
|
2020-02-01 05:30:58 +00:00
|
|
|
visibility: 'private',
|
2020-03-05 05:06:24 +00:00
|
|
|
name: `${chatTime}, ${chatDate} - ${this.props.roomName}`,
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const isCryptoEnabled = await this.state.client.isCryptoEnabled()
|
|
|
|
|
|
|
|
if (isCryptoEnabled) {
|
|
|
|
roomConfig.initial_state = [
|
2020-02-04 05:43:26 +00:00
|
|
|
{
|
|
|
|
type: 'm.room.encryption',
|
|
|
|
state_key: '',
|
2020-02-04 06:13:47 +00:00
|
|
|
content: ENCRYPTION_CONFIG,
|
2020-02-04 05:43:26 +00:00
|
|
|
},
|
|
|
|
]
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const { room_id } = await this.state.client.createRoom(roomConfig)
|
|
|
|
|
2020-03-22 15:07:41 +00:00
|
|
|
this.state.client.setPowerLevel(room_id, this.props.botId, 100)
|
2020-03-05 03:05:09 +00:00
|
|
|
|
|
|
|
if (isCryptoEnabled) {
|
|
|
|
this.verifyAllRoomDevices(room_id)
|
|
|
|
} else {
|
|
|
|
this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
|
|
|
|
}
|
|
|
|
|
2020-03-05 05:06:24 +00:00
|
|
|
this.displayBotMessage({ body: this.props.confirmationMessage })
|
2020-03-05 03:05:09 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
roomId: room_id,
|
|
|
|
isCryptoEnabled
|
2020-02-01 05:30:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-15 04:47:19 +00:00
|
|
|
sendMessage = (message) => {
|
|
|
|
this.state.client.sendTextMessage(this.state.roomId, message)
|
2020-02-05 18:42:34 +00:00
|
|
|
.catch((err) => {
|
2020-02-06 01:26:51 +00:00
|
|
|
switch (err["name"]) {
|
|
|
|
case "UnknownDeviceError":
|
2020-02-05 18:42:34 +00:00
|
|
|
Object.keys(err.devices).forEach((userId) => {
|
|
|
|
Object.keys(err.devices[userId]).map((deviceId) => {
|
|
|
|
this.state.client.setDeviceKnown(userId, deviceId, true);
|
|
|
|
});
|
2020-02-04 05:43:26 +00:00
|
|
|
});
|
2020-03-15 04:47:19 +00:00
|
|
|
this.sendMessage(message)
|
2020-02-05 18:42:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-03-15 04:47:19 +00:00
|
|
|
this.displayBotMessage({ body: "Your message was not sent." })
|
2020-02-05 18:42:34 +00:00
|
|
|
console.log("Error sending message", err);
|
|
|
|
}
|
|
|
|
})
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 03:05:09 +00:00
|
|
|
displayFakeMessage = (content, sender) => {
|
|
|
|
const msgList = [...this.state.messages]
|
|
|
|
const msg = {
|
|
|
|
id: uuid(),
|
|
|
|
type: 'm.room.message',
|
|
|
|
sender: sender,
|
|
|
|
roomId: this.state.roomId,
|
|
|
|
content: content,
|
|
|
|
}
|
|
|
|
msgList.push(msg)
|
|
|
|
|
|
|
|
this.setState({ messages: msgList })
|
|
|
|
}
|
|
|
|
|
|
|
|
displayBotMessage = (content, roomId) => {
|
|
|
|
const msgList = [...this.state.messages]
|
|
|
|
const msg = {
|
|
|
|
id: uuid(),
|
|
|
|
type: 'm.room.message',
|
2020-03-22 15:07:41 +00:00
|
|
|
sender: this.props.botId,
|
2020-03-05 03:05:09 +00:00
|
|
|
roomId: roomId || this.state.roomId,
|
|
|
|
content: content,
|
|
|
|
}
|
|
|
|
msgList.push(msg)
|
|
|
|
|
|
|
|
this.setState({ messages: msgList })
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:43:26 +00:00
|
|
|
handleMessageEvent = event => {
|
|
|
|
const message = {
|
|
|
|
id: event.getId(),
|
|
|
|
type: event.getType(),
|
|
|
|
sender: event.getSender(),
|
|
|
|
roomId: event.getRoomId(),
|
|
|
|
content: event.getContent(),
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:47:13 +00:00
|
|
|
if (message.content.showToUser && message.content.showToUser !== this.state.userId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.content.body.startsWith('!bot') && message.sender !== this.state.userId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-04 05:43:26 +00:00
|
|
|
const messages = [...this.state.messages]
|
|
|
|
messages.push(message)
|
|
|
|
this.setState({ messages })
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:38:20 +00:00
|
|
|
|
2020-03-25 23:01:29 +00:00
|
|
|
handleKeyDown = (e) => {
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
if (this.state.emojiSelectorOpen) {
|
|
|
|
this.closeEmojiSelector()
|
|
|
|
} else if (this.state.opened) {
|
|
|
|
this.handleToggleOpen()
|
|
|
|
}
|
2020-02-14 16:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2020-02-24 04:12:47 +00:00
|
|
|
if (this.state.client && prevState.client !== this.state.client) {
|
2020-02-05 18:42:34 +00:00
|
|
|
this.createRoom()
|
2020-02-01 05:30:58 +00:00
|
|
|
|
|
|
|
this.state.client.once('sync', (state, prevState, res) => {
|
|
|
|
if (state === "PREPARED") {
|
|
|
|
this.setState({ ready: true })
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.state.client.on("Room.timeline", (event, room, toStartOfTimeline) => {
|
2020-02-16 23:31:50 +00:00
|
|
|
if (event.getType() === "m.room.encryption") {
|
2020-03-05 03:05:09 +00:00
|
|
|
this.displayBotMessage({ body: ENCRYPTION_NOTICE }, room.room_id)
|
|
|
|
}
|
2020-02-23 17:01:58 +00:00
|
|
|
|
2020-03-05 03:05:09 +00:00
|
|
|
if (event.getType() === "m.room.message" && !this.state.isCryptoEnabled) {
|
|
|
|
if (event.isEncrypted()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.handleMessageEvent(event)
|
2020-02-23 17:01:58 +00:00
|
|
|
}
|
2020-02-04 05:43:26 +00:00
|
|
|
});
|
|
|
|
|
2020-03-05 03:05:09 +00:00
|
|
|
this.state.client.on("Event.decrypted", (event, err) => {
|
|
|
|
if (err) {
|
|
|
|
return this.handleDecryptionError()
|
|
|
|
}
|
2020-02-04 05:43:26 +00:00
|
|
|
if (event.getType() === "m.room.message") {
|
|
|
|
this.handleMessageEvent(event)
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
});
|
2020-02-23 17:01:58 +00:00
|
|
|
|
|
|
|
this.state.client.on("RoomMember.typing", (event, member) => {
|
2020-03-22 02:50:21 +00:00
|
|
|
if (member.typing && member.roomId === this.state.roomId) {
|
|
|
|
this.setState({ typingStatus: `${member.name} is typing...` })
|
2020-02-23 17:01:58 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setState({ typingStatus: null })
|
|
|
|
}
|
|
|
|
});
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 22:48:30 +00:00
|
|
|
if (prevState.messages.length !== this.state.messages.length) {
|
2020-03-13 04:05:12 +00:00
|
|
|
if (this.messageWindow.current.scrollTo) {
|
|
|
|
this.messageWindow.current.scrollTo(0, this.messageWindow.current.scrollHeight)
|
|
|
|
}
|
2020-02-25 22:48:30 +00:00
|
|
|
}
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 16:38:20 +00:00
|
|
|
componentDidMount() {
|
2020-03-25 23:01:29 +00:00
|
|
|
document.addEventListener("keydown", this.handleKeyDown, false);
|
2020-02-26 05:12:05 +00:00
|
|
|
window.addEventListener('beforeunload', this.exitChat)
|
2020-02-14 16:38:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
componentWillUnmount() {
|
2020-03-25 23:01:29 +00:00
|
|
|
document.removeEventListener("keydown", this.handleKeyDown, false);
|
2020-02-26 05:12:05 +00:00
|
|
|
window.removeEventListener('beforeunload', this.exitChat)
|
|
|
|
this.exitChat();
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInputChange = e => {
|
2020-03-13 04:05:12 +00:00
|
|
|
this.setState({ inputValue: e.target.value })
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 17:08:57 +00:00
|
|
|
handleAcceptTerms = () => {
|
|
|
|
this.setState({ awaitingAgreement: false })
|
|
|
|
this.initializeChat()
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRejectTerms = () => {
|
|
|
|
this.exitChat()
|
|
|
|
this.displayBotMessage({ body: this.props.exitMessage })
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
handleSubmit = e => {
|
|
|
|
e.preventDefault()
|
2020-03-15 04:47:19 +00:00
|
|
|
const message = this.state.inputValue
|
|
|
|
if (!Boolean(message)) return null;
|
2020-02-01 05:30:58 +00:00
|
|
|
|
2020-02-24 19:16:43 +00:00
|
|
|
if (this.state.client && this.state.roomId) {
|
2020-03-15 04:47:19 +00:00
|
|
|
this.setState({ inputValue: "" })
|
|
|
|
this.chatboxInput.current.focus()
|
|
|
|
return this.sendMessage(message)
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:01:29 +00:00
|
|
|
onEmojiClick = (event, emojiObject) => {
|
|
|
|
const { emoji } = emojiObject;
|
|
|
|
this.setState({
|
|
|
|
inputValue: this.state.inputValue.concat(emoji),
|
|
|
|
emojiSelectorOpen: false,
|
|
|
|
}, () => this.chatboxInput.current.focus())
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
render() {
|
2020-03-25 23:01:29 +00:00
|
|
|
const { ready, messages, inputValue, userId, roomId, typingStatus, opened, showDock, emojiSelectorOpen } = this.state;
|
2020-02-14 16:38:20 +00:00
|
|
|
const inputLabel = 'Send a message...'
|
2020-02-01 05:30:58 +00:00
|
|
|
|
|
|
|
return (
|
2020-02-24 04:12:47 +00:00
|
|
|
<div className="docked-widget" role="complementary">
|
2020-02-24 19:16:43 +00:00
|
|
|
<Transition in={opened} timeout={250} onExited={this.handleWidgetExit} onEntered={this.handleWidgetEnter}>
|
|
|
|
{(status) => {
|
|
|
|
return (
|
2020-02-24 04:12:47 +00:00
|
|
|
<div className={`widget widget-${status}`} aria-hidden={!opened}>
|
|
|
|
<div id="ocrcc-chatbox" aria-haspopup="dialog">
|
|
|
|
<Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />
|
|
|
|
|
2020-02-25 22:48:30 +00:00
|
|
|
<div className="message-window" ref={this.messageWindow}>
|
2020-02-24 04:12:47 +00:00
|
|
|
<div className="messages">
|
2020-03-12 17:08:57 +00:00
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<div className="text">{ this.props.introMessage }</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={`message from-bot`}>
|
2020-03-15 04:47:19 +00:00
|
|
|
<div className="text">Please read the full <a href={this.props.termsUrl} ref={this.termsUrl} target='_blank' rel='noopener noreferrer'>terms and conditions</a>. By using this chat, you agree to these terms.</div>
|
2020-03-12 17:08:57 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<div className="text">{ this.props.agreementMessage }</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<div className="text buttons">
|
|
|
|
{`👉`}
|
2020-03-15 04:47:19 +00:00
|
|
|
<button className="btn" id="accept" onClick={this.handleAcceptTerms}>YES</button>
|
|
|
|
<button className="btn" id="reject" onClick={this.handleRejectTerms}>NO</button>
|
2020-03-12 17:08:57 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2020-02-24 04:12:47 +00:00
|
|
|
{
|
|
|
|
messages.map((message, index) => {
|
|
|
|
return(
|
2020-03-22 15:07:41 +00:00
|
|
|
<Message key={message.id} message={message} userId={userId} botId={this.props.botId} client={this.state.client} />
|
2020-02-24 04:12:47 +00:00
|
|
|
)
|
2020-02-24 19:16:43 +00:00
|
|
|
})
|
2020-02-24 04:12:47 +00:00
|
|
|
}
|
|
|
|
{ typingStatus &&
|
|
|
|
<div className="notices">
|
|
|
|
<div role="status">{typingStatus}</div>
|
|
|
|
</div>
|
|
|
|
}
|
2020-03-13 04:05:12 +00:00
|
|
|
{ !ready && <div className={`loader`}>loading...</div> }
|
2020-02-24 04:12:47 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="input-window">
|
|
|
|
<form onSubmit={this.handleSubmit}>
|
2020-03-25 23:01:29 +00:00
|
|
|
<div className="message-input-container">
|
|
|
|
<input
|
|
|
|
id="message-input"
|
|
|
|
type="text"
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
value={inputValue}
|
|
|
|
aria-label={inputLabel}
|
|
|
|
placeholder={inputLabel}
|
|
|
|
autoFocus={true}
|
|
|
|
onFocus={(e) => this.setState({ inputValue: this.state.inputValue })}
|
|
|
|
ref={this.chatboxInput}
|
|
|
|
/>
|
|
|
|
<EmojiSelector
|
|
|
|
onEmojiClick={this.onEmojiClick}
|
|
|
|
emojiSelectorOpen={emojiSelectorOpen}
|
|
|
|
toggleEmojiSelector={this.toggleEmojiSelector}
|
|
|
|
closeEmojiSelector={this.closeEmojiSelector}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-03-13 04:05:12 +00:00
|
|
|
<input type="submit" value="Send" id="submit" />
|
2020-02-24 04:12:47 +00:00
|
|
|
</form>
|
|
|
|
</div>
|
2020-02-23 21:57:16 +00:00
|
|
|
</div>
|
2020-02-24 04:12:47 +00:00
|
|
|
</div>
|
2020-02-24 19:16:43 +00:00
|
|
|
)}
|
|
|
|
}
|
2020-02-24 04:12:47 +00:00
|
|
|
</Transition>
|
|
|
|
{showDock && !roomId && <Dock handleToggleOpen={this.handleToggleOpen} />}
|
|
|
|
{showDock && roomId && <Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />}
|
2020-02-01 05:30:58 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ChatBox.propTypes = {
|
2020-02-05 18:42:34 +00:00
|
|
|
matrixServerUrl: PropTypes.string.isRequired,
|
2020-03-22 15:07:41 +00:00
|
|
|
botId: PropTypes.string.isRequired,
|
|
|
|
termsUrl: PropTypes.string,
|
|
|
|
introMessage: PropTypes.string,
|
2020-03-05 05:06:24 +00:00
|
|
|
roomName: PropTypes.string,
|
|
|
|
agreementMessage: PropTypes.string,
|
|
|
|
confirmationMessage: PropTypes.string,
|
|
|
|
exitMessage: PropTypes.string,
|
|
|
|
chatUnavailableMessage: PropTypes.string,
|
|
|
|
anonymousDisplayName: PropTypes.string,
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChatBox.defaultProps = {
|
2020-03-12 17:08:57 +00:00
|
|
|
matrixServerUrl: DEFAULT_MATRIX_SERVER,
|
2020-03-22 15:07:41 +00:00
|
|
|
botId: DEFAULT_BOT_ID,
|
2020-03-12 17:08:57 +00:00
|
|
|
termsUrl: DEFAULT_TERMS_URL,
|
2020-02-05 18:42:34 +00:00
|
|
|
roomName: DEFAULT_ROOM_NAME,
|
2020-03-05 05:06:24 +00:00
|
|
|
introMessage: DEFAULT_INTRO_MESSAGE,
|
|
|
|
agreementMessage: DEFAULT_AGREEMENT_MESSAGE,
|
|
|
|
confirmationMessage: DEFAULT_CONFIRMATION_MESSAGE,
|
|
|
|
exitMessage: DEFAULT_EXIT_MESSAGE,
|
|
|
|
anonymousDisplayName: DEFAULT_ANONYMOUS_DISPLAY_NAME,
|
|
|
|
chatUnavailableMessage: DEFAULT_CHAT_UNAVAILABLE_MESSAGE,
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ChatBox;
|
|
|
|
|