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

96 lines
2.2 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';
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',
];
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();
});
}
},
});
}
function didUserWasKicked() {
wasKickedDep.depend();
return wasKicked;
}
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:48:26 +08:00
didUserWasKicked,
redirectToLogoutUrl
2016-05-12 23:41:51 +08:00
};