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

242 lines
7.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 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';
import mapUser from '/imports/ui/services/user/mapUser';
2018-10-13 01:05:30 +08:00
import { Session } from 'meteor/session';
2018-07-19 20:46:44 +08:00
import IntlStartup from './intl';
2019-01-18 00:43:44 +08:00
import Meetings from '../../api/meetings';
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 = {
subscriptionsReady: PropTypes.bool.isRequired,
locale: PropTypes.string,
approved: PropTypes.bool,
meetingEnded: PropTypes.bool,
meetingExist: PropTypes.bool,
};
2017-03-11 02:33:46 +08:00
const defaultProps = {
2017-11-01 01:13:44 +08:00
locale: undefined,
approved: undefined,
meetingEnded: false,
meetingExist: false,
};
2017-03-11 02:33:46 +08:00
class Base extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
meetingExisted: false,
2017-03-11 02:33:46 +08:00
};
this.updateLoadingState = this.updateLoadingState.bind(this);
}
componentDidUpdate(prevProps, prevState) {
const {
ejected,
approved,
meetingExist,
animations,
2019-02-07 23:34:21 +08:00
meteorIsConnected,
} = this.props;
const { loading, meetingExisted } = this.state;
2018-07-19 20:46:44 +08:00
if (!prevProps.meetingExist && meetingExist) {
Session.set('isMeetingEnded', false);
}
if (prevProps.meetingExist && !meetingExist) {
Session.set('isMeetingEnded', true);
this.setMeetingExisted(true);
}
// In case the meeting delayed to load
if (!meetingExist) return;
if (approved && loading) this.updateLoadingState(false);
if (prevProps.ejected || ejected) {
Session.set('codeError', '403');
Session.set('isMeetingEnded', true);
}
// In case the meteor restart avoid error log
2019-02-07 23:34:21 +08:00
if (meteorIsConnected && (prevState.meetingExisted !== meetingExisted)) {
this.setMeetingExisted(false);
}
if (animations && animations !== prevProps.animations) {
document.documentElement.style.setProperty('--enableAnimation', 1);
} else if (!animations && animations !== prevProps.animations) {
document.documentElement.style.setProperty('--enableAnimation', 0);
}
2018-07-19 20:46:44 +08:00
}
setMeetingExisted(meetingExisted) {
this.setState({ meetingExisted });
}
2017-03-11 02:33:46 +08:00
updateLoadingState(loading = false) {
this.setState({
loading,
});
}
renderByState() {
2018-10-13 01:05:30 +08:00
const { updateLoadingState } = this;
const stateControls = { updateLoadingState };
2017-03-11 02:33:46 +08:00
const { loading, meetingExisted } = this.state;
2017-03-11 02:33:46 +08:00
2018-10-13 01:05:30 +08:00
const codeError = Session.get('codeError');
const {
subscriptionsReady,
meetingExist,
} = this.props;
if (meetingExisted && !meetingExist) {
2018-10-13 01:05:30 +08:00
AudioManager.exitAudio();
return (<MeetingEnded code={Session.get('codeError')} />);
}
if (codeError) {
logger.error({ logCode: 'startup_client_usercouldnotlogin_error' }, `User could not log in HTML5, hit ${codeError}`);
2018-10-13 01:05:30 +08:00
return (<ErrorScreen code={codeError} />);
}
2017-03-11 02:33:46 +08:00
if (loading || !subscriptionsReady) {
return (<LoadingScreen>{loading}</LoadingScreen>);
}
// this.props.annotationsHandler.stop();
if (subscriptionsReady) {
logger.info({ logCode: 'startup_client_subscriptions_ready' }, 'Subscriptions are ready');
}
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() {
2018-10-13 01:05:30 +08:00
const { updateLoadingState } = this;
2019-01-18 00:43:44 +08:00
const { locale, meetingExist } = this.props;
2018-10-13 01:05:30 +08:00
const stateControls = { updateLoadingState };
const { meetingExisted } = this.state;
2017-03-11 02:33:46 +08:00
return (
2019-01-30 19:52:24 +08:00
(!meetingExisted && !meetingExist && Auth.loggedIn)
? <LoadingScreen />
: (
2019-01-18 00:43:44 +08:00
<IntlStartup locale={locale} baseControls={stateControls}>
{this.renderByState()}
</IntlStartup>
)
2017-03-11 02:33:46 +08:00
);
}
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', 'voiceUsers', 'whiteboard-multi-user', 'screenshare',
'group-chat', 'presentation-pods', 'users-settings',
2017-03-11 02:33:46 +08:00
];
2018-10-03 03:53:13 +08:00
const BaseContainer = withTracker(() => {
const { locale, animations } = Settings.application;
const { credentials, loggedIn } = Auth;
const { meetingId, requesterUserId } = credentials;
let breakoutRoomSubscriptionHandler;
let userSubscriptionHandler;
const subscriptionErrorHandler = {
onError: (error) => {
logger.error({ logCode: 'startup_client_subscription_error' }, error);
2018-10-13 01:05:30 +08:00
Session.set('isMeetingEnded', true);
Session.set('codeError', error.error);
},
};
const subscriptionsHandlers = SUBSCRIPTIONS_NAME
.map(name => Meteor.subscribe(name, credentials, subscriptionErrorHandler));
2019-02-07 19:19:35 +08:00
const subscriptionsReady = subscriptionsHandlers.every(handler => handler.ready())
&& loggedIn;
2019-01-18 00:43:44 +08:00
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 User = Users.findOne({ intId: credentials.requesterUserId });
if (User) {
const mappedUser = mapUser(User);
// override meteor subscription to verify if is moderator
userSubscriptionHandler = Meteor.subscribe('users', credentials, mappedUser.isModerator, subscriptionErrorHandler);
breakoutRoomSubscriptionHandler = Meteor.subscribe('breakouts', credentials, mappedUser.isModerator, 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,
});
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 }),
ejected: Users.findOne({ userId: Auth.userID, ejected: true }),
meetingEnded: Session.get('isMeetingEnded'),
locale,
subscriptionsReady,
annotationsHandler,
groupChatMessageHandler,
userSubscriptionHandler,
breakoutRoomSubscriptionHandler,
animations,
meetingExist: !!Meetings.find({ meetingId }).count(),
User,
2019-02-07 23:34:21 +08:00
meteorIsConnected: Meteor.status().connected,
2017-03-11 02:33:46 +08:00
};
2018-10-03 03:53:13 +08:00
})(Base);
export default BaseContainer;