bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/app/service.js

120 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-05-14 00:31:06 +08:00
import { Meteor } from 'meteor/meteor';
import Auth from '/imports/ui/services/auth';
2016-05-17 02:05:44 +08:00
import Users from '/imports/api/users';
import Chat from '/imports/api/chat';
import Meetings from '/imports/api/meetings';
import Cursor from '/imports/api/cursor';
import Captions from '/imports/api/captions';
2016-05-13 03:50:02 +08:00
import Polls from '/imports/api/polls';
2016-05-14 00:31:06 +08:00
function setCredentials(nextState, replace) {
2016-06-03 02:40:27 +08:00
if (nextState && nextState.params.authToken) {
const { meetingID, userID, authToken } = nextState.params;
Auth.setCredentials(meetingID, userID, authToken);
2016-07-11 20:34:58 +08:00
replace({
pathname: '/',
2016-07-11 20:34:58 +08:00
});
}
};
2016-07-11 20:34:58 +08:00
let dataSubscriptions = null;
2016-05-14 00:31:06 +08:00
function subscribeForData() {
if (dataSubscriptions) {
2016-07-11 20:34:58 +08:00
return dataSubscriptions;
}
const subNames = [
'users', 'chat', 'cursor', 'deskshare', 'meetings',
'polls', 'presentations', 'shapes', 'slides', 'captions', 'breakouts',
];
2016-07-11 20:34:58 +08:00
let subs = [];
subNames.forEach(name => subs.push(subscribeFor(name)));
2016-06-18 06:15:11 +08:00
2016-07-11 20:34:58 +08:00
dataSubscriptions = subs;
return subs;
};
2016-05-14 00:31:06 +08:00
function subscribeFor(collectionName) {
const credentials = Auth.getCredentials();
2016-07-11 20:34:58 +08:00
return new Promise((resolve, reject) => {
Meteor.subscribe(collectionName, credentials, {
onReady: (...args) => resolve(...args),
onStop: (...args) => reject(...args),
});
});
};
2016-07-11 20:34:58 +08:00
function subscribeToCollections(cb) {
2016-07-29 03:48:26 +08:00
subscribeFor('users')
.then(() => {
observeUserKick();
return Promise.all(subscribeForData())
.then(() => {
if (cb) {
return cb();
}
});
})
.catch(redirectToLogoutUrl);
2016-07-11 20:34:58 +08:00
};
2016-06-25 07:09:32 +08:00
2016-07-29 03:48:26 +08:00
function redirectToLogoutUrl(reason) {
console.error(reason);
console.log('Redirecting user to the logoutURL...');
document.location.href = Auth.logoutURL;
}
2016-07-29 03:48:26 +08:00
let wasKicked = false;
const wasKickedDep = new Tracker.Dependency;
function observeUserKick() {
Users.find().observe({
removed(old) {
if (old.userId === Auth.userID) {
Auth.clearCredentials(() => {
wasKicked = true;
wasKickedDep.changed();
});
}
},
});
}
2016-07-29 03:57:42 +08:00
function wasUserKicked() {
2016-07-29 03:48:26 +08:00
wasKickedDep.depend();
return wasKicked;
}
let modal = null;
const modalDep = new Tracker.Dependency;
const getModal = () => {
modalDep.depend();
return modal;
};
const showModal = (val) => {
if (val !== modal) {
modal = val;
modalDep.changed();
}
};
2016-09-22 03:18:13 +08:00
const clearModal = () => {
showModal(null);
};
2016-05-13 03:50:02 +08:00
export {
subscribeForData,
setCredentials,
2016-07-11 20:34:58 +08:00
subscribeFor,
subscribeToCollections,
2016-07-29 03:57:42 +08:00
wasUserKicked,
redirectToLogoutUrl,
getBreakouts,
getModal,
showModal,
clearModal,
2016-05-12 23:41:51 +08:00
};