2020-10-05 04:43:31 +03:00
|
|
|
import { h, Component } from '/js/web_modules/preact.js';
|
|
|
|
import htm from '/js/web_modules/htm.js';
|
2020-08-19 10:16:35 +03:00
|
|
|
const html = htm.bind(h);
|
2021-08-01 02:21:30 +03:00
|
|
|
import UsernameForm from './components/chat/username.js';
|
2020-08-24 05:37:06 +03:00
|
|
|
import Chat from './components/chat/chat.js';
|
2021-08-01 02:21:30 +03:00
|
|
|
import Websocket, {
|
|
|
|
CALLBACKS,
|
|
|
|
SOCKET_MESSAGE_TYPES,
|
|
|
|
} from './utils/websocket.js';
|
2021-07-20 05:22:29 +03:00
|
|
|
import { registerChat } from './chat/register.js';
|
2021-08-01 02:22:00 +03:00
|
|
|
import { getLocalStorage, setLocalStorage } from './utils/helpers.js';
|
2021-08-01 02:21:30 +03:00
|
|
|
import {
|
|
|
|
CHAT_MAX_MESSAGE_LENGTH,
|
|
|
|
EST_SOCKET_PAYLOAD_BUFFER,
|
|
|
|
KEY_EMBED_CHAT_ACCESS_TOKEN,
|
|
|
|
KEY_ACCESS_TOKEN,
|
|
|
|
KEY_USERNAME,
|
|
|
|
TIMER_DISABLE_CHAT_AFTER_OFFLINE,
|
|
|
|
URL_STATUS,
|
|
|
|
URL_CONFIG,
|
|
|
|
TIMER_STATUS_UPDATE,
|
|
|
|
} from './utils/constants.js';
|
2022-03-07 04:11:51 +03:00
|
|
|
import { URL_WEBSOCKET } from './utils/constants.js';
|
2021-08-01 02:21:30 +03:00
|
|
|
|
2020-08-14 14:19:19 +03:00
|
|
|
export default class StandaloneChat extends Component {
|
2020-08-13 11:28:25 +03:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
2021-08-01 02:21:30 +03:00
|
|
|
websocket: null,
|
|
|
|
canChat: false,
|
2020-08-13 11:28:25 +03:00
|
|
|
chatEnabled: true, // always true for standalone chat
|
2021-08-01 02:21:30 +03:00
|
|
|
chatInputEnabled: false, // chat input box state
|
|
|
|
accessToken: null,
|
2021-07-20 05:22:29 +03:00
|
|
|
username: null,
|
2021-08-01 02:21:30 +03:00
|
|
|
isRegistering: false,
|
2021-08-16 04:22:13 +03:00
|
|
|
streamOnline: null, // stream is active/online
|
2021-08-01 02:21:30 +03:00
|
|
|
lastDisconnectTime: null,
|
|
|
|
configData: {
|
|
|
|
loading: true,
|
|
|
|
},
|
2020-08-13 11:28:25 +03:00
|
|
|
};
|
2021-08-01 02:21:30 +03:00
|
|
|
this.disableChatInputTimer = null;
|
2021-07-20 05:22:29 +03:00
|
|
|
this.hasConfiguredChat = false;
|
2021-08-01 02:21:30 +03:00
|
|
|
|
2020-08-13 11:28:25 +03:00
|
|
|
this.handleUsernameChange = this.handleUsernameChange.bind(this);
|
2021-08-01 02:21:30 +03:00
|
|
|
this.handleOfflineMode = this.handleOfflineMode.bind(this);
|
|
|
|
this.handleOnlineMode = this.handleOnlineMode.bind(this);
|
|
|
|
this.handleFormFocus = this.handleFormFocus.bind(this);
|
|
|
|
this.handleFormBlur = this.handleFormBlur.bind(this);
|
|
|
|
this.getStreamStatus = this.getStreamStatus.bind(this);
|
|
|
|
this.getConfig = this.getConfig.bind(this);
|
|
|
|
this.disableChatInput = this.disableChatInput.bind(this);
|
|
|
|
this.setupChatAuth = this.setupChatAuth.bind(this);
|
|
|
|
this.disableChat = this.disableChat.bind(this);
|
|
|
|
|
2022-03-07 04:11:51 +03:00
|
|
|
this.socketHostOverride = null;
|
|
|
|
|
2021-08-01 02:21:30 +03:00
|
|
|
// user events
|
|
|
|
this.handleWebsocketMessage = this.handleWebsocketMessage.bind(this);
|
|
|
|
|
|
|
|
this.getConfig();
|
|
|
|
|
|
|
|
this.getStreamStatus();
|
|
|
|
this.statusTimer = setInterval(this.getStreamStatus, TIMER_STATUS_UPDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch /config data
|
|
|
|
getConfig() {
|
|
|
|
fetch(URL_CONFIG)
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network response was not ok ${response.ok}`);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((json) => {
|
|
|
|
this.setConfigData(json);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.handleNetworkingError(`Fetch config: ${error}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch stream status
|
|
|
|
getStreamStatus() {
|
|
|
|
fetch(URL_STATUS)
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network response was not ok ${response.ok}`);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((json) => {
|
|
|
|
this.updateStreamStatus(json);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.handleOfflineMode();
|
|
|
|
this.handleNetworkingError(`Stream status: ${error}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setConfigData(data = {}) {
|
2022-03-07 04:11:51 +03:00
|
|
|
const { chatDisabled, socketHostOverride } = data;
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
// If this is the first time setting the config
|
|
|
|
// then setup chat if it's enabled.
|
2021-11-12 02:39:56 +03:00
|
|
|
if (!this.hasConfiguredChat && !chatDisabled) {
|
2021-07-20 05:22:29 +03:00
|
|
|
this.setupChatAuth();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hasConfiguredChat = true;
|
2022-03-07 04:11:51 +03:00
|
|
|
this.socketHostOverride = socketHostOverride;
|
2021-08-01 02:21:30 +03:00
|
|
|
this.setState({
|
2021-11-12 02:39:56 +03:00
|
|
|
canChat: !chatDisabled,
|
2021-08-01 02:21:30 +03:00
|
|
|
configData: {
|
|
|
|
...data,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle UI things from stream status result
|
|
|
|
updateStreamStatus(status = {}) {
|
|
|
|
const { streamOnline: curStreamOnline } = this.state;
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-01 02:22:00 +03:00
|
|
|
const { online, lastDisconnectTime } = status;
|
2021-08-01 02:21:30 +03:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
lastDisconnectTime,
|
|
|
|
streamOnline: online,
|
|
|
|
});
|
2021-08-16 04:22:13 +03:00
|
|
|
|
|
|
|
if (status.online !== curStreamOnline) {
|
|
|
|
if (status.online) {
|
|
|
|
// stream has just come online.
|
|
|
|
this.handleOnlineMode();
|
|
|
|
} else {
|
|
|
|
// stream has just flipped offline or app just got loaded and stream is offline.
|
|
|
|
this.handleOfflineMode(lastDisconnectTime);
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 02:21:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop status timer and disable chat after some time.
|
2021-08-16 04:22:13 +03:00
|
|
|
handleOfflineMode(lastDisconnectTime) {
|
|
|
|
if (lastDisconnectTime) {
|
|
|
|
const remainingChatTime =
|
|
|
|
TIMER_DISABLE_CHAT_AFTER_OFFLINE -
|
|
|
|
(Date.now() - new Date(lastDisconnectTime));
|
|
|
|
const countdown = remainingChatTime < 0 ? 0 : remainingChatTime;
|
|
|
|
if (countdown > 0) {
|
|
|
|
this.setState({
|
|
|
|
chatInputEnabled: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.disableChatInputTimer = setTimeout(this.disableChatInput, countdown);
|
|
|
|
}
|
2021-08-01 02:21:30 +03:00
|
|
|
this.setState({
|
|
|
|
streamOnline: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOnlineMode() {
|
|
|
|
clearTimeout(this.disableChatInputTimer);
|
|
|
|
this.disableChatInputTimer = null;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
streamOnline: true,
|
|
|
|
chatInputEnabled: true,
|
|
|
|
});
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:33:55 +03:00
|
|
|
handleUsernameChange(newName) {
|
2020-08-13 11:28:25 +03:00
|
|
|
this.setState({
|
|
|
|
username: newName,
|
|
|
|
});
|
2021-08-01 02:21:30 +03:00
|
|
|
this.sendUsernameChange(newName);
|
|
|
|
}
|
|
|
|
|
|
|
|
disableChatInput() {
|
|
|
|
this.setState({
|
|
|
|
chatInputEnabled: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNetworkingError(error) {
|
|
|
|
console.error(`>>> App Error: ${error}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWebsocketMessage(e) {
|
|
|
|
if (e.type === SOCKET_MESSAGE_TYPES.ERROR_USER_DISABLED) {
|
|
|
|
// User has been actively disabled on the backend. Turn off chat for them.
|
|
|
|
this.handleBlockedChat();
|
|
|
|
} else if (
|
|
|
|
e.type === SOCKET_MESSAGE_TYPES.ERROR_NEEDS_REGISTRATION &&
|
|
|
|
!this.isRegistering
|
|
|
|
) {
|
|
|
|
// User needs an access token, so start the user auth flow.
|
|
|
|
this.state.websocket.shutdown();
|
|
|
|
this.setState({ websocket: null });
|
|
|
|
this.setupChatAuth(true);
|
|
|
|
} else if (e.type === SOCKET_MESSAGE_TYPES.ERROR_MAX_CONNECTIONS_EXCEEDED) {
|
|
|
|
// Chat server cannot support any more chat clients. Turn off chat for them.
|
|
|
|
this.disableChat();
|
|
|
|
} else if (e.type === SOCKET_MESSAGE_TYPES.CONNECTED_USER_INFO) {
|
|
|
|
// When connected the user will return an event letting us know what our
|
|
|
|
// user details are so we can display them properly.
|
|
|
|
const { user } = e;
|
|
|
|
const { displayName } = user;
|
|
|
|
|
|
|
|
this.setState({ username: displayName });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBlockedChat() {
|
|
|
|
setLocalStorage('owncast_chat_blocked', true);
|
|
|
|
this.disableChat();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFormFocus() {
|
|
|
|
if (this.hasTouchScreen) {
|
|
|
|
this.setState({
|
|
|
|
touchKeyboardActive: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFormBlur() {
|
|
|
|
if (this.hasTouchScreen) {
|
|
|
|
this.setState({
|
|
|
|
touchKeyboardActive: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disableChat() {
|
|
|
|
this.state.websocket.shutdown();
|
|
|
|
this.setState({ websocket: null, canChat: false });
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
async setupChatAuth(force) {
|
2021-08-01 02:21:30 +03:00
|
|
|
const { readonly } = this.props;
|
2021-08-01 02:22:00 +03:00
|
|
|
var accessToken = readonly
|
|
|
|
? getLocalStorage(KEY_EMBED_CHAT_ACCESS_TOKEN)
|
|
|
|
: getLocalStorage(KEY_ACCESS_TOKEN);
|
2021-08-01 02:21:30 +03:00
|
|
|
var randomIntArray = new Uint32Array(1);
|
|
|
|
window.crypto.getRandomValues(randomIntArray);
|
2021-08-01 02:22:00 +03:00
|
|
|
var username = readonly
|
|
|
|
? 'chat-embed-' + randomIntArray[0]
|
|
|
|
: getLocalStorage(KEY_USERNAME);
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
if (!accessToken || force) {
|
|
|
|
try {
|
|
|
|
this.isRegistering = true;
|
|
|
|
const registration = await registerChat(username);
|
|
|
|
accessToken = registration.accessToken;
|
|
|
|
username = registration.displayName;
|
|
|
|
|
2021-08-01 02:21:30 +03:00
|
|
|
if (readonly) {
|
|
|
|
setLocalStorage(KEY_EMBED_CHAT_ACCESS_TOKEN, accessToken);
|
|
|
|
} else {
|
|
|
|
setLocalStorage(KEY_ACCESS_TOKEN, accessToken);
|
|
|
|
setLocalStorage(KEY_USERNAME, username);
|
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
this.isRegistering = false;
|
|
|
|
} catch (e) {
|
|
|
|
console.error('registration error:', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.websocket) {
|
|
|
|
this.state.websocket.shutdown();
|
|
|
|
this.setState({
|
|
|
|
websocket: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Without a valid access token he websocket connection will be rejected.
|
2022-03-07 04:11:51 +03:00
|
|
|
const websocket = new Websocket(
|
|
|
|
accessToken,
|
|
|
|
this.socketHostOverride || URL_WEBSOCKET
|
|
|
|
);
|
2021-08-01 02:21:30 +03:00
|
|
|
websocket.addListener(
|
|
|
|
CALLBACKS.RAW_WEBSOCKET_MESSAGE_RECEIVED,
|
|
|
|
this.handleWebsocketMessage
|
|
|
|
);
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
username,
|
|
|
|
websocket,
|
|
|
|
accessToken,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:21:30 +03:00
|
|
|
sendUsernameChange(newName) {
|
|
|
|
const nameChange = {
|
|
|
|
type: SOCKET_MESSAGE_TYPES.NAME_CHANGE,
|
|
|
|
newName,
|
|
|
|
};
|
|
|
|
this.state.websocket.send(nameChange);
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:28:25 +03:00
|
|
|
render(props, state) {
|
2021-08-01 02:22:00 +03:00
|
|
|
const { username, websocket, accessToken, chatInputEnabled, configData } =
|
|
|
|
state;
|
2021-08-01 02:21:30 +03:00
|
|
|
|
2021-08-01 02:22:00 +03:00
|
|
|
const { chatDisabled, maxSocketPayloadSize, customStyles, name } =
|
|
|
|
configData;
|
2021-08-01 02:21:30 +03:00
|
|
|
|
|
|
|
const { readonly } = props;
|
2021-08-01 02:22:00 +03:00
|
|
|
return this.state.websocket
|
|
|
|
? html`${!readonly
|
|
|
|
? html`<style>
|
|
|
|
${customStyles}
|
|
|
|
</style>
|
|
|
|
<header
|
|
|
|
class="flex flex-row-reverse fixed z-10 w-full bg-gray-900"
|
|
|
|
>
|
|
|
|
<${UsernameForm}
|
|
|
|
username=${username}
|
|
|
|
onUsernameChange=${this.handleUsernameChange}
|
|
|
|
onFocus=${this.handleFormFocus}
|
|
|
|
onBlur=${this.handleFormBlur}
|
|
|
|
/>
|
|
|
|
</header>`
|
|
|
|
: ''}
|
|
|
|
<${Chat}
|
|
|
|
websocket=${websocket}
|
2021-08-01 02:21:30 +03:00
|
|
|
username=${username}
|
2021-08-01 02:22:00 +03:00
|
|
|
accessToken=${accessToken}
|
|
|
|
readonly=${readonly}
|
|
|
|
instanceTitle=${name}
|
|
|
|
chatInputEnabled=${chatInputEnabled && !chatDisabled}
|
|
|
|
inputMaxBytes=${maxSocketPayloadSize - EST_SOCKET_PAYLOAD_BUFFER ||
|
|
|
|
CHAT_MAX_MESSAGE_LENGTH}
|
|
|
|
/>`
|
2021-08-01 02:21:30 +03:00
|
|
|
: null;
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
}
|