bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/app/service.js
Oswaldo Acauan 6f9c4219ad Fix typo
2016-07-28 19:57:42 +00:00

96 lines
2.1 KiB
JavaScript
Executable File

import { Meteor } from 'meteor/meteor';
import Auth from '/imports/ui/services/auth';
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 Polls from '/imports/api/polls';
function setCredentials(nextState, replace) {
if (nextState && nextState.params.authToken) {
const { meetingID, userID, authToken } = nextState.params;
Auth.setCredentials(meetingID, userID, authToken);
replace({
pathname: '/',
});
}
};
let dataSubscriptions = null;
function subscribeForData() {
if (dataSubscriptions) {
return dataSubscriptions;
}
const subNames = [
'users', 'chat', 'cursor', 'deskshare', 'meetings',
'polls', 'presentations', 'shapes', 'slides',
];
let subs = [];
subNames.forEach(name => subs.push(subscribeFor(name)));
dataSubscriptions = subs;
return subs;
};
function subscribeFor(collectionName) {
const credentials = Auth.getCredentials();
return new Promise((resolve, reject) => {
Meteor.subscribe(collectionName, credentials, {
onReady: (...args) => resolve(...args),
onStop: (...args) => reject(...args),
});
});
};
function subscribeToCollections(cb) {
subscribeFor('users')
.then(() => {
observeUserKick();
return Promise.all(subscribeForData())
.then(() => {
if (cb) {
return cb();
}
});
})
.catch(redirectToLogoutUrl);
};
function redirectToLogoutUrl(reason) {
console.error(reason);
console.log('Redirecting user to the logoutURL...');
document.location.href = Auth.logoutURL;
}
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();
});
}
},
});
}
function wasUserKicked() {
wasKickedDep.depend();
return wasKicked;
}
export {
subscribeForData,
setCredentials,
subscribeFor,
subscribeToCollections,
wasUserKicked,
redirectToLogoutUrl
};