bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/connection-status/service.js

132 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-04-10 01:01:46 +08:00
import ConnectionStatus from '/imports/api/connection-status';
import Users from '/imports/api/users';
import Auth from '/imports/ui/services/auth';
import { makeCall } from '/imports/ui/services/api';
const STATS = Meteor.settings.public.stats;
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
2020-04-28 22:20:19 +08:00
let audioStats = '';
const audioStatsDep = new Tracker.Dependency();
let statsTimeout = null;
const getHelp = () => STATS.help;
const getLevel = () => STATS.level;
const getAudioStats = () => {
audioStatsDep.depend();
return audioStats;
};
const setAudioStats = (level = '') => {
if (audioStats !== level) {
audioStats = level;
audioStatsDep.changed();
addConnectionStatus(level);
}
};
const handleAudioStatsEvent = (event) => {
const { detail } = event;
if (detail) {
const { loss, jitter } = detail;
let active = false;
// From higher to lower
for (let i = STATS.level.length - 1; i >= 0; i--) {
if (loss > STATS.loss[i] || jitter > STATS.jitter[i]) {
active = true;
setAudioStats(STATS.level[i]);
break;
}
}
if (active) {
if (statsTimeout !== null) clearTimeout(statsTimeout);
statsTimeout = setTimeout(() => {
setAudioStats();
}, STATS.length * STATS.interval);
}
}
};
2020-05-14 21:35:25 +08:00
const addConnectionStatus = (level) => {
if (level !== '') makeCall('addConnectionStatus', level);
};
const sortLevel = (a, b) => {
const indexOfA = STATS.level.indexOf(a.level);
const indexOfB = STATS.level.indexOf(b.level);
if (indexOfA < indexOfB) return 1;
if (indexOfA === indexOfB) return 0;
if (indexOfA > indexOfB) return -1;
};
2020-04-10 01:01:46 +08:00
const getConnectionStatus = () => {
const connectionStatus = ConnectionStatus.find(
{ meetingId: Auth.meetingID },
).fetch().map(status => {
2020-05-14 21:35:25 +08:00
const {
userId,
level,
timestamp,
} = status;
return {
userId,
level,
timestamp,
};
2020-04-10 01:01:46 +08:00
});
return Users.find(
2020-05-14 21:35:25 +08:00
{ meetingId: Auth.meetingID },
2020-04-10 01:01:46 +08:00
{ fields:
{
userId: 1,
name: 1,
role: 1,
connectionStatus: 1,
2020-04-10 01:01:46 +08:00
},
},
).fetch().reduce((result, user) => {
const {
userId,
name,
role,
connectionStatus: userStatus,
2020-04-10 01:01:46 +08:00
} = user;
const status = connectionStatus.find(status => status.userId === userId);
if (status) {
result.push({
name,
offline: userStatus === 'offline',
2020-04-10 01:01:46 +08:00
you: Auth.userID === userId,
moderator: role === ROLE_MODERATOR,
level: status.level,
timestamp: status.timestamp,
});
}
return result;
2020-05-14 21:35:25 +08:00
}, []).sort(sortLevel);
2020-04-10 01:01:46 +08:00
};
const isEnabled = () => STATS.enabled;
2020-04-28 22:20:19 +08:00
if (STATS.enabled) {
window.addEventListener('audiostats', handleAudioStatsEvent);
}
2020-04-10 01:01:46 +08:00
export default {
addConnectionStatus,
getConnectionStatus,
2020-04-28 22:20:19 +08:00
getAudioStats,
getHelp,
getLevel,
2020-04-10 01:01:46 +08:00
isEnabled,
};