bigbluebutton-Github/bigbluebutton-html5/imports/startup/client/base.jsx

190 lines
5.2 KiB
React
Raw Normal View History

2017-03-11 02:33:46 +08:00
import React, { Component } from 'react';
import { withTracker } from 'meteor/react-meteor-data';
import { withRouter } from 'react-router';
import PropTypes from 'prop-types';
2017-03-11 02:33:46 +08:00
import Auth from '/imports/ui/services/auth';
import AppContainer from '/imports/ui/components/app/container';
2017-03-11 04:47:23 +08:00
import ErrorScreen from '/imports/ui/components/error-screen/component';
import MeetingEnded from '/imports/ui/components/meeting-ended/component';
2017-03-11 02:33:46 +08:00
import LoadingScreen from '/imports/ui/components/loading-screen/component';
2017-04-06 20:36:59 +08:00
import Settings from '/imports/ui/services/settings';
import AudioManager from '/imports/ui/services/audio-manager';
import logger from '/imports/startup/client/logger';
2018-07-19 20:46:44 +08:00
import Users from '/imports/api/users';
import Annotations from '/imports/api/annotations';
import AnnotationsLocal from '/imports/ui/components/whiteboard/service';
import GroupChat from '/imports/api/group-chat';
2018-07-19 20:46:44 +08:00
import IntlStartup from './intl';
const CHAT_CONFIG = Meteor.settings.public.chat;
const PUBLIC_GROUP_CHAT_ID = CHAT_CONFIG.public_group_id;
const PUBLIC_CHAT_TYPE = CHAT_CONFIG.type_public;
const propTypes = {
error: PropTypes.object,
errorCode: PropTypes.number,
subscriptionsReady: PropTypes.bool.isRequired,
locale: PropTypes.string,
endedCode: PropTypes.string,
approved: PropTypes.bool,
};
2017-03-11 02:33:46 +08:00
const defaultProps = {
error: undefined,
errorCode: undefined,
2017-11-01 01:13:44 +08:00
locale: undefined,
endedCode: undefined,
approved: undefined,
};
2017-03-11 02:33:46 +08:00
class Base extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
error: props.error || null,
};
this.updateLoadingState = this.updateLoadingState.bind(this);
this.updateErrorState = this.updateErrorState.bind(this);
}
2018-07-19 20:46:44 +08:00
componentWillUpdate() {
const { approved } = this.props;
const isLoading = this.state.loading;
if (approved && isLoading) this.updateLoadingState(false);
}
2017-03-11 02:33:46 +08:00
updateLoadingState(loading = false) {
this.setState({
loading,
});
}
updateErrorState(error = null) {
this.setState({
error,
});
}
renderByState() {
const { updateLoadingState, updateErrorState } = this;
const stateControls = { updateLoadingState, updateErrorState };
const { loading, error } = this.state;
2017-03-11 04:47:23 +08:00
const { subscriptionsReady, errorCode } = this.props;
const { endedCode } = this.props.params;
if (endedCode) {
AudioManager.exitAudio();
return (<MeetingEnded code={endedCode} />);
}
2017-03-11 02:33:46 +08:00
2017-03-11 04:47:23 +08:00
if (error || errorCode) {
logger.error(`User could not log in HTML5, hit ${errorCode}`);
2017-03-11 04:47:23 +08:00
return (<ErrorScreen code={errorCode}>{error}</ErrorScreen>);
2017-03-11 02:33:46 +08:00
}
if (loading || !subscriptionsReady) {
return (<LoadingScreen>{loading}</LoadingScreen>);
}
// this.props.annotationsHandler.stop();
if (subscriptionsReady) {
logger.info('Client loaded successfully');
}
2018-08-09 05:20:55 +08:00
2017-06-03 03:25:02 +08:00
return (<AppContainer {...this.props} baseControls={stateControls} />);
2017-03-11 02:33:46 +08:00
}
render() {
const { updateLoadingState, updateErrorState } = this;
2017-04-06 20:36:59 +08:00
const { locale } = this.props;
2017-03-11 02:33:46 +08:00
const stateControls = { updateLoadingState, updateErrorState };
return (
<IntlStartup locale={locale} baseControls={stateControls}>
2017-03-11 02:33:46 +08:00
{this.renderByState()}
</IntlStartup>
);
}
2017-06-03 03:25:02 +08:00
}
2017-03-11 02:33:46 +08:00
Base.propTypes = propTypes;
Base.defaultProps = defaultProps;
2017-03-11 02:33:46 +08:00
const SUBSCRIPTIONS_NAME = [
'users', 'meetings', 'polls', 'presentations',
'slides', 'captions', 'breakouts', 'voiceUsers', 'whiteboard-multi-user', 'screenshare',
'group-chat', 'presentation-pods',
2017-03-11 02:33:46 +08:00
];
const BaseContainer = withRouter(withTracker(({ params, router }) => {
2017-03-17 00:52:43 +08:00
if (params.errorCode) return params;
2017-03-11 04:47:23 +08:00
const { locale } = Settings.application;
const { credentials, loggedIn } = Auth;
const { meetingId, requesterUserId } = credentials;
if (!loggedIn) {
return {
locale,
subscriptionsReady: false,
};
}
const subscriptionErrorHandler = {
onError: (error) => {
logger.error(error);
return router.push('/logout');
},
};
const subscriptionsHandlers = SUBSCRIPTIONS_NAME.map(name =>
Meteor.subscribe(name, credentials, subscriptionErrorHandler));
const chats = GroupChat.find({
$or: [
{
meetingId,
access: PUBLIC_CHAT_TYPE,
chatId: { $ne: PUBLIC_GROUP_CHAT_ID },
},
{ meetingId, users: { $all: [requesterUserId] } },
],
}).fetch();
const chatIds = chats.map(chat => chat.chatId);
const groupChatMessageHandler = Meteor.subscribe('group-chat-msg', credentials, chatIds, subscriptionErrorHandler);
const annotationsHandler = Meteor.subscribe('annotations', credentials, {
onReady: () => {
AnnotationsLocal.remove({});
2018-06-30 02:11:11 +08:00
Annotations.find({}, { reactive: false }).forEach((a) => {
try {
2018-06-30 02:11:11 +08:00
AnnotationsLocal.insert(a);
} catch (e) {
// TODO
}
});
annotationsHandler.stop();
},
...subscriptionErrorHandler,
});
const subscriptionsReady = subscriptionsHandlers.every(handler => handler.ready());
2017-03-11 02:33:46 +08:00
return {
2018-07-24 00:46:53 +08:00
approved: Users.findOne({ userId: Auth.userID, approved: true, guest: true }),
locale,
subscriptionsReady,
annotationsHandler,
groupChatMessageHandler,
2017-03-11 02:33:46 +08:00
};
})(Base));
export default BaseContainer;