Skip to content

Commit

Permalink
fix(randomizer): update randomizer to use new tts code
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Dec 21, 2021
1 parent 29e7c1f commit f2d285a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/graphql/resolvers/overlayResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class overlayResolver {
group: items.filter(o => o.value === 'group'),
stats: items.filter(o => o.value === 'stats'),
wordcloud: items.filter(o => o.value === 'wordcloud'),
randomizer: items.filter(o => o.value === 'randomizer'),
} as OverlayObject;
return response;
}
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/schema/overlay/OverlayObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class OverlayObject {
carousel: OverlayMapperCarousel[];
@Field(type => [OverlayStatsObject])
stats: OverlayMapperInterface[];
@Field(type => [OverlayStatsObject])
randomizer: OverlayMapperInterface[];
@Field(type => [OverlayWordcloudObject])
wordcloud: OverlayMapperInterface[];
}
36 changes: 1 addition & 35 deletions src/overlays/texttospeech.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { MINUTE } from '@sogebot/ui-helpers/constants';
import { v4 } from 'uuid';

import {
Expand All @@ -8,37 +7,8 @@ import { warning } from '../helpers/log';
import Overlay from './_interface';

import { defaultPermissions } from '~/helpers/permissions/index';
import { publicEndpoint } from '~/helpers/socket';

/* secureKeys are used to authenticate use of public overlay endpoint */
const secureKeys = new Set<string>();

class TextToSpeech extends Overlay {
sockets() {
publicEndpoint(this.nsp, 'speak', async (opts, cb) => {
if (secureKeys.has(opts.key)) {
secureKeys.delete(opts.key);

const { default: tts, services } = await import ('../tts');
if (!tts.ready) {
cb(new Error('TTS is not properly set and ready.'));
return;
}

if (tts.service === services.GOOGLE) {
try {
const audioContent = await tts.googleSpeak(opts);
cb(null, audioContent);
} catch (e) {
cb(e);
}
}
} else {
cb(new Error('Invalid auth.'));
}
});
}

@command('!tts')
@default_permission(defaultPermissions.CASTERS)
async textToSpeech(opts: CommandOptions): Promise<CommandResponse[]> {
Expand All @@ -49,11 +19,7 @@ class TextToSpeech extends Overlay {
key = tts.responsiveVoiceKey;
}
if (tts.service === services.GOOGLE) {
// add secureKey
secureKeys.add(key);
setTimeout(() => {
secureKeys.delete(key);
}, 10 * MINUTE);
tts.addSecureKey(key);
}

this.emit('speak', {
Expand Down
21 changes: 19 additions & 2 deletions src/registries/randomizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Randomizer as RandomizerEntity } from '@entity/randomizer';
import { LOW } from '@sogebot/ui-helpers/constants';
import { getRepository } from 'typeorm';
import { v4 } from 'uuid';

import { parser } from '../decorators';
import Registry from './_interface';
Expand All @@ -19,10 +20,26 @@ class Randomizer extends Registry {

sockets () {
publicEndpoint(this.nsp, 'randomizer::getVisible', async (cb) => {
cb(null, await getRepository(RandomizerEntity).findOne({ where: { isShown: true }, relations: [ 'items'] }));
cb(
null,
await getRepository(RandomizerEntity).findOne({ where: { isShown: true }, relations: [ 'items'] })
);
});
adminEndpoint(this.nsp, 'randomizer::startSpin', async () => {
this.socket?.emit('spin');
const { default: tts, services } = await import ('../tts');
let key = v4();
if (tts.ready) {
if (tts.service === services.RESPONSIVEVOICE) {
key = tts.responsiveVoiceKey;
}
if (tts.service === services.GOOGLE) {
tts.addSecureKey(key);
}
}
this.socket?.emit('spin', {
service: tts.service,
key,
});
});
adminEndpoint(this.nsp, 'randomizer::showById', async (id, cb) => {
try {
Expand Down
35 changes: 34 additions & 1 deletion src/tts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import { MINUTE } from '@sogebot/ui-helpers/constants';
import { JWT } from 'google-auth-library';
import { google } from 'googleapis';
import { getRepository } from 'typeorm';
Expand All @@ -11,7 +12,10 @@ import {
onStartup,
} from '~/decorators/on';
import { error, info, warning } from '~/helpers/log';
import { adminEndpoint } from '~/helpers/socket';
import { adminEndpoint, publicEndpoint } from '~/helpers/socket';

/* secureKeys are used to authenticate use of public overlay endpoint */
const secureKeys = new Set<string>();

export enum services {
'NONE' = -1,
Expand All @@ -33,6 +37,13 @@ class TTS extends Core {
@settings()
googleVoices: string[] = [];

addSecureKey(key: string) {
secureKeys.add(key);
setTimeout(() => {
secureKeys.delete(key);
}, 10 * MINUTE);
}

sockets() {
adminEndpoint(this.nsp, 'settings.refresh', async () => {
this.onStartup(); // reset settings
Expand All @@ -44,6 +55,28 @@ class TTS extends Core {
cb(null, audioContent);
}
});

publicEndpoint(this.nsp, 'speak', async (opts, cb) => {
if (secureKeys.has(opts.key)) {
secureKeys.delete(opts.key);

if (!this.ready) {
cb(new Error('TTS is not properly set and ready.'));
return;
}

if (this.service === services.GOOGLE) {
try {
const audioContent = await this.googleSpeak(opts);
cb(null, audioContent);
} catch (e) {
cb(e);
}
}
} else {
cb(new Error('Invalid auth.'));
}
});
}

@onStartup()
Expand Down

0 comments on commit f2d285a

Please sign in to comment.