2020-02-01 05:30:58 +00:00
|
|
|
import React from "react"
|
|
|
|
import PropTypes from "prop-types"
|
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-02 18:36:11 +00:00
|
|
|
|
2020-02-05 18:42:34 +00:00
|
|
|
const DEFAULT_MATRIX_SERVER = "https://matrix.rhok.space"
|
|
|
|
const DEFAULT_ROOM_NAME = "Support Chat"
|
2020-02-23 17:01:58 +00:00
|
|
|
const FACILITATOR_USERNAME = "@help-bot:rhok.space"
|
2020-02-04 05:43:26 +00:00
|
|
|
const ENCRYPTION_CONFIG = { "algorithm": "m.megolm.v1.aes-sha2" };
|
2020-02-05 18:42:34 +00:00
|
|
|
const DEFAULT_THEME = {
|
|
|
|
themeColor: "#008080", // teal
|
|
|
|
lightColor: "#FFF8F0",
|
|
|
|
darkColor: "#22333B",
|
|
|
|
errorColor: "#FFFACD",
|
|
|
|
font: "'Assistant', 'Helvetica', sans-serif",
|
|
|
|
placement: "right"
|
|
|
|
};
|
2020-02-01 05:30:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChatBox extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-02-05 18:42:34 +00:00
|
|
|
const client = matrix.createClient(this.props.matrixServerUrl)
|
2020-02-01 05:30:58 +00:00
|
|
|
this.state = {
|
2020-02-05 18:42:34 +00:00
|
|
|
client: null,
|
2020-02-01 05:30:58 +00:00
|
|
|
ready: false,
|
2020-02-04 06:13:47 +00:00
|
|
|
accessToken: null,
|
|
|
|
userId: null,
|
2020-02-01 05:30:58 +00:00
|
|
|
messages: [],
|
|
|
|
inputValue: "",
|
2020-02-05 18:42:34 +00:00
|
|
|
errors: [],
|
|
|
|
roomId: null,
|
2020-02-23 17:01:58 +00:00
|
|
|
typingStatus: null,
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
this.chatboxInput = React.createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
leaveRoom = () => {
|
2020-02-04 06:13:47 +00:00
|
|
|
if (this.state.roomId) {
|
|
|
|
this.state.client.leave(this.state.roomId).then(data => {
|
2020-02-01 05:30:58 +00:00
|
|
|
console.log("Left room", data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 18:42:34 +00:00
|
|
|
initializeClient = () => {
|
|
|
|
// empty registration request to get session
|
|
|
|
let client = matrix.createClient(this.props.matrixServerUrl)
|
|
|
|
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,
|
|
|
|
username: username
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
client.setDisplayName("Anonymous")
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log("Registration error", err)
|
|
|
|
})
|
|
|
|
.then(() => client.initCrypto())
|
|
|
|
.finally(() => client.startClient())
|
|
|
|
.then(() => {
|
|
|
|
this.setState({
|
|
|
|
client: client
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:58 +00:00
|
|
|
verifyAllRoomDevices = async function(roomId) {
|
|
|
|
let room = this.state.client.getRoom(roomId);
|
|
|
|
console.log('room', room)
|
|
|
|
let members = (await room.getEncryptionTargetMembers()).map(x => x["userId"])
|
|
|
|
console.log('members', members)
|
|
|
|
let memberkeys = await this.state.client.downloadKeys(members);
|
|
|
|
for (const userId in memberkeys) {
|
|
|
|
for (const deviceId in memberkeys[userId]) {
|
|
|
|
console.log("verifying device", `${userId} - ${deviceId}`)
|
|
|
|
await this.state.client.setDeviceVerified(userId, deviceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
createRoom = () => {
|
|
|
|
const currentDate = new Date()
|
|
|
|
const chatDate = currentDate.toLocaleDateString()
|
|
|
|
const chatTime = currentDate.toLocaleTimeString()
|
|
|
|
return this.state.client.createRoom({
|
|
|
|
room_alias_name: `private-support-chat-${uuid()}`,
|
|
|
|
invite: [FACILITATOR_USERNAME], // TODO: create bot user to add
|
|
|
|
visibility: 'private',
|
2020-02-05 18:42:34 +00:00
|
|
|
name: `${chatDate} - ${this.props.roomName} - started at ${chatTime}`,
|
2020-02-04 05:43:26 +00:00
|
|
|
initial_state: [
|
|
|
|
{
|
|
|
|
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-02-05 18:42:34 +00:00
|
|
|
})
|
|
|
|
.then(data => {
|
2020-02-23 17:01:58 +00:00
|
|
|
this.verifyAllRoomDevices(data.room_id)
|
|
|
|
this.state.client.setPowerLevel(data.room_id, FACILITATOR_USERNAME, 100)
|
|
|
|
.then(() => console.log("Set bot power level to 100"))
|
|
|
|
.catch(err => console.log("Error setting bot power level", err))
|
2020-02-04 05:43:26 +00:00
|
|
|
this.setState({
|
2020-02-04 06:13:47 +00:00
|
|
|
roomId: data.room_id
|
2020-02-04 05:43:26 +00:00
|
|
|
})
|
2020-02-05 18:42:34 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2020-02-04 05:43:26 +00:00
|
|
|
console.log("Unable to create room", err)
|
2020-02-01 05:30:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage = () => {
|
2020-02-05 18:42:34 +00:00
|
|
|
this.state.client.sendTextMessage(this.state.roomId, this.state.inputValue)
|
|
|
|
.then((res) => {
|
|
|
|
this.setState({
|
|
|
|
inputValue: "",
|
|
|
|
})
|
|
|
|
this.chatboxInput.current.focus()
|
2020-02-04 06:13:47 +00:00
|
|
|
})
|
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-02-05 18:42:34 +00:00
|
|
|
this.sendMessage()
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Error sending message", err);
|
|
|
|
}
|
|
|
|
})
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages = [...this.state.messages]
|
|
|
|
messages.push(message)
|
|
|
|
this.setState({ messages })
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:38:20 +00:00
|
|
|
|
|
|
|
handleEscape = (e) => {
|
|
|
|
if (e.keyCode === 27 && this.props.opened) {
|
|
|
|
this.props.handleToggleOpen()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (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) => {
|
|
|
|
if (event.getType() === "m.room.message") {
|
2020-02-04 05:43:26 +00:00
|
|
|
|
|
|
|
if (event.status === "sending") {
|
|
|
|
return; // do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.status === "not sent") {
|
|
|
|
return console.log("message not sent!", event)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.isEncrypted()) {
|
|
|
|
return console.log("message encrypted")
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleMessageEvent(event)
|
|
|
|
}
|
2020-02-16 23:31:50 +00:00
|
|
|
|
|
|
|
if (event.getType() === "m.room.encryption") {
|
|
|
|
this.setState({ isRoomEncrypted: true })
|
|
|
|
}
|
2020-02-23 17:01:58 +00:00
|
|
|
|
|
|
|
if (event.getType() === "m.notice") {
|
|
|
|
console.log("GOT BOT NOTICE!", event)
|
|
|
|
}
|
2020-02-04 05:43:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.state.client.on("Event.decrypted", (event) => {
|
|
|
|
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) => {
|
|
|
|
if (member.typing) {
|
|
|
|
this.setState({ typingStatus: `${member.name} is typing...`})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setState({ typingStatus: null })
|
|
|
|
}
|
|
|
|
});
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 18:42:34 +00:00
|
|
|
if (prevState.roomId !== this.state.roomId) {
|
|
|
|
this.setState({
|
|
|
|
isRoomEncrypted: this.state.client.isRoomEncrypted(this.state.roomId)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prevState.ready && this.state.ready) {
|
2020-02-01 05:30:58 +00:00
|
|
|
this.chatboxInput.current.focus()
|
|
|
|
}
|
2020-02-05 18:42:34 +00:00
|
|
|
|
|
|
|
if (this.state.client === null && prevProps.status !== "entered" && this.props.status === "entered") {
|
|
|
|
this.initializeClient()
|
|
|
|
}
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 16:38:20 +00:00
|
|
|
componentDidMount() {
|
|
|
|
document.addEventListener("keydown", this.handleEscape, false);
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
componentWillUnmount() {
|
2020-02-14 16:38:20 +00:00
|
|
|
document.removeEventListener("keydown", this.handleEscape, false);
|
2020-02-01 05:30:58 +00:00
|
|
|
this.leaveRoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputChange = e => {
|
|
|
|
this.setState({ inputValue: e.currentTarget.value })
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = e => {
|
|
|
|
e.preventDefault()
|
|
|
|
if (!Boolean(this.state.inputValue)) return null;
|
|
|
|
|
2020-02-04 06:13:47 +00:00
|
|
|
if (!this.state.roomId) {
|
2020-02-01 05:30:58 +00:00
|
|
|
return this.createRoom().then(this.sendMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-02-23 17:01:58 +00:00
|
|
|
const { ready, messages, inputValue, userId, isRoomEncrypted, typingStatus } = this.state;
|
2020-02-16 23:31:50 +00:00
|
|
|
const { opened, handleToggleOpen, privacyStatement, termsUrl } = this.props;
|
2020-02-14 16:38:20 +00:00
|
|
|
const inputLabel = 'Send a message...'
|
2020-02-01 05:30:58 +00:00
|
|
|
|
|
|
|
return (
|
2020-02-14 16:38:20 +00:00
|
|
|
<div id="ocrcc-chatbox" aria-haspopup="dialog" aria-label="Open support chat">
|
2020-02-01 05:30:58 +00:00
|
|
|
<div className="widget-header">
|
|
|
|
<div className="widget-header-title">
|
2020-02-14 16:38:20 +00:00
|
|
|
Support Chat
|
2020-02-01 05:30:58 +00:00
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={`widget-header-icon`}
|
|
|
|
onClick={handleToggleOpen}
|
|
|
|
onKeyPress={handleToggleOpen}
|
|
|
|
>
|
|
|
|
<span className={`arrow ${opened ? "opened" : "closed"}`}>⌃</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
2020-02-05 18:42:34 +00:00
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
<div className="message-window">
|
|
|
|
<div className="messages">
|
2020-02-23 17:01:58 +00:00
|
|
|
<div className="notices">
|
|
|
|
{ typingStatus && <div>{typingStatus}</div> }
|
|
|
|
</div>
|
2020-02-01 05:30:58 +00:00
|
|
|
{
|
2020-02-05 18:42:34 +00:00
|
|
|
ready ?
|
2020-02-01 05:30:58 +00:00
|
|
|
messages.map((message, index) => {
|
|
|
|
return(
|
2020-02-04 06:13:47 +00:00
|
|
|
<Message key={message.id} message={message} userId={userId} />
|
2020-02-01 05:30:58 +00:00
|
|
|
)
|
2020-02-05 18:42:34 +00:00
|
|
|
}) :
|
|
|
|
<div className="loader">loading...</div>
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
2020-02-05 18:42:34 +00:00
|
|
|
</div>
|
|
|
|
<div className="notices">
|
2020-02-16 23:31:50 +00:00
|
|
|
{ privacyStatement && <div>{privacyStatement}</div> }
|
|
|
|
{ termsUrl && <div>By using this service you agree to the <a href={termsUrl} target="_blank">Terms of Use</a>.</div> }
|
|
|
|
{ isRoomEncrypted && <div role="status">Messages in this chat are secured with end-to-end encryption.</div> }
|
2020-02-05 18:42:34 +00:00
|
|
|
</div>
|
2020-02-01 05:30:58 +00:00
|
|
|
</div>
|
|
|
|
<div className="input-window">
|
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
value={inputValue}
|
2020-02-14 16:38:20 +00:00
|
|
|
aria-label={inputLabel}
|
|
|
|
placeholder={inputLabel}
|
2020-02-01 05:30:58 +00:00
|
|
|
autoFocus={true}
|
|
|
|
ref={this.chatboxInput}
|
|
|
|
/>
|
|
|
|
<input type="submit" value="Send" />
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ChatBox.propTypes = {
|
2020-02-05 18:42:34 +00:00
|
|
|
matrixServerUrl: PropTypes.string.isRequired,
|
|
|
|
roomName: PropTypes.string.isRequired,
|
|
|
|
theme: PropTypes.object,
|
2020-02-16 23:31:50 +00:00
|
|
|
termsUrl: PropTypes.string,
|
|
|
|
privacyStatement: PropTypes.string,
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChatBox.defaultProps = {
|
2020-02-05 18:42:34 +00:00
|
|
|
matrixServerUrl: DEFAULT_MATRIX_SERVER,
|
|
|
|
roomName: DEFAULT_ROOM_NAME,
|
|
|
|
theme: DEFAULT_THEME,
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ChatBox;
|
|
|
|
|