Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
Banned and chat disconnected states will hide chat. Closes #2764
Gabe Kangas04ba3fc1d1cf
diff --git a/web/components/stores/ClientConfigStore.tsx b/web/components/stores/ClientConfigStore.tsxindex 81c0fe60be..32d7850fc9 100644--- a/web/components/stores/ClientConfigStore.tsx+++ b/web/components/stores/ClientConfigStore.tsx@@ -35,8 +35,9 @@ const ACCESS_TOKEN_KEY = 'accessToken';let serverStatusRefreshPoll: ReturnType<typeof setInterval>;let hasBeenModeratorNotified = false;+let hasWebsocketDisconnected = false;-const serverConnectivityError = `Cannot connect to the Owncast service. Please check your internet connection or if needed, double check this Owncast server is running.`;+const serverConnectivityError = `Cannot connect to the Owncast service. Please check your internet connection and verify this Owncast server is running.`;// Server status is what gets updated such as viewer count, durations,// stream title, online/offline state, etc.@@ -112,24 +113,23 @@ export const removedMessageIdsAtom = atom<string[]>({default: [],});-// Chat is visible if the user wishes it to be visible AND the required-// chat state is set.-export const isChatVisibleSelector = selector({- key: 'isChatVisibleSelector',+export const isChatAvailableSelector = selector({+ key: 'isChatAvailableSelector',get: ({ get }) => {const state: AppStateOptions = get(appStateAtom);- const userVisibleToggle: boolean = get(chatVisibleToggleAtom);const accessToken: string = get(accessTokenAtom);- return accessToken && state.chatAvailable && userVisibleToggle;+ return accessToken && state.chatAvailable && !hasWebsocketDisconnected;},});-export const isChatAvailableSelector = selector({- key: 'isChatAvailableSelector',+// Chat is visible if the user wishes it to be visible AND the required+// chat state is set.+export const isChatVisibleSelector = selector({+ key: 'isChatVisibleSelector',get: ({ get }) => {const state: AppStateOptions = get(appStateAtom);- const accessToken: string = get(accessTokenAtom);- return accessToken && state.chatAvailable;+ const userVisibleToggle: boolean = get(chatVisibleToggleAtom);+ return state.chatAvailable && userVisibleToggle && !hasWebsocketDisconnected;},});@@ -163,9 +163,9 @@ export const ClientConfigStore: FC = () => {const [currentUser, setCurrentUser] = useRecoilState(currentUserAtom);const setChatAuthenticated = useSetRecoilState<boolean>(chatAuthenticatedAtom);const [clientConfig, setClientConfig] = useRecoilState<ClientConfig>(clientConfigStateAtom);- const [, setServerStatus] = useRecoilState<ServerStatus>(serverStatusState);+ const setServerStatus = useSetRecoilState<ServerStatus>(serverStatusState);const setClockSkew = useSetRecoilState<Number>(clockSkewAtom);- const [chatMessages, setChatMessages] = useRecoilState<SocketEvent[]>(chatMessagesAtom);+ const setChatMessages = useSetRecoilState<SocketEvent[]>(chatMessagesAtom);const [accessToken, setAccessToken] = useRecoilState<string>(accessTokenAtom);const setAppState = useSetRecoilState<AppStateOptions>(appStateAtom);const setGlobalFatalErrorMessage = useSetRecoilState<DisplayableError>(fatalErrorStateAtom);@@ -281,6 +281,14 @@ export const ClientConfigStore: FC = () => {}};+ const handleSocketDisconnect = () => {+ hasWebsocketDisconnected = true;+ };++ const handleSocketConnected = () => {+ hasWebsocketDisconnected = false;+ };+const handleMessage = (message: SocketEvent) => {switch (message.type) {case MessageType.ERROR_NEEDS_REGISTRATION:@@ -328,6 +336,10 @@ export const ClientConfigStore: FC = () => {case MessageType.VISIBILITY_UPDATE:handleMessageVisibilityChange(message as MessageVisibilityEvent);break;+ case MessageType.ERROR_USER_DISABLED:+ console.log('User has been disabled');+ sendEvent([AppStateEvent.ChatUserDisabled]);+ break;default:console.error('Unknown socket message type: ', message.type);}@@ -336,7 +348,9 @@ export const ClientConfigStore: FC = () => {const getChatHistory = async () => {try {const messages = await ChatService.getChatHistory(accessToken);- setChatMessages(currentState => [...currentState, ...messages]);+ if (messages) {+ setChatMessages(currentState => [...currentState, ...messages]);+ }} catch (error) {console.error(`ChatService -> getChatHistory() ERROR: \n${error}`);}@@ -354,14 +368,15 @@ export const ClientConfigStore: FC = () => {ws = new WebsocketService(accessToken, '/ws', host);ws.handleMessage = handleMessage;+ ws.socketDisconnected = handleSocketDisconnect;+ ws.socketConnected = handleSocketConnected;setWebsocketService(ws);} catch (error) {console.error(`ChatService -> startChat() ERROR: \n${error}`);+ sendEvent([AppStateEvent.ChatUserDisabled]);}};- const handleChatNotification = () => {};-// Read the config and status on initial load from a JSON string that lives// in window. This is placed there server-side and allows for fast initial// load times because we don't have to wait for the API calls to complete.@@ -393,11 +408,6 @@ export const ClientConfigStore: FC = () => {}}, [hasLoadedConfig, accessToken]);- // Notify about chat activity when backgrounded.- useEffect(() => {- handleChatNotification();- }, [chatMessages]);-useEffect(() => {updateClientConfig();handleUserRegistration();diff --git a/web/components/stores/application-state.ts b/web/components/stores/application-state.tsindex f0df0d4122..d43f143399 100644--- a/web/components/stores/application-state.ts+++ b/web/components/stores/application-state.ts@@ -63,6 +63,7 @@ export enum AppStateEvent {Offline = 'OFFLINE', // Stream is not liveNeedsRegister = 'NEEDS_REGISTER', // Needs to register a chat userFail = 'FAIL', // Error+ ChatUserDisabled = 'CHAT_USER_DISABLED', // Chat user is disabled}const appStateModel =@@ -97,6 +98,9 @@ const appStateModel =OFFLINE: {target: 'goodbye',},+ CHAT_USER_DISABLED: {+ target: 'chatUserDisabled',+ },},},offline: {@@ -124,6 +128,12 @@ const appStateModel =},},},+ chatUserDisabled: {+ meta: {+ ...ONLINE_STATE,+ chatAvailable: false,+ },+ },},},serverFailure: {diff --git a/web/components/ui/Content/Content.tsx b/web/components/ui/Content/Content.tsxindex 8f8b3c3b4a..a5a95b0ed5 100644--- a/web/components/ui/Content/Content.tsx+++ b/web/components/ui/Content/Content.tsx@@ -199,7 +199,7 @@ export const Content: FC = () => {setSupportsBrowserNotifications(isPushNotificationSupported() && browserNotificationsEnabled);}, [browserNotificationsEnabled]);- const showChat = !chatDisabled && isChatAvailable && isChatVisible;+ const showChat = online && !chatDisabled && isChatVisible;return (<>diff --git a/web/components/ui/Sidebar/Sidebar.tsx b/web/components/ui/Sidebar/Sidebar.tsxindex 1e492aa502..45453e17d4 100644--- a/web/components/ui/Sidebar/Sidebar.tsx+++ b/web/components/ui/Sidebar/Sidebar.tsx@@ -2,6 +2,7 @@ import Sider from 'antd/lib/layout/Sider';import { useRecoilValue } from 'recoil';import { FC } from 'react';import dynamic from 'next/dynamic';+import { Spin } from 'antd';import { ChatMessage } from '../../../interfaces/chat-message.model';import styles from './Sidebar.module.scss';@@ -25,7 +26,11 @@ export const Sidebar: FC = () => {const isChatAvailable = useRecoilValue(isChatAvailableSelector);if (!currentUser) {- return <Sider className={styles.root} collapsedWidth={0} width={320} />;+ return (+ <Sider className={styles.root} collapsedWidth={0} width={320}>+ <Spin spinning size="large" />+ </Sider>+ );}const { id, isModerator, displayName } = currentUser;@@ -37,6 +42,7 @@ export const Sidebar: FC = () => {chatUserId={id}isModerator={isModerator}chatAvailable={isChatAvailable}+ showInput={!!currentUser}/></Sider>);diff --git a/web/services/chat-service.ts b/web/services/chat-service.tsindex edf433a554..2e5837b588 100644--- a/web/services/chat-service.ts+++ b/web/services/chat-service.ts@@ -19,8 +19,12 @@ export interface ChatStaticService {class ChatService {public static async getChatHistory(accessToken: string): Promise<ChatMessage[]> {- const response = await getUnauthedData(`${ENDPOINT}?accessToken=${accessToken}`);- return response;+ try {+ const response = await getUnauthedData(`${ENDPOINT}?accessToken=${accessToken}`);+ return response;+ } catch (e) {+ return [];+ }}public static async registerUser(username: string): Promise<UserRegistrationResponse> {diff --git a/web/services/websocket-service.ts b/web/services/websocket-service.tsindex d6fdca2cd3..31e60a2b32 100644--- a/web/services/websocket-service.ts+++ b/web/services/websocket-service.ts@@ -10,6 +10,8 @@ export default class WebsocketService {accessToken: string;+ host: string;+path: string;websocketReconnectTimer: ReturnType<typeof setTimeout>;@@ -20,20 +22,28 @@ export default class WebsocketService {handleMessage?: (message: SocketEvent) => void;+ socketConnected: () => void;++ socketDisconnected: () => void;+constructor(accessToken, path, host) {this.accessToken = accessToken;this.path = path;this.websocketReconnectTimer = null;this.isShutdown = false;+ this.host = host;this.createAndConnect = this.createAndConnect.bind(this);this.shutdown = this.shutdown.bind(this);- this.createAndConnect(host);+ this.createAndConnect();}- createAndConnect(host) {- const url = new URL(host);+ createAndConnect() {+ if (!this.host) {+ return;+ }+ const url = new URL(this.host);url.protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';url.pathname = '/ws';url.port = window.location.port === '3000' ? '8080' : window.location.port;@@ -52,11 +62,13 @@ export default class WebsocketService {if (this.websocketReconnectTimer) {clearTimeout(this.websocketReconnectTimer);}+ this.socketConnected();}// On ws error just close the socket and let it re-connect again for now.- onError(e) {- handleNetworkingError(`Socket error: ${e}`);+ onError() {+ handleNetworkingError();+ this.socketDisconnected();this.websocket.close();if (!this.isShutdown) {this.scheduleReconnect();@@ -137,8 +149,8 @@ export default class WebsocketService {}}-function handleNetworkingError(error) {+function handleNetworkingError() {console.error(- `Chat has been disconnected and is likely not working for you. It's possible you were removed from chat. If this is a server configuration issue, visit troubleshooting steps to resolve. https://owncast.online/docs/troubleshooting/#chat-is-disabled: ${error}`,+ `Chat has been disconnected and is likely not working for you. It's possible you were removed from chat. If this is a server configuration issue, visit troubleshooting steps to resolve. https://owncast.online/docs/troubleshooting/#chat-is-disabled`,);}diff --git a/web/utils/apis.ts b/web/utils/apis.tsindex 097889144f..b18148eb5d 100644--- a/web/utils/apis.ts+++ b/web/utils/apis.ts@@ -150,21 +150,14 @@ export async function fetchData(url: string, options?: FetchOptions) {requestOptions.credentials = 'include';}- try {- const response = await fetch(url, requestOptions);- const json = await response.json();+ const response = await fetch(url, requestOptions);+ const json = await response.json();- if (!response.ok) {- const message = json.message || `An error has occurred: ${response.status}`;- throw new Error(message);- }- return json;- } catch (error) {- console.error(error);- return error;- // console.log(error)- // throw new Error(error)+ if (!response.ok) {+ const message = json.message || `An error has occurred: ${response.status}`;+ throw new Error(message);}+ return json;}export async function getUnauthedData(url: string, options?: FetchOptions) {