Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
977fe1afa5 | ||
|
|
bf29508e8c | ||
|
|
40e968fcee | ||
|
|
c45182a574 | ||
|
|
819d9b8ebb | ||
|
|
4491b681c5 | ||
|
|
dcd7718f8f | ||
|
|
c8c42e9086 | ||
|
|
004f88d5cd | ||
|
|
22c17d314d | ||
|
|
2b3e46ad53 |
@@ -45,7 +45,6 @@ Options:
|
|||||||
| `chatOfflineMessage` (optional) | Text to show if there is no-one online respond | `All of the chat facilitators are currently offline.` |
|
| `chatOfflineMessage` (optional) | Text to show if there is no-one online respond | `All of the chat facilitators are currently offline.` |
|
||||||
| `size` (optional) | The size of the start button. Can be 'small' or 'large' | `large` |
|
| `size` (optional) | The size of the start button. Can be 'small' or 'large' | `large` |
|
||||||
| `position` (optional) | The position of the start button. Can be 'top left', 'top right', 'bottom left', 'bottom right'. | `bottom right` |
|
| `position` (optional) | The position of the start button. Can be 'top left', 'top right', 'bottom left', 'bottom right'. | `bottom right` |
|
||||||
| `maxWaitTime` (optional) | The maximum time (in ms) the chatbox will wait for someone to join before closing the chat and displaying the chat unavailable message | 600000 |
|
|
||||||
| `waitInterval` (optional) | The interval (in ms) at which the bot sends the wait message | 120000 |
|
| `waitInterval` (optional) | The interval (in ms) at which the bot sends the wait message | 120000 |
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
10
dist/bookmarklet.js
vendored
10
dist/bookmarklet.js
vendored
File diff suppressed because one or more lines are too long
10
dist/chatbox.js
vendored
10
dist/chatbox.js
vendored
File diff suppressed because one or more lines are too long
2
dist/component.js
vendored
2
dist/component.js
vendored
File diff suppressed because one or more lines are too long
5
dist/index.html
vendored
5
dist/index.html
vendored
@@ -13,9 +13,8 @@
|
|||||||
<script src="./chatbox.js"></script>
|
<script src="./chatbox.js"></script>
|
||||||
<script>
|
<script>
|
||||||
var config = {
|
var config = {
|
||||||
settingsEndpoint: 'https://safesupport-admin.herokuapp.com/api/get-settings'
|
matrixServerUrl: 'https://matrix.safesupport.chat'
|
||||||
}
|
};
|
||||||
|
|
||||||
EmbeddableChatbox.mount(config);
|
EmbeddableChatbox.mount(config);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "private-safesupport-chatbox",
|
"name": "private-safesupport-chatbox",
|
||||||
"version": "2.2.0",
|
"version": "2.3.2",
|
||||||
"description": "A secure and private embeddable chatbox that connects to Riot",
|
"description": "A secure and private embeddable chatbox that connects to Riot",
|
||||||
"main": "dist/chatbox.js",
|
"main": "dist/chatbox.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -13,9 +13,8 @@
|
|||||||
<script src="./chatbox.js"></script>
|
<script src="./chatbox.js"></script>
|
||||||
<script>
|
<script>
|
||||||
var config = {
|
var config = {
|
||||||
settingsEndpoint: 'https://safesupport-admin.herokuapp.com/api/get-settings'
|
matrixServerUrl: 'https://matrix.safesupport.chat'
|
||||||
}
|
};
|
||||||
|
|
||||||
EmbeddableChatbox.mount(config);
|
EmbeddableChatbox.mount(config);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,33 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import Chatbox from './chatbox';
|
import Chatbox from './chatbox';
|
||||||
|
import { DEFAULT_SETTINGS_ENDPOINT } from '../utils/constants';
|
||||||
|
|
||||||
|
const ChatboxWithSettings = ({ settingsEndpoint = null, matrixServerUrl, ...rest }) => {
|
||||||
const ChatboxWithSettings = ({ settingsEndpoint, ...rest }) => {
|
|
||||||
const [settings, setSettings] = useState({});
|
const [settings, setSettings] = useState({});
|
||||||
|
const [shifts, setShifts] = useState();
|
||||||
const settingsObj = {};
|
const [isAvailable, setAvailability] = useState(false);
|
||||||
|
|
||||||
const getSettings = async () => {
|
const getSettings = async () => {
|
||||||
if (!settingsEndpoint) {
|
const endpoint = settingsEndpoint || DEFAULT_SETTINGS_ENDPOINT;
|
||||||
|
const url = `${endpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
|
||||||
|
const res = await fetch(url);
|
||||||
|
const data = await res.json();
|
||||||
|
const { fields, schedule = [] } = data;
|
||||||
|
|
||||||
|
if (!fields) {
|
||||||
const props = {
|
const props = {
|
||||||
...rest,
|
...rest,
|
||||||
enabled: true
|
enabled: true,
|
||||||
}
|
isAvailable: true,
|
||||||
|
};
|
||||||
|
|
||||||
return setSettings(props);
|
return setSettings(props);
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await fetch(settingsEndpoint);
|
const settingsObj = {};
|
||||||
const data = await res.json();
|
|
||||||
const { fields } = data;
|
setShifts(schedule);
|
||||||
|
|
||||||
Object.entries(fields).forEach(([k, v]) => {
|
Object.entries(fields).forEach(([k, v]) => {
|
||||||
const [scope, key] = k.split('_');
|
const [scope, key] = k.split('_');
|
||||||
@@ -37,17 +44,55 @@ const ChatboxWithSettings = ({ settingsEndpoint, ...rest }) => {
|
|||||||
return setSettings(settingsObj);
|
return setSettings(settingsObj);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const checkSchedule = () => {
|
||||||
|
if (shifts.length === 0) {
|
||||||
|
setAvailability(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
const weekday = weekdays[now.getDay()];
|
||||||
|
const hours = now.getHours();
|
||||||
|
const minutes = now.getMinutes();
|
||||||
|
|
||||||
|
shifts.map(async (shift) => {
|
||||||
|
const [startHours, startMinutes] = shift.start.split(':').map((t) => Number(t));
|
||||||
|
const [endHours, endMinutes] = shift.end.split(':').map((t) => Number(t));
|
||||||
|
if (
|
||||||
|
shift.service === 'web'
|
||||||
|
&& shift.weekday === weekday
|
||||||
|
&& ((startHours < hours) || (startHours === hours && startMinutes <= minutes))
|
||||||
|
&& ((endHours > hours) || (endHours === hours && endMinutes > minutes))
|
||||||
|
) {
|
||||||
|
setAvailability(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getSettings();
|
getSettings();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (shifts) {
|
||||||
|
checkSchedule();
|
||||||
|
}
|
||||||
|
}, [shifts]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Chatbox {...settings} {...rest} />
|
/* eslint-disable react/jsx-props-no-spreading */
|
||||||
|
<Chatbox matrixServerUrl={matrixServerUrl} isAvailable={isAvailable} {...settings} {...rest} />
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
ChatboxWithSettings.propTypes = {
|
ChatboxWithSettings.propTypes = {
|
||||||
|
matrixServerUrl: PropTypes.string.isRequired,
|
||||||
settingsEndpoint: PropTypes.string,
|
settingsEndpoint: PropTypes.string,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ChatboxWithSettings.defaultProps = {
|
||||||
|
settingsEndpoint: null,
|
||||||
|
};
|
||||||
|
|
||||||
export default ChatboxWithSettings;
|
export default ChatboxWithSettings;
|
||||||
|
|||||||
@@ -34,14 +34,13 @@ const DEFAULT_INTRO_MESSAGE = "This chat application does not collect any of you
|
|||||||
const DEFAULT_AGREEMENT_MESSAGE = "Do you want to continue?"
|
const DEFAULT_AGREEMENT_MESSAGE = "Do you want to continue?"
|
||||||
const DEFAULT_CONFIRMATION_MESSAGE = "Waiting for a facilitator to join the chat..."
|
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_EXIT_MESSAGE = "The chat is closed. You may close this window."
|
||||||
const DEFAULT_ANONYMOUS_DISPLAY_NAME="Anonymous"
|
const DEFAULT_DISPLAY_NAME="Anonymous"
|
||||||
const DEFAULT_CHAT_UNAVAILABLE_MESSAGE = "The chat service is not available right now. Please try again later."
|
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_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_WAIT_MESSAGE = "Please be patient, our online facilitators are currently responding to other support requests."
|
||||||
const DEFAULT_ENCRYPTION_DISABLED = false
|
const DEFAULT_ENCRYPTION_DISABLED = false
|
||||||
const DEFAULT_POSITION = 'bottom right'
|
const DEFAULT_POSITION = 'bottom right'
|
||||||
const DEFAULT_SIZE = 'large'
|
const DEFAULT_SIZE = 'large'
|
||||||
const DEFAULT_MAX_WAIT_MS = 600000 // 10 minutes
|
|
||||||
const DEFAULT_WAIT_INTERVAL_MS = 120000 // 2 minutes
|
const DEFAULT_WAIT_INTERVAL_MS = 120000 // 2 minutes
|
||||||
const DEFAULT_DOCK_LABEL = 'Start a new chat'
|
const DEFAULT_DOCK_LABEL = 'Start a new chat'
|
||||||
const DEFAULT_ENABLED = false
|
const DEFAULT_ENABLED = false
|
||||||
@@ -69,7 +68,7 @@ class ChatBox extends React.Component {
|
|||||||
isMobile: true,
|
isMobile: true,
|
||||||
isSlowConnection: true,
|
isSlowConnection: true,
|
||||||
decryptionErrors: {},
|
decryptionErrors: {},
|
||||||
messagesInFlight: [],
|
messagesInFlight: []
|
||||||
}
|
}
|
||||||
this.state = this.initialState
|
this.state = this.initialState
|
||||||
this.chatboxInput = React.createRef();
|
this.chatboxInput = React.createRef();
|
||||||
@@ -152,23 +151,28 @@ class ChatBox extends React.Component {
|
|||||||
|
|
||||||
exitChat = async (resetState=true) => {
|
exitChat = async (resetState=true) => {
|
||||||
if (this.state.client) {
|
if (this.state.client) {
|
||||||
await this.state.client.leave(this.state.roomId)
|
try {
|
||||||
|
await this.state.client.leave(this.state.roomId)
|
||||||
|
|
||||||
const auth = {
|
const auth = {
|
||||||
type: 'm.login.password',
|
type: 'm.login.password',
|
||||||
user: this.state.userId,
|
user: this.state.userId,
|
||||||
identifier: {
|
identifier: {
|
||||||
type: "m.id.user",
|
type: "m.id.user",
|
||||||
user: this.state.userId,
|
user: this.state.userId,
|
||||||
},
|
},
|
||||||
password: this.state.password,
|
password: this.state.password,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.state.client.deactivateAccount(auth, true)
|
await this.state.client.deactivateAccount(auth, true)
|
||||||
await this.state.client.stopClient()
|
await this.state.client.stopClient()
|
||||||
await this.state.client.clearStores()
|
await this.state.client.clearStores()
|
||||||
this.setState({ client: null, ready: true }) // no more loading animation
|
} catch (err) {
|
||||||
window.clearInterval(this.state.waitIntervalId) // no more waiting messages
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.state.localStorage) {
|
if (this.state.localStorage) {
|
||||||
@@ -247,7 +251,7 @@ class ChatBox extends React.Component {
|
|||||||
client.once('sync', async (state, prevState, data) => {
|
client.once('sync', async (state, prevState, data) => {
|
||||||
if (state === "PREPARED") {
|
if (state === "PREPARED") {
|
||||||
this.setState({ client })
|
this.setState({ client })
|
||||||
client.setDisplayName(this.props.anonymousDisplayName)
|
client.setDisplayName(this.props.displayName)
|
||||||
this.setMatrixListeners(client)
|
this.setMatrixListeners(client)
|
||||||
await this.createRoom(client)
|
await this.createRoom(client)
|
||||||
}
|
}
|
||||||
@@ -289,7 +293,7 @@ class ChatBox extends React.Component {
|
|||||||
if (state === "PREPARED") {
|
if (state === "PREPARED") {
|
||||||
try {
|
try {
|
||||||
this.setState({ client })
|
this.setState({ client })
|
||||||
client.setDisplayName(this.props.anonymousDisplayName)
|
client.setDisplayName(this.props.displayName)
|
||||||
this.setMatrixListeners(client)
|
this.setMatrixListeners(client)
|
||||||
await this.createRoom(client)
|
await this.createRoom(client)
|
||||||
this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
|
this.displayBotMessage({ body: UNENCRYPTION_NOTICE })
|
||||||
@@ -308,7 +312,7 @@ class ChatBox extends React.Component {
|
|||||||
await client.startClient()
|
await client.startClient()
|
||||||
|
|
||||||
client.once('sync', async (state, prevState, data) => {
|
client.once('sync', async (state, prevState, data) => {
|
||||||
client.setDisplayName(this.props.anonymousDisplayName)
|
client.setDisplayName(this.props.displayName)
|
||||||
this.setMatrixListeners(client)
|
this.setMatrixListeners(client)
|
||||||
await this.createRoom(client)
|
await this.createRoom(client)
|
||||||
})
|
})
|
||||||
@@ -587,14 +591,14 @@ class ChatBox extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
document.addEventListener("keydown", this.handleKeyDown, false);
|
document.addEventListener("keydown", this.handleKeyDown, false)
|
||||||
window.addEventListener('beforeunload', this.exitChat)
|
window.addEventListener('beforeunload', this.exitChat)
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
document.removeEventListener("keydown", this.handleKeyDown, false);
|
document.removeEventListener("keydown", this.handleKeyDown, false)
|
||||||
window.removeEventListener('beforeunload', this.exitChat)
|
window.removeEventListener('beforeunload', this.exitChat)
|
||||||
this.exitChat();
|
this.exitChat()
|
||||||
}
|
}
|
||||||
|
|
||||||
handleInputChange = e => {
|
handleInputChange = e => {
|
||||||
@@ -655,7 +659,7 @@ class ChatBox extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (!this.props.enabled) {
|
if (!this.props.enabled || !this.props.isAvailable) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -779,16 +783,15 @@ ChatBox.propTypes = {
|
|||||||
confirmationMessage: PropTypes.string,
|
confirmationMessage: PropTypes.string,
|
||||||
exitMessage: PropTypes.string,
|
exitMessage: PropTypes.string,
|
||||||
chatUnavailableMessage: PropTypes.string,
|
chatUnavailableMessage: PropTypes.string,
|
||||||
anonymousDisplayName: PropTypes.string,
|
displayName: PropTypes.string,
|
||||||
waitMessage: PropTypes.string,
|
waitMessage: PropTypes.string,
|
||||||
chatOfflineMessage: PropTypes.string,
|
chatOfflineMessage: PropTypes.string,
|
||||||
isEncryptionDisabled: PropTypes.bool,
|
isEncryptionDisabled: PropTypes.bool,
|
||||||
position: PropTypes.oneOf(['top left', 'top right', 'bottom left', 'bottom right']),
|
position: PropTypes.oneOf(['top left', 'top right', 'bottom left', 'bottom right']),
|
||||||
size: PropTypes.oneOf(['small', 'large']),
|
size: PropTypes.oneOf(['small', 'large']),
|
||||||
maxWaitTime: PropTypes.number,
|
|
||||||
waitInterval: PropTypes.number,
|
waitInterval: PropTypes.number,
|
||||||
dockLabel: PropTypes.string,
|
dockLabel: PropTypes.string,
|
||||||
enabled: PropTypes.bool,
|
enabled: PropTypes.bool
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatBox.defaultProps = {
|
ChatBox.defaultProps = {
|
||||||
@@ -800,14 +803,13 @@ ChatBox.defaultProps = {
|
|||||||
agreementMessage: DEFAULT_AGREEMENT_MESSAGE,
|
agreementMessage: DEFAULT_AGREEMENT_MESSAGE,
|
||||||
confirmationMessage: DEFAULT_CONFIRMATION_MESSAGE,
|
confirmationMessage: DEFAULT_CONFIRMATION_MESSAGE,
|
||||||
exitMessage: DEFAULT_EXIT_MESSAGE,
|
exitMessage: DEFAULT_EXIT_MESSAGE,
|
||||||
anonymousDisplayName: DEFAULT_ANONYMOUS_DISPLAY_NAME,
|
displayName: DEFAULT_DISPLAY_NAME,
|
||||||
chatUnavailableMessage: DEFAULT_CHAT_UNAVAILABLE_MESSAGE,
|
chatUnavailableMessage: DEFAULT_CHAT_UNAVAILABLE_MESSAGE,
|
||||||
waitMessage: DEFAULT_WAIT_MESSAGE,
|
waitMessage: DEFAULT_WAIT_MESSAGE,
|
||||||
chatOfflineMessage: DEFAULT_CHAT_OFFLINE_MESSAGE,
|
chatOfflineMessage: DEFAULT_CHAT_OFFLINE_MESSAGE,
|
||||||
isEncryptionDisabled: DEFAULT_ENCRYPTION_DISABLED,
|
isEncryptionDisabled: DEFAULT_ENCRYPTION_DISABLED,
|
||||||
position: DEFAULT_POSITION,
|
position: DEFAULT_POSITION,
|
||||||
size: DEFAULT_SIZE,
|
size: DEFAULT_SIZE,
|
||||||
maxWaitTime: DEFAULT_MAX_WAIT_MS,
|
|
||||||
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
|
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
|
||||||
dockLabel: DEFAULT_DOCK_LABEL,
|
dockLabel: DEFAULT_DOCK_LABEL,
|
||||||
enabled: DEFAULT_ENABLED,
|
enabled: DEFAULT_ENABLED,
|
||||||
|
|||||||
1
src/utils/constants.js
Normal file
1
src/utils/constants.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export const DEFAULT_SETTINGS_ENDPOINT = 'https://admin.safesupport.chat/api/get-settings';
|
||||||
Reference in New Issue
Block a user