19 Commits

Author SHA1 Message Date
Sharon Kennedy
3107cf6904 1.2.0 2020-06-25 23:04:00 -04:00
Sharon Kennedy
0fcb7e6e07 build and add profiler 2020-06-25 23:03:22 -04:00
Sharon Kennedy
c80e3269c3 configurable max wait time 2020-06-25 22:20:54 -04:00
Sharon Kennedy
5ed83271d9 configurable size and position of chatbox 2020-06-25 21:56:55 -04:00
Sharon Kennedy
706791cc38 1.1.1 2020-06-11 23:09:23 -04:00
Sharon Kennedy
181a4da221 generic chat unavailable message 2020-06-11 23:08:38 -04:00
Sharon Kennedy
46868b141a 1.1.0 2020-06-11 23:03:05 -04:00
Sharon Kennedy
247bc3e2ba 1.1.1 2020-06-11 23:02:14 -04:00
Sharon Kennedy
29b86e4190 1.1.0 2020-06-11 23:02:03 -04:00
Sharon Kennedy
716f61b183 1.1.1 2020-06-11 23:01:43 -04:00
Sharon Kennedy
516a1e0fbd encrypt by default 2020-06-11 22:59:42 -04:00
Sharon Kennedy
80ecf98be1 1.1.0 2020-06-11 11:01:52 -04:00
Sharon Kennedy
f0b43743f7 add flag to disable encryption 2020-06-11 08:31:46 -04:00
Sharon Kennedy
5252321f56 store messages in object instead of array 2020-06-11 01:54:05 -04:00
Sharon Kennedy
93135daaa8 close chat connection if chat is offline 2020-06-10 17:49:19 -04:00
Sharon Kennedy
f27ebb3c71 move offline messaging to chatbox instead of bot 2020-06-10 16:59:50 -04:00
Sharon Kennedy
7d5ad29b60 use rhok server for demo 2020-05-27 22:22:16 -04:00
Sharon Kennedy
27916c9eef 1.0.2 2020-05-26 10:46:17 -04:00
Sharon Kennedy
b5fb1d0a8b fix font sizing in dark mode 2020-05-26 10:46:06 -04:00
12 changed files with 16152 additions and 115 deletions

18
dist/bookmarklet.js vendored

File diff suppressed because one or more lines are too long

18
dist/chatbox.js vendored

File diff suppressed because one or more lines are too long

7
dist/index.html vendored
View File

@@ -13,8 +13,8 @@
<script src="./chatbox.js"></script>
<script>
var config = {
matrixServerUrl: 'https://matrix.safesupport.chat',
botId: '@help-bot:safesupport.chat',
matrixServerUrl: 'https://matrix.rhok.space',
botId: '@help-bot:rhok.space',
roomName: 'Support Chat',
termsUrl: 'https://tosdr.org/',
introMessage: "This chat application does not collect any of your personal data or any data from your use of this service.",
@@ -23,6 +23,9 @@
exitMessage: 'The chat is closed. You may close this window.',
chatUnavailableMessage: 'The chat service is not available right now. Please try again later.',
anonymousDisplayName: 'Anonymous',
position: 'bottom right',
size: 'large',
maxWaitTime: 6000*3,
}
EmbeddableChatbox.mount(config);

