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

191 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-05-21 00:43:31 +08:00
import _ from 'lodash';
import Captions from '/imports/api/captions';
import Users from '/imports/api/users';
import Auth from '/imports/ui/services/auth';
import { makeCall } from '/imports/ui/services/api';
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
const CAPTIONS_CONFIG = Meteor.settings.public.captions;
const CAPTIONS = '_captions_';
2019-05-21 01:39:04 +08:00
const LINE_BREAK = '\n';
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
2019-05-23 22:51:01 +08:00
const getActiveCaptions = () => {
const activeCaptions = Session.get('activeCaptions');
if (!activeCaptions) return '';
return activeCaptions;
};
const getCaptions = locale => Captions.findOne({
meetingId: Auth.meetingID,
padId: { $regex: `${CAPTIONS}${locale}$` },
});
2019-05-23 22:51:01 +08:00
const getCaptionsData = () => {
const activeCaptions = getActiveCaptions();
2019-05-29 22:31:27 +08:00
let padId = '';
2019-05-21 00:43:31 +08:00
let revs = 0;
2019-05-29 22:31:27 +08:00
let data = '';
if (activeCaptions) {
2019-05-21 00:43:31 +08:00
const captions = getCaptions(activeCaptions);
if (!_.isEmpty(captions)) {
2019-05-29 22:31:27 +08:00
padId = captions.padId; // eslint-disable-line prefer-destructuring
revs = captions.revs; // eslint-disable-line prefer-destructuring
data = captions.data; // eslint-disable-line prefer-destructuring
2019-05-21 00:43:31 +08:00
}
}
2019-05-21 00:43:31 +08:00
return { padId, revs, data };
};
const getAvailableLocales = () => {
const { meetingID } = Auth;
2019-05-29 22:31:27 +08:00
const locales = [];
Captions.find({ meetingId: meetingID },
{ fields: { ownerId: 1, locale: 1 } })
.forEach((caption) => {
if (caption.ownerId === '') {
locales.push(caption.locale);
}
});
return locales;
};
const getOwnedLocales = () => {
const { meetingID } = Auth;
2019-05-29 22:31:27 +08:00
const locales = [];
Captions.find({ meetingId: meetingID }, { fields: { ownerId: 1, locale: 1 } })
.forEach((caption) => {
if (caption.ownerId !== '') {
locales.push(caption.locale);
}
});
return locales;
};
2019-05-29 22:31:27 +08:00
const takeOwnership = (locale) => {
makeCall('takeOwnership', locale);
};
const formatEntry = (entry) => {
const letterIndex = entry.charAt(0) === ' ' ? 1 : 0;
const formattedEntry = `${entry.charAt(letterIndex).toUpperCase() + entry.slice(letterIndex + 1)}.\n\n`;
return formattedEntry;
};
const appendText = (text, locale) => {
makeCall('appendText', formatEntry(text), locale);
};
2019-05-29 22:31:27 +08:00
const canIOwnThisPad = (ownerId) => {
const { userID } = Auth;
if (!CAPTIONS_CONFIG.takeOwnership) return false;
2019-05-29 22:31:27 +08:00
if (ownerId === '') return false;
return ownerId !== userID;
};
const canIDictateThisPad = (ownerId) => {
const { userID } = Auth;
if (!CAPTIONS_CONFIG.enableDictation) return false;
if (ownerId === '') return false;
const SpeechRecognitionAPI = getSpeechRecognitionAPI();
if (!SpeechRecognitionAPI) return false;
return ownerId === userID;
};
const setActiveCaptions = (locale) => {
Session.set('activeCaptions', locale);
};
const setCaptionsSettings = (settings) => {
Session.set('captionsSettings', settings);
};
const getCaptionsSettings = () => {
const settings = Session.get('captionsSettings');
if (!settings) {
2019-05-29 22:31:27 +08:00
const {
backgroundColor, fontColor, fontFamily, fontSize,
} = CAPTIONS_CONFIG;
return {
backgroundColor, fontColor, fontFamily, fontSize,
};
}
return settings;
2019-05-21 01:39:04 +08:00
};
2019-05-29 22:31:27 +08:00
const isCaptionsEnabled = () => CAPTIONS_CONFIG.enabled;
const isCaptionsAvailable = () => {
if (isCaptionsEnabled) {
const ownedLocales = getOwnedLocales();
return (ownedLocales.length > 0);
}
return false;
2019-05-21 01:39:04 +08:00
};
const isCaptionsActive = () => {
const enabled = isCaptionsEnabled();
const activated = getActiveCaptions() !== '';
return (enabled && activated);
2019-05-21 01:39:04 +08:00
};
const deactivateCaptions = () => {
setActiveCaptions('');
2019-05-21 01:39:04 +08:00
};
const activateCaptions = (locale, settings) => {
setCaptionsSettings(settings);
setActiveCaptions(locale);
2019-05-21 01:39:04 +08:00
};
2019-05-29 22:31:27 +08:00
const formatCaptionsText = (text) => {
2019-05-21 01:39:04 +08:00
const splitText = text.split(LINE_BREAK);
const filteredText = splitText.filter((line, index) => {
2019-05-23 22:51:01 +08:00
const lastLine = index === (splitText.length - 1);
const emptyLine = line.length === 0;
2019-05-21 01:39:04 +08:00
return (!emptyLine || lastLine);
});
while (filteredText.length > CAPTIONS_CONFIG.lines) filteredText.shift();
return filteredText.join(LINE_BREAK);
};
const amIModerator = () => Users.findOne({ userId: Auth.userID },
{ fields: { role: 1 } }).role === ROLE_MODERATOR;
const getSpeechRecognitionAPI = () => window.SpeechRecognition || window.webkitSpeechRecognition;
const initSpeechRecognition = (locale) => {
const SpeechRecognitionAPI = getSpeechRecognitionAPI();
let recognition = null;
if (SpeechRecognitionAPI) {
recognition = new SpeechRecognitionAPI();
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = locale;
}
return recognition;
};
export default {
getCaptionsData,
getAvailableLocales,
getOwnedLocales,
takeOwnership,
appendText,
getCaptions,
canIOwnThisPad,
canIDictateThisPad,
getCaptionsSettings,
isCaptionsEnabled,
isCaptionsAvailable,
isCaptionsActive,
deactivateCaptions,
activateCaptions,
2019-05-21 01:39:04 +08:00
formatCaptionsText,
amIModerator,
initSpeechRecognition,
};