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-04-29 03:43:17 +00:00
|
|
|
import * as olm from "olm/olm_legacy.js"
|
2020-02-02 04:18:09 +00:00
|
|
|
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';
|
2022-01-17 20:44:05 +00:00
|
|
|
import defaultConfig from '../defaultConfig.js';
|
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-04-17 22:04:34 +00:00
|
|
|
const UNENCRYPTION_NOTICE = "Messages in this chat are not encrypted."
|
2020-03-05 03:05:09 +00:00
|
|
|
const RESTARTING_UNENCRYPTED_CHAT_MESSAGE = "Restarting chat without encryption."
|
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-06-11 05:54:05 +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-04-17 22:04:34 +00:00
|
|
|
facilitatorInvited: false,
|
|
|
|
isMobile: true,
|
|
|
|
isSlowConnection: true,
|
|
|
|
decryptionErrors: {},
|
2020-11-28 20:02:55 +00:00
|
|
|
messagesInFlight: []
|
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-04-17 22:04:34 +00:00
|
|
|
detectMobile = () => {
|
|
|
|
let isMobile = false;
|
|
|
|
|
|
|
|
if ( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
|
|
|
|
isMobile = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (screen.width < 767) {
|
|
|
|
isMobile = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ isMobile })
|
|
|
|
}
|
|
|
|
|
|
|
|
detectSlowConnection = () => {
|
|
|
|
let isSlowConnection = false;
|
|
|
|
|
|
|
|
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
|
|
|
|
|
|
if (typeof connection !== 'undefined' || connection === null) {
|
|
|
|
const connectionType = connection.effectiveType;
|
|
|
|
const slowConnections = ['slow-2g', '2g']
|
|
|
|
|
|
|
|
isSlowConnection = slowConnections.includes(connectionType)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ isSlowConnection })
|
|
|
|
}
|
|
|
|
|
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-26 23:13:55 +00:00
|
|
|
toggleEmojiSelector = (e) => {
|
|
|
|
e.preventDefault();
|
2020-03-25 23:01:29 +00:00
|
|
|
this.setState({ emojiSelectorOpen: !this.state.emojiSelectorOpen })
|
|
|
|
}
|
|
|
|
|
|
|
|
closeEmojiSelector = () => {
|
2020-03-27 03:58:47 +00:00
|
|
|
this.setState({ emojiSelectorOpen: false })
|
2020-03-25 23:01:29 +00:00
|
|
|
}
|
|
|
|
|
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-06-10 20:59:50 +00:00
|
|
|
exitChat = async (resetState=true) => {
|
2020-06-10 21:49:19 +00:00
|
|
|
if (this.state.client) {
|
2021-01-21 03:44:53 +00:00
|
|
|
try {
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
await this.state.client.deactivateAccount(auth, true)
|
|
|
|
await this.state.client.stopClient()
|
|
|
|
await this.state.client.clearStores()
|
|
|
|
} catch (err) {
|
|
|
|
console.log("Error exiting chat", err)
|
|
|
|
} finally {
|
|
|
|
this.setState({ client: null, ready: true }) // no more loading animation
|
|
|
|
window.clearInterval(this.state.waitIntervalId) // no more waiting messages
|
|
|
|
}
|
2020-06-10 21:49:19 +00:00
|
|
|
}
|
2020-04-17 22:04:34 +00:00
|
|
|
|
2020-09-06 19:24:41 +00:00
|
|
|
if (this.state.localStorage) {
|
|
|
|
this.state.localStorage.clear()
|
|
|
|
}
|
2020-06-10 20:59:50 +00:00
|
|
|
|
|
|
|
if (resetState) {
|
|
|
|
this.setState(this.initialState)
|
|
|
|
}
|
2020-04-17 22:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createLocalStorage = async (deviceId, sessionId) => {
|
|
|
|
let localStorage = global.localStorage;
|
|
|
|
if (typeof localStorage === "undefined" || localStorage === null) {
|
|
|
|
const deviceDesc = `matrix-chat-${deviceId}-${sessionId}`
|
|
|
|
const localStoragePath = path.resolve(path.join(os.homedir(), ".local-storage", deviceDesc))
|
|
|
|
localStorage = new LocalStorage(localStoragePath);
|
|
|
|
}
|
|
|
|
return localStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
createClientWithAccount = async () => {
|
|
|
|
const tmpClient = matrix.createClient(this.props.matrixServerUrl)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tmpClient.registerRequest({})
|
|
|
|
} catch(err) {
|
|
|
|
const username = uuid()
|
|
|
|
const password = uuid()
|
|
|
|
const sessionId = err.data.session
|
|
|
|
|
|
|
|
const account = await tmpClient.registerRequest({
|
|
|
|
auth: {session: sessionId, type: "m.login.dummy"},
|
|
|
|
inhibit_login: false,
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
x_show_msisdn: true,
|
2020-02-26 05:12:05 +00:00
|
|
|
})
|
2020-04-17 22:04:34 +00:00
|
|
|
|
|
|
|
const localStorage = await this.createLocalStorage(account.device_id, sessionId)
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
accessToken: account.access_token,
|
|
|
|
userId: account.user_id,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
localStorage: localStorage,
|
|
|
|
sessionId: sessionId,
|
|
|
|
deviceId: account.device_id,
|
2020-02-26 05:12:05 +00:00
|
|
|
})
|
2020-04-17 22:04:34 +00:00
|
|
|
|
|
|
|
let opts = {
|
|
|
|
baseUrl: this.props.matrixServerUrl,
|
|
|
|
accessToken: account.access_token,
|
|
|
|
userId: account.user_id,
|
|
|
|
deviceId: account.device_id,
|
|
|
|
sessionStore: new matrix.WebStorageSessionStore(localStorage),
|
|
|
|
}
|
|
|
|
|
|
|
|
return matrix.createClient(opts)
|
|
|
|
}
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
initializeChat = async () => {
|
2020-02-24 19:16:43 +00:00
|
|
|
this.setState({ ready: false })
|
2020-04-17 22:04:34 +00:00
|
|
|
|
|
|
|
const client = await this.createClientWithAccount()
|
2020-03-05 05:06:24 +00:00
|
|
|
try {
|
2020-04-17 22:04:34 +00:00
|
|
|
await client.initCrypto()
|
|
|
|
} catch(err) {
|
2020-06-11 12:20:49 +00:00
|
|
|
return this.restartWithoutCrypto()
|
2020-03-05 05:06:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
await client.startClient()
|
2020-07-01 04:50:34 +00:00
|
|
|
|
|
|
|
client.once('sync', async (state, prevState, data) => {
|
|
|
|
if (state === "PREPARED") {
|
|
|
|
this.setState({ client })
|
2020-10-21 01:04:34 +00:00
|
|
|
client.setDisplayName(this.props.displayName)
|
2020-07-01 04:50:34 +00:00
|
|
|
this.setMatrixListeners(client)
|
|
|
|
await this.createRoom(client)
|
|
|
|
}
|
|
|
|
})
|
2020-04-17 22:04:34 +00:00
|
|
|
}
|
2020-02-05 18:42:34 +00:00
|
|
|
|
2020-06-11 12:20:49 +00:00
|
|
|
restartWithoutCrypto = async () => {
|
2020-04-17 22:04:34 +00:00
|
|
|
if (this.state.client) {
|
2020-04-23 14:25:24 +00:00
|
|
|
this.state.client.leave(this.state.roomId)
|
2020-04-17 22:04:34 +00:00
|
|
|
this.state.client.stopClient()
|
|
|
|
this.state.client.clearStores()
|
|
|
|
this.state.localStorage.clear()
|
|
|
|
}
|
2020-02-05 18:42:34 +00:00
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
this.setState({
|
|
|
|
ready: false,
|
|
|
|
facilitatorInvited: false,
|
|
|
|
decryptionErrors: {},
|
|
|
|
roomId: null,
|
|
|
|
typingStatus: null,
|
|
|
|
client: null,
|
|
|
|
isCryptoEnabled: false,
|
2020-02-05 18:42:34 +00:00
|
|
|
})
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
this.displayBotMessage({ body: RESTARTING_UNENCRYPTED_CHAT_MESSAGE })
|
2020-03-05 03:05:09 +00:00
|
|
|
|
|
|
|
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;
|
2020-04-17 22:04:34 +00:00
|
|
|
client = matrix.createClient(opts)
|
2020-07-01 04:50:34 +00:00
|
|
|
await client.startClient()
|
2020-04-17 22:04:34 +00:00
|
|
|
|
2020-07-01 04:50:34 +00:00
|
|
|
client.once('sync', async (state, prevState, data) => {
|
|
|
|
if (state === "PREPARED") {
|
|
|
|
try {
|
|
|
|
this.setState({ client })
|
2020-10-21 01:04:34 +00:00
|
|
|
client.setDisplayName(this.props.displayName)
|
2020-07-01 04:50:34 +00:00
|
|
|
this.setMatrixListeners(client)
|
|
|
|
await this.createRoom(client)
|
|
|
|
this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
|
|
|
|
} catch(err) {
|
|
|
|
console.log("error", err)
|
|
|
|
this.handleInitError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-06-11 12:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initializeUnencryptedChat = async () => {
|
|
|
|
this.setState({ ready: false })
|
|
|
|
const client = await this.createClientWithAccount()
|
|
|
|
await client.startClient()
|
2020-07-01 04:50:34 +00:00
|
|
|
|
|
|
|
client.once('sync', async (state, prevState, data) => {
|
2020-10-21 01:04:34 +00:00
|
|
|
client.setDisplayName(this.props.displayName)
|
2020-07-01 04:50:34 +00:00
|
|
|
this.setMatrixListeners(client)
|
|
|
|
await this.createRoom(client)
|
|
|
|
})
|
2020-03-05 05:06:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 23:13:55 +00:00
|
|
|
handleInitError = (err) => {
|
2020-04-23 14:25:24 +00:00
|
|
|
console.log("error", err)
|
2020-03-05 05:06:24 +00:00
|
|
|
this.displayBotMessage({ body: this.props.chatUnavailableMessage })
|
|
|
|
this.setState({ ready: true })
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
handleDecryptionError = async (event, err) => {
|
|
|
|
const eventId = event.getId()
|
2020-06-10 20:59:50 +00:00
|
|
|
this.handleMessageEvent(event)
|
2020-04-17 22:04:34 +00:00
|
|
|
this.setState({ decryptionErrors: { [eventId]: true }})
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
verifyAllRoomDevices = async (client, room) => {
|
|
|
|
if (!room) return;
|
|
|
|
if (!client) return;
|
|
|
|
if (!this.state.isCryptoEnabled) return;
|
|
|
|
|
2020-02-23 17:01:58 +00:00
|
|
|
let members = (await room.getEncryptionTargetMembers()).map(x => x["userId"])
|
2020-04-17 22:04:34 +00:00
|
|
|
let memberkeys = await client.downloadKeys(members);
|
2020-02-23 17:01:58 +00:00
|
|
|
for (const userId in memberkeys) {
|
|
|
|
for (const deviceId in memberkeys[userId]) {
|
2020-04-17 22:04:34 +00:00
|
|
|
await client.setDeviceVerified(userId, deviceId);
|
2020-02-23 17:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
createRoom = async (client) => {
|
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
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
const isCryptoEnabled = await client.isCryptoEnabled()
|
2020-03-05 03:05:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
const { room_id } = await client.createRoom(roomConfig)
|
2020-03-05 03:05:09 +00:00
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
client.setPowerLevel(room_id, this.props.botId, 100)
|
2020-03-05 03:05:09 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
roomId: room_id,
|
|
|
|
isCryptoEnabled
|
2020-02-01 05:30:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:25:24 +00:00
|
|
|
sendMessage = async (message) => {
|
|
|
|
if (this.state.client && this.state.roomId) {
|
|
|
|
try {
|
|
|
|
await this.state.client.sendTextMessage(this.state.roomId, message)
|
|
|
|
} catch(err) {
|
|
|
|
switch (err["name"]) {
|
2020-02-06 01:26:51 +00:00
|
|
|
case "UnknownDeviceError":
|
2020-02-05 18:42:34 +00:00
|
|
|
Object.keys(err.devices).forEach((userId) => {
|
2020-04-23 14:25:24 +00:00
|
|
|
Object.keys(err.devices[userId]).map(async (deviceId) => {
|
|
|
|
await this.state.client.setDeviceKnown(userId, deviceId, true);
|
2020-02-05 18:42:34 +00:00
|
|
|
});
|
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-04-23 14:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
displayFakeMessage = (content, sender, messageId=uuid()) => {
|
2020-03-05 03:05:09 +00:00
|
|
|
const msg = {
|
2020-04-17 22:04:34 +00:00
|
|
|
id: messageId,
|
2020-03-05 03:05:09 +00:00
|
|
|
type: 'm.room.message',
|
|
|
|
sender: sender,
|
|
|
|
roomId: this.state.roomId,
|
|
|
|
content: content,
|
2020-06-11 05:54:05 +00:00
|
|
|
timestamp: Date.now(),
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 05:54:05 +00:00
|
|
|
this.setState({
|
|
|
|
messages: {
|
|
|
|
...this.state.messages,
|
|
|
|
[messageId]: msg
|
|
|
|
}
|
|
|
|
})
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 05:54:05 +00:00
|
|
|
displayBotMessage = (content, roomId, messageId=uuid()) => {
|
2020-03-05 03:05:09 +00:00
|
|
|
const msg = {
|
2020-06-11 05:54:05 +00:00
|
|
|
id: messageId,
|
2020-03-05 03:05:09 +00:00
|
|
|
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,
|
2020-06-11 05:54:05 +00:00
|
|
|
timestamp: Date.now(),
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 05:54:05 +00:00
|
|
|
this.setState({
|
|
|
|
messages: {
|
|
|
|
...this.state.messages,
|
|
|
|
[messageId]: msg
|
|
|
|
}
|
|
|
|
})
|
2020-03-05 03:05:09 +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(),
|
2020-06-11 05:54:05 +00:00
|
|
|
timestamp: event.getTs(),
|
2020-02-04 05:43:26 +00:00
|
|
|
}
|
2020-06-11 05:54:05 +00:00
|
|
|
|
2020-06-10 21:49:19 +00:00
|
|
|
// there's also event.getClearContent() which only works on encrypted messages
|
|
|
|
// but not really sure when it should be used vs event.getContent()
|
2020-06-10 20:59:50 +00:00
|
|
|
|
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-04-23 04:04:48 +00:00
|
|
|
const messagesInFlight = [...this.state.messagesInFlight]
|
|
|
|
const placeholderMessageIndex = messagesInFlight.findIndex(msg => msg === message.content.body)
|
|
|
|
if (placeholderMessageIndex > -1) {
|
|
|
|
messagesInFlight.splice(placeholderMessageIndex, 1)
|
|
|
|
this.setState({ messagesInFlight })
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
const decryptionErrors = {...this.state.decryptionErrors}
|
|
|
|
delete decryptionErrors[message.id]
|
2020-06-11 05:54:05 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
messages: {
|
|
|
|
...this.state.messages,
|
2020-09-11 20:32:20 +00:00
|
|
|
[message.id]: message,
|
2020-06-11 05:54:05 +00:00
|
|
|
},
|
|
|
|
decryptionErrors
|
|
|
|
})
|
2020-02-04 05:43:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 23:01:29 +00:00
|
|
|
handleKeyDown = (e) => {
|
2020-03-26 23:13:55 +00:00
|
|
|
switch (e.keyCode) {
|
|
|
|
case 27:
|
|
|
|
if (this.state.emojiSelectorOpen) {
|
|
|
|
this.closeEmojiSelector()
|
|
|
|
} else if (this.state.opened) {
|
|
|
|
this.handleToggleOpen()
|
2020-03-27 03:58:47 +00:00
|
|
|
};
|
|
|
|
default:
|
|
|
|
break;
|
2020-02-14 16:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
setMatrixListeners = client => {
|
2020-07-01 04:50:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
client.on("Room.timeline", (event, room) => {
|
2020-04-22 22:01:58 +00:00
|
|
|
const eventType = event.getType()
|
|
|
|
const content = event.getContent()
|
|
|
|
const sender = event.getSender()
|
|
|
|
|
|
|
|
if (eventType === "m.room.encryption") {
|
2020-04-17 22:04:34 +00:00
|
|
|
this.displayBotMessage({ body: ENCRYPTION_NOTICE }, room.room_id)
|
|
|
|
this.verifyAllRoomDevices(client, room)
|
|
|
|
}
|
2020-02-01 05:30:58 +00:00
|
|
|
|
2020-04-22 22:01:58 +00:00
|
|
|
if (eventType === "m.room.message" && !this.state.isCryptoEnabled) {
|
2020-04-17 22:04:34 +00:00
|
|
|
if (event.isEncrypted()) {
|
|
|
|
return;
|
2020-03-05 03:05:09 +00:00
|
|
|
}
|
2020-04-17 22:04:34 +00:00
|
|
|
this.handleMessageEvent(event)
|
|
|
|
}
|
2020-02-23 17:01:58 +00:00
|
|
|
|
2020-04-22 22:01:58 +00:00
|
|
|
if (eventType === "m.room.member" && content.membership === "invite" && sender === this.props.botId) {
|
2020-04-17 22:04:34 +00:00
|
|
|
this.setState({ facilitatorInvited: true })
|
|
|
|
}
|
2020-02-04 05:43:26 +00:00
|
|
|
|
2020-04-22 22:01:58 +00:00
|
|
|
if (eventType === "m.room.member" && content.membership === "join" && sender !== this.props.botId && sender !== this.state.userId) {
|
2020-04-17 22:04:34 +00:00
|
|
|
this.verifyAllRoomDevices(client, room)
|
2020-04-22 22:01:58 +00:00
|
|
|
this.setState({ facilitatorId: sender, ready: true })
|
2020-06-26 02:20:54 +00:00
|
|
|
window.clearInterval(this.state.waitIntervalId)
|
2020-04-17 22:04:34 +00:00
|
|
|
}
|
|
|
|
});
|
2020-02-23 17:01:58 +00:00
|
|
|
|
2020-04-29 03:43:17 +00:00
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
client.on("Event.decrypted", (event, err) => {
|
|
|
|
if (err) {
|
|
|
|
return this.handleDecryptionError(event, err)
|
|
|
|
}
|
|
|
|
if (event.getType() === "m.room.message") {
|
|
|
|
this.handleMessageEvent(event)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on("RoomMember.typing", (event, member) => {
|
|
|
|
if (member.typing && member.roomId === this.state.roomId) {
|
|
|
|
this.setState({ typingStatus: `${member.name} is typing...` })
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setState({ typingStatus: null })
|
|
|
|
}
|
|
|
|
});
|
2020-09-06 18:13:04 +00:00
|
|
|
|
|
|
|
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 })
|
2022-01-17 20:44:05 +00:00
|
|
|
return this.exitChat(false); // keepg chat state
|
2020-09-11 20:32:20 +00:00
|
|
|
case 'CHAT_OFFLINE':
|
|
|
|
this.displayBotMessage({ body: this.props.chatOfflineMessage })
|
|
|
|
return this.exitChat(false); // keep chat state
|
2020-09-06 18:13:04 +00:00
|
|
|
}
|
2020-04-17 22:04:34 +00:00
|
|
|
}
|
2020-02-01 05:30:58 +00:00
|
|
|
|
2020-04-17 22:04:34 +00:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2020-06-11 05:54:05 +00:00
|
|
|
if ((prevState.messages !== this.state.messages) || (prevState.messagesInFlight !== this.state.messagesInFlight) || (prevState.typingStatus !== this.state.typingStatus)) {
|
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-04-17 22:04:34 +00:00
|
|
|
|
|
|
|
if (!prevState.facilitatorInvited && this.state.facilitatorInvited) {
|
|
|
|
this.displayBotMessage({ body: this.props.confirmationMessage })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prevState.opened && this.state.opened) {
|
|
|
|
this.detectMobile()
|
|
|
|
// not sure what to do with this
|
|
|
|
// this.detectSlowConnection()
|
|
|
|
}
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 16:38:20 +00:00
|
|
|
componentDidMount() {
|
2020-11-28 20:02:55 +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-11-28 20:02:55 +00:00
|
|
|
document.removeEventListener("keydown", this.handleKeyDown, false)
|
2020-02-26 05:12:05 +00:00
|
|
|
window.removeEventListener('beforeunload', this.exitChat)
|
2020-11-28 20:02:55 +00:00
|
|
|
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 })
|
2020-04-22 22:01:58 +00:00
|
|
|
this.startWaitTimeForFacilitator()
|
2020-04-17 22:04:34 +00:00
|
|
|
try {
|
2020-06-11 12:20:49 +00:00
|
|
|
if (this.props.isEncryptionDisabled) {
|
|
|
|
this.initializeUnencryptedChat()
|
|
|
|
} else {
|
|
|
|
this.initializeChat()
|
|
|
|
}
|
2020-04-17 22:04:34 +00:00
|
|
|
} catch(err) {
|
|
|
|
this.handleInitError(err)
|
|
|
|
}
|
2020-03-12 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 22:01:58 +00:00
|
|
|
startWaitTimeForFacilitator = () => {
|
2020-06-26 02:20:54 +00:00
|
|
|
const waitIntervalId = window.setInterval(() => {
|
2020-06-10 20:59:50 +00:00
|
|
|
if (!this.state.facilitatorId && !this.state.ready) {
|
2020-04-23 02:03:38 +00:00
|
|
|
this.displayBotMessage({ body: this.props.waitMessage })
|
2020-04-22 22:01:58 +00:00
|
|
|
}
|
2020-06-26 02:20:54 +00:00
|
|
|
}, this.props.waitInterval)
|
2020-04-22 22:01:58 +00:00
|
|
|
|
2020-09-06 18:13:04 +00:00
|
|
|
this.setState({ waitIntervalId })
|
2020-04-22 22:01:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 17:08:57 +00:00
|
|
|
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-06-10 21:49:19 +00:00
|
|
|
if (this.state.isCryptoEnabled && this.state.client && !(this.state.client.isRoomEncrypted(this.state.roomId) && this.state.client.isCryptoEnabled())) return null;
|
2020-04-23 14:25:24 +00:00
|
|
|
|
2020-02-24 19:16:43 +00:00
|
|
|
if (this.state.client && this.state.roomId) {
|
2020-04-23 04:04:48 +00:00
|
|
|
const messagesInFlight = [...this.state.messagesInFlight]
|
|
|
|
messagesInFlight.push(message)
|
|
|
|
this.setState({ inputValue: "", messagesInFlight }, () => this.sendMessage(message))
|
2020-03-15 04:47:19 +00:00
|
|
|
this.chatboxInput.current.focus()
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:01:29 +00:00
|
|
|
onEmojiClick = (event, emojiObject) => {
|
2020-03-27 03:58:47 +00:00
|
|
|
event.preventDefault()
|
2020-03-25 23:01:29 +00:00
|
|
|
const { emoji } = emojiObject;
|
|
|
|
this.setState({
|
|
|
|
inputValue: this.state.inputValue.concat(emoji),
|
|
|
|
emojiSelectorOpen: false,
|
2020-03-27 03:58:47 +00:00
|
|
|
}, this.chatboxInput.current.focus())
|
2020-03-25 23:01:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 05:30:58 +00:00
|
|
|
render() {
|
2022-01-17 20:44:05 +00:00
|
|
|
console.log(this.props)
|
2020-11-28 20:02:55 +00:00
|
|
|
if (!this.props.enabled || !this.props.isAvailable) {
|
2020-10-20 23:29:12 +00:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2020-04-23 04:04:48 +00:00
|
|
|
const { ready, messages, messagesInFlight, inputValue, userId, roomId, typingStatus, opened, showDock, emojiSelectorOpen, isMobile, decryptionErrors } = this.state;
|
2020-06-11 05:54:05 +00:00
|
|
|
const orderedMessages = Object.values(messages).sort((a,b) => a.timestamp - b.timestamp)
|
2020-02-14 16:38:20 +00:00
|
|
|
const inputLabel = 'Send a message...'
|
2020-06-26 01:56:55 +00:00
|
|
|
const [positionY, positionX] = this.props.position.split(' ')
|
2020-02-01 05:30:58 +00:00
|
|
|
|
|
|
|
return (
|
2020-05-26 13:22:08 +00:00
|
|
|
<div id="safesupport">
|
2020-06-26 01:56:55 +00:00
|
|
|
<div className={`docked-widget size-${this.props.size}`} role="complementary" style={{ [positionY]: '10px', [positionX]: '10px' }}>
|
2020-05-26 13:22:08 +00:00
|
|
|
<Transition in={opened} timeout={250} onExited={this.handleWidgetExit} onEntered={this.handleWidgetEnter}>
|
|
|
|
{(status) => {
|
|
|
|
return (
|
2020-06-26 01:56:55 +00:00
|
|
|
<div className={`widget widget-${status} position-${positionY}`} aria-hidden={!opened}>
|
2020-05-26 13:22:08 +00:00
|
|
|
<div id="safesupport-chatbox" aria-haspopup="dialog">
|
|
|
|
<Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />
|
|
|
|
|
|
|
|
<div className="message-window" ref={this.messageWindow}>
|
|
|
|
<div className="messages">
|
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<div className="text">{ this.props.introMessage }</div>
|
|
|
|
</div>
|
2020-03-12 17:08:57 +00:00
|
|
|
|
2020-05-26 13:22:08 +00:00
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<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>
|
|
|
|
|
2020-05-26 13:22:08 +00:00
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<div className="text">{ this.props.agreementMessage }</div>
|
2020-02-24 04:12:47 +00:00
|
|
|
</div>
|
2020-04-17 22:04:34 +00:00
|
|
|
|
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<div className="text buttons">
|
2020-05-26 13:22:08 +00:00
|
|
|
{`👉`}
|
|
|
|
<button className="btn" id="accept" onClick={this.handleAcceptTerms}>YES</button>
|
|
|
|
<button className="btn" id="reject" onClick={this.handleRejectTerms}>NO</button>
|
2020-04-17 22:04:34 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{
|
2020-06-11 05:54:05 +00:00
|
|
|
orderedMessages.map((message, index) => {
|
2020-05-26 13:22:08 +00:00
|
|
|
return(
|
|
|
|
<Message key={message.id} message={message} userId={userId} botId={this.props.botId} client={this.state.client} />
|
|
|
|
)
|
|
|
|
})
|
2020-04-17 22:04:34 +00:00
|
|
|
}
|
2020-05-26 13:22:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
messagesInFlight.map((message, index) => {
|
|
|
|
return(
|
|
|
|
<Message key={`message-inflight-${index}`} message={{ content: { body: message }}} placeholder={true} />
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{ typingStatus &&
|
|
|
|
<div className="notices">
|
|
|
|
<div role="status">{typingStatus}</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
|
|
|
{ Boolean(Object.keys(decryptionErrors).length) &&
|
|
|
|
<div className={`message from-bot`}>
|
|
|
|
<div className="text buttons">
|
|
|
|
{`Restart chat without encryption?`}
|
2020-06-11 12:20:49 +00:00
|
|
|
<button className="btn" id="accept" onClick={this.restartWithoutCrypto}>RESTART</button>
|
2020-05-26 13:22:08 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
|
|
|
{ !ready && <div className={`loader`}>loading...</div> }
|
2020-03-25 23:01:29 +00:00
|
|
|
</div>
|
2020-05-26 13:22:08 +00:00
|
|
|
</div>
|
|
|
|
<div className="input-window">
|
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
<div className="message-input-container">
|
|
|
|
<input
|
|
|
|
id="message-input"
|
|
|
|
type="text"
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
value={inputValue}
|
|
|
|
aria-label={inputLabel}
|
|
|
|
placeholder={inputLabel}
|
|
|
|
autoFocus={true}
|
|
|
|
ref={this.chatboxInput}
|
|
|
|
/>
|
|
|
|
{
|
|
|
|
(status === "entered") && !isMobile &&
|
|
|
|
<EmojiSelector
|
|
|
|
onEmojiClick={this.onEmojiClick}
|
|
|
|
emojiSelectorOpen={emojiSelectorOpen}
|
|
|
|
toggleEmojiSelector={this.toggleEmojiSelector}
|
|
|
|
closeEmojiSelector={this.closeEmojiSelector}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
<input type="submit" value="Send" id="submit" onClick={this.handleSubmit} />
|
|
|
|
</form>
|
|
|
|
</div>
|
2020-02-24 04:12:47 +00:00
|
|
|
</div>
|
2020-02-23 21:57:16 +00:00
|
|
|
</div>
|
2020-05-26 13:22:08 +00:00
|
|
|
)}
|
|
|
|
}
|
|
|
|
</Transition>
|
2020-10-16 17:43:15 +00:00
|
|
|
{showDock && !roomId && <Dock handleToggleOpen={this.handleToggleOpen} size={this.props.size} label={this.props.dockLabel} />}
|
2020-05-26 13:22:08 +00:00
|
|
|
{showDock && roomId && <Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />}
|
|
|
|
</div>
|
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,
|
2020-10-21 01:04:34 +00:00
|
|
|
displayName: PropTypes.string,
|
2020-04-23 02:03:38 +00:00
|
|
|
waitMessage: PropTypes.string,
|
2020-06-10 20:59:50 +00:00
|
|
|
chatOfflineMessage: PropTypes.string,
|
2020-06-11 12:20:49 +00:00
|
|
|
isEncryptionDisabled: PropTypes.bool,
|
2020-06-26 01:56:55 +00:00
|
|
|
position: PropTypes.oneOf(['top left', 'top right', 'bottom left', 'bottom right']),
|
2020-06-26 02:20:54 +00:00
|
|
|
size: PropTypes.oneOf(['small', 'large']),
|
|
|
|
waitInterval: PropTypes.number,
|
2020-10-16 17:43:15 +00:00
|
|
|
dockLabel: PropTypes.string,
|
2022-01-17 20:44:05 +00:00
|
|
|
enabled: PropTypes.bool,
|
|
|
|
isAvailable: PropTypes.bool
|
2020-02-01 05:30:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:44:05 +00:00
|
|
|
ChatBox.defaultProps = defaultConfig
|
2020-02-01 05:30:58 +00:00
|
|
|
|
|
|
|
export default ChatBox;
|
|
|
|
|