15900
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,11 @@
{
"name": "private-safesupport-chatbox",
"version": "1.0.1",
"version": "1.2.0",
"description": "A secure and private embeddable chatbox that connects to Riot",
"main": "dist/chatbox.js",
"scripts": {
"build": "NODE_ENV=production webpack-cli --mode production",
"build:profile": "webpack --mode production --config webpack.config.profile.js",
"start": "webpack-dev-server",
"test": "jest",
"test-update-snapshots": "jest --updateSnapshot",
@@ -121,7 +122,7 @@
"style-loader": "1.1.2",
"wait-for-expect": "^3.0.2",
"webpack": "4.41.5",
"webpack-bundle-analyzer": "^3.7.0",
"webpack-bundle-analyzer": "^3.8.0",
"webpack-cli": "3.3.10",
"webpack-dev-server": "3.10.1",
"webpack-obfuscator": "0.22.0",

View File

@@ -13,8 +13,8 @@
<script src="./chatbox.js"></script>
<script>
var config = {
matrixServerUrl: 'https://matrix.safesupport.chat',
botId: '@help-bot:safesupport.chat',
matrixServerUrl: 'https://matrix.rhok.space',
botId: '@help-bot:rhok.space',
roomName: 'Support Chat',
termsUrl: 'https://tosdr.org/',
introMessage: "This chat application does not collect any of your personal data or any data from your use of this service.",
@@ -23,6 +23,9 @@
exitMessage: 'The chat is closed. You may close this window.',
chatUnavailableMessage: 'The chat service is not available right now. Please try again later.',
anonymousDisplayName: 'Anonymous',
position: 'bottom right',
size: 'large',
maxWaitTime: 6000*3,
}
EmbeddableChatbox.mount(config);

View File

@@ -26,14 +26,56 @@
}
}
@keyframes slideInDown {
from {
transform: translate3d(0, -100%, 0);
display: inherit;
visibility: visible;
}
to {
transform: translate3d(0, 0, 0);
}
}
@keyframes slideOutUp {
from {
transform: translate3d(0, 0, 0);
}
to {
display: none;
visibility: hidden;
transform: translate3d(0, -100%, 0);
}
}
.docked-widget {
position: fixed;
bottom: 10px;
right: 10px;
z-index: 9999;
width: 400px;
max-width: 100vw;
font-size: $base-font-size;
&.size-large {
width: 400px;
.dock {
width: 400px;
}
}
&.size-small {
#open-chatbox-label {
display: flex;
align-items: center;
.icon {
margin-left: 0.5em;
font-size: 1.2em;
}
}
}
}
.dock {
@@ -41,7 +83,6 @@
display: flex;
align-items: center;
justify-content: space-between;
width: 400px;
max-width: calc(100vw - 10px);
color: $white;
font-family: $theme-font;
@@ -53,6 +94,7 @@
background-color: transparent;
padding: 5px;
#open-chatbox-label {
background: $theme-color;
padding: 0.75em;
@@ -105,6 +147,10 @@
&-entering {
animation-name: slideInUp;
&.position-top {
animation-name: slideInDown;
}
}
&-entered {
display: inherit;
@@ -112,6 +158,10 @@
}
&-exiting {
animation-name: slideOutDown;
&.position-top {
animation-name: slideOutUp;
}
}
&-exited {
display: none;

View File

@@ -68,7 +68,6 @@
.message {
&.from-bot {
color: transparentize($light-text-color, 0.3);
font-size: 0.9rem;
}
&.from-me {

View File

@@ -24,8 +24,7 @@ const ENCRYPTION_CONFIG = { "algorithm": "m.megolm.v1.aes-sha2" };
const ENCRYPTION_NOTICE = "Messages in this chat are secured with end-to-end encryption."
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 +36,13 @@ 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 = "All of the chat facilitators are currently offline."
const DEFAULT_WAIT_MESSAGE = "Please be patient, our online facilitators are currently responding to other support requests."
const DEFAULT_ENCRYPTION_DISABLED = false
const DEFAULT_POSITION = 'bottom right'
const DEFAULT_SIZE = 'large'
const DEFAULT_MAX_WAIT_MS = 600000 // 10 minutes
const DEFAULT_WAIT_INTERVAL_MS = 120000 // 2 minutes
class ChatBox extends React.Component {
@@ -52,7 +57,7 @@ class ChatBox extends React.Component {
userId: null,
password: null,
localStorage: null,
messages: [],
messages: {},
inputValue: "",
errors: [],
roomId: null,
@@ -144,27 +149,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 +241,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 +291,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 +315,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 +390,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 +432,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 +453,42 @@ 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()
}
this.setState({ messages, decryptionErrors })
this.setState({
messages: {
...this.state.messages,
[message.id]: newMessage,
},
decryptionErrors
})
}
handleChatOffline = () => {
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
}
handleKeyDown = (e) => {
switch (e.keyCode) {
@@ -482,7 +528,8 @@ class ChatBox extends React.Component {
if (eventType === "m.room.member" && content.membership === "join" && sender !== this.props.botId && sender !== this.state.userId) {
this.verifyAllRoomDevices(client, room)
this.setState({ facilitatorId: sender, ready: true })
window.clearInterval(this.state.timeoutId)
window.clearInterval(this.state.waitIntervalId)
window.clearInterval(this.state.waitTimeoutId)
}
});
@@ -492,12 +539,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 +554,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,20 +590,31 @@ 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)
}
}
startWaitTimeForFacilitator = () => {
const timeoutId = window.setInterval(() => {
if (!this.state.facilitatorId) {
const waitIntervalId = window.setInterval(() => {
if (!this.state.facilitatorId && !this.state.ready) {
this.displayBotMessage({ body: this.props.waitMessage })
}
}, WAIT_TIME_MS)
}, this.props.waitInterval)
this.setState({ timeoutId })
const waitTimeoutId = window.setTimeout(() => {
if (!this.state.facilitatorId && !this.state.ready) {
this.displayBotMessage({ body: this.props.chatUnavailableMessage })
this.handleChatOffline()
}
}, this.props.maxWaitTime)
this.setState({ waitIntervalId, waitTimeoutId })
}
handleRejectTerms = () => {
@@ -575,7 +627,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,15 +648,17 @@ 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...'
const [positionY, positionX] = this.props.position.split(' ')
return (
<div id="safesupport">
<div className="docked-widget" role="complementary">
<div className={`docked-widget size-${this.props.size}`} role="complementary" style={{ [positionY]: '10px', [positionX]: '10px' }}>
<Transition in={opened} timeout={250} onExited={this.handleWidgetExit} onEntered={this.handleWidgetEnter}>
{(status) => {
return (
<div className={`widget widget-${status}`} aria-hidden={!opened}>
<div className={`widget widget-${status} position-${positionY}`} aria-hidden={!opened}>
<div id="safesupport-chatbox" aria-haspopup="dialog">
<Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />
@@ -631,7 +685,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 +710,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>
}
@@ -695,7 +749,7 @@ class ChatBox extends React.Component {
)}
}
</Transition>
{showDock && !roomId && <Dock handleToggleOpen={this.handleToggleOpen} />}
{showDock && !roomId && <Dock handleToggleOpen={this.handleToggleOpen} size={this.props.size} />}
{showDock && roomId && <Header handleToggleOpen={this.handleToggleOpen} opened={opened} handleExitChat={this.handleExitChat} />}
</div>
</div>
@@ -715,6 +769,12 @@ ChatBox.propTypes = {
chatUnavailableMessage: PropTypes.string,
anonymousDisplayName: PropTypes.string,
waitMessage: PropTypes.string,
chatOfflineMessage: PropTypes.string,
isEncryptionDisabled: PropTypes.bool,
position: PropTypes.oneOf(['top left', 'top right', 'bottom left', 'bottom right']),
size: PropTypes.oneOf(['small', 'large']),
maxWaitTime: PropTypes.number,
waitInterval: PropTypes.number,
}
ChatBox.defaultProps = {
@@ -729,6 +789,12 @@ 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,
position: DEFAULT_POSITION,
size: DEFAULT_SIZE,
maxWaitTime: DEFAULT_MAX_WAIT_MS,
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
}
export default ChatBox;

View File

@@ -1,8 +1,7 @@
import React from "react"
import React, { Fragment } from "react"
import PropTypes from "prop-types"
const Dock = ({ handleToggleOpen }) => {
const Dock = ({ handleToggleOpen, size }) => {
return(
<button
type="button"
@@ -11,10 +10,19 @@ const Dock = ({ handleToggleOpen }) => {
onKeyPress={handleToggleOpen}
aria-labelledby="open-chatbox-label"
>
<div id="open-chatbox-label">Start a new chat</div>
<div className="label-icon">
<div className={`btn-icon`} aria-label={`Open support chat window`}>+</div>
</div>
{
size === 'small' ?
<div id="open-chatbox-label">
<span>Chat</span><span className="icon">+</span>
</div>
:
<Fragment>
<div id="open-chatbox-label">Start a new chat</div>
<div className="label-icon">
<div className={`btn-icon`} aria-label={`Open support chat window`}>+</div>
</div>
</Fragment>
}
</button>
)
}

View File

@@ -1,17 +1,17 @@
import EmbeddableChatbox from './embeddable-chatbox';
const config = {
matrixServerUrl: 'https://matrix.safesupport.chat',
botId: '@help-bot:safesupport.chat',
roomName: 'Support Chat',
termsUrl: 'https://tosdr.org/',
introMessage: "This chat application does not collect any of your personal data or any data from your use of this service.",
agreementMessage: 'Do you want to continue?',
confirmationMessage: 'Waiting for a facilitator to join the chat...',
exitMessage: 'The chat is closed. You may close this window.',
chatUnavailableMessage: 'The chat service is not available right now. Please try again later.',
anonymousDisplayName: 'Anonymous',
}
matrixServerUrl: 'https://matrix.rhok.space',
botId: '@help-bot:rhok.space',
roomName: 'Support Chat',
termsUrl: 'https://tosdr.org/',
introMessage: 'This chat application does not collect any of your personal data or any data from your use of this service.',
agreementMessage: 'Do you want to continue?',
confirmationMessage: 'Waiting for a facilitator to join the chat...',
exitMessage: 'The chat is closed. You may close this window.',
chatUnavailableMessage: 'The chat service is not available right now. Please try again later.',
anonymousDisplayName: 'Anonymous',
};
export default function bookmarklet() {
if (window.EmbeddableChatbox) {

View File

@@ -0,0 +1,7 @@
const config = require('./webpack.config');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
config[0].plugins = config[0].plugins.concat([new BundleAnalyzerPlugin({ analyzerPort: 5555 })]);
module.exports = config;