bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/captions/service.js
Pedro Beschorner Marin 810deb907b refactor(etherpad): access control et al.
Move all Etherpad's access control from Meteor to a separated [Node application](https://github.com/bigbluebutton/bbb-pads).
This new app uses [Etherpad's API](https://etherpad.org/doc/v1.8.4/#index_overview)
to create groups and manage session tokens for users to access them. Each group
represents one distinct pad at the html5 client.

- Removed locked users' access to pads: replaced readOnly pad's access with a new pad's content sharing routine
- Pad's access is now controlled by [Etherpad's API](https://etherpad.org/doc/v1.8.4/#index_overview)
- Closed captions edited content now reflects at it's live feedback
- Improved closed caption's dictation mode live feedback
- Moved all Etherpad's API control from Meteor to a separated [app](https://github.com/bigbluebutton/bbb-pads)
- Included access control both in akka-apps and bbb-pads
2022-01-21 16:56:01 -03:00

259 lines
5.5 KiB
JavaScript

import Captions from '/imports/api/captions';
import Users from '/imports/api/users';
import Auth from '/imports/ui/services/auth';
import PadsService from '/imports/ui/components/pads/service';
import SpeechService from '/imports/ui/components/captions/speech/service';
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 ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
const LINE_BREAK = '\n';
const getAvailableLocales = () => {
const availableLocales = Captions.find(
{ meetingId: Auth.meetingID, ownerId: '' },
{ sort: { locale: 1 } },
{ fields: { ownerId: 1, locale: 1, name: 1 } },
).fetch();
return availableLocales;
};
const getOwnedLocales = () => {
const ownedLocales = Captions.find(
{ meetingId: Auth.meetingID, ownerId: { $not: '' } },
{ fields: { ownerId: 1, locale: 1, name: 1 } },
).fetch();
return ownedLocales;
};
const updateCaptionsOwner = (locale, name) => makeCall('updateCaptionsOwner', locale, name);
const startDictation = (locale) => makeCall('startDictation', locale);
const stopDictation = (locale) => makeCall('stopDictation', locale);
const getCaptionsSettings = () => {
const settings = Session.get('captionsSettings');
if (settings) return settings;
const {
background,
font,
} = CAPTIONS_CONFIG;
return {
backgroundColor: background,
fontColor: font.color,
fontFamily: font.family,
fontSize: font.size,
};
};
const setCaptionsSettings = (settings) => Session.set('captionsSettings', settings);
const getCaptionsLocale = () => Session.get('captionsLocale') || '';
const getCaptions = () => {
const locale = getCaptionsLocale();
if (locale) {
const {
name,
ownerId,
dictating,
} = Captions.findOne({
meetingId: Auth.meetingID,
locale,
});
return {
locale,
name,
ownerId,
dictating,
};
}
return {
locale,
name: '',
ownerId: '',
dictating: false,
};
};
const setCaptionsLocale = (locale) => Session.set('captionsLocale', locale);
const getCaptionsActive = () => Session.get('captionsActive') || '';
const formatCaptionsText = (text) => {
const splitText = text.split(LINE_BREAK);
const filteredText = splitText.filter((line, index) => {
const lastLine = index === (splitText.length - 1);
const emptyLine = line.length === 0;
return (!emptyLine || lastLine);
});
while (filteredText.length > CAPTIONS_CONFIG.lines) filteredText.shift();
return filteredText.join(LINE_BREAK);
};
const getCaptionsData = () => {
const locale = getCaptionsActive();
if (locale) {
const captions = Captions.findOne({
meetingId: Auth.meetingID,
locale,
dictating: true,
});
let data = '';
if (captions) {
data = captions.transcript;
} else {
data = PadsService.getPadTail(locale);
}
return formatCaptionsText(data);
}
return '';
};
const setCaptionsActive = (locale) => Session.set('captionsActive', locale);
const amICaptionsOwner = (ownerId) => ownerId === Auth.userID;
const isCaptionsEnabled = () => CAPTIONS_CONFIG.enabled;
const isCaptionsAvailable = () => {
if (isCaptionsEnabled()) {
const ownedLocales = getOwnedLocales();
return (ownedLocales.length > 0);
}
return false;
};
const isCaptionsActive = () => {
const enabled = isCaptionsEnabled();
const activated = getCaptionsActive() !== '';
return (enabled && activated);
};
const deactivateCaptions = () => setCaptionsActive('');
const activateCaptions = (locale, settings) => {
setCaptionsSettings(settings);
setCaptionsActive(locale);
};
const amIModerator = () => {
const user = Users.findOne(
{ userId: Auth.userID },
{ fields: { role: 1 } },
);
return user && user.role === ROLE_MODERATOR;
};
const getName = (locale) => {
const captions = Captions.findOne({
meetingId: Auth.meetingID,
locale,
});
return captions.name;
};
const createCaptions = (locale) => {
const name = getName(locale);
PadsService.createGroup(locale, CAPTIONS_CONFIG.id, name);
updateCaptionsOwner(locale, name);
setCaptionsLocale(locale);
};
const hasPermission = () => {
if (amIModerator()) {
const { ownerId } = getCaptions();
return Auth.userID === ownerId;
}
return false;
};
const getDictationStatus = () => {
if (!CAPTIONS_CONFIG.dictation || !amIModerator()) {
return {
locale: '',
dictating: false,
};
}
const captions = Captions.findOne({
meetingId: Auth.meetingID,
ownerId: Auth.userID,
}, {
fields: {
locale: 1,
dictating: 1,
},
});
if (captions) {
return {
locale: captions.locale,
dictating: captions.dictating,
};
}
return {
locale: '',
dictating: false,
};
};
const canIDictateThisPad = (ownerId) => {
if (!CAPTIONS_CONFIG.dictation) return false;
if (ownerId !== Auth.userID) return false;
if (!SpeechService.hasSpeechRecognitionSupport()) return false;
return true;
};
export default {
ID: CAPTIONS_CONFIG.id,
getAvailableLocales,
getOwnedLocales,
updateCaptionsOwner,
startDictation,
stopDictation,
getCaptionsSettings,
getCaptionsData,
getCaptions,
hasPermission,
amICaptionsOwner,
isCaptionsEnabled,
isCaptionsAvailable,
isCaptionsActive,
deactivateCaptions,
activateCaptions,
formatCaptionsText,
amIModerator,
createCaptions,
getCaptionsLocale,
setCaptionsLocale,
getDictationStatus,
canIDictateThisPad,
};