Demo Bot: first proof of concept.

This commit is contained in:
John Livingston
2021-12-07 13:14:01 +01:00
parent f8ce4e6583
commit 978ee83eee
9 changed files with 307 additions and 25 deletions

View File

@ -49,8 +49,8 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
}
result.messages.push(`Room content will be saved for '${wantedConfig.logExpiration.value}'`)
if (wantedConfig.bots.demo) {
result.messages.push(`The Demo bot is active for videos: ${wantedConfig.bots.demo.join(', ')}`)
if (wantedConfig.bots.demobot) {
result.messages.push(`The Demo bot is active for videos: ${wantedConfig.bots.demobot.join(', ')}`)
}
const configFiles = wantedConfig.getConfigFiles()

View File

@ -24,9 +24,9 @@ async function getWorkingDir (options: RegisterServerOptions): Promise<string> {
/**
* Creates the working dir if needed, and returns it.
*/
async function ensureWorkingDir (options: RegisterServerOptions): Promise<string> {
async function ensureWorkingDirs (options: RegisterServerOptions): Promise<string> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling ensureworkingDir')
logger.debug('Calling ensureworkingDirs')
const paths = await getProsodyFilePaths(options)
const dir = paths.dir
@ -39,10 +39,12 @@ async function ensureWorkingDir (options: RegisterServerOptions): Promise<string
await fs.promises.access(dir, fs.constants.W_OK) // will throw an error if no access
logger.debug(`Write access ok on ${dir}`)
if (!fs.existsSync(paths.data)) {
logger.info(`The data dir ${paths.data} does not exists, trying to create it`)
await fs.promises.mkdir(paths.data)
logger.debug(`Working dir ${paths.data} was created`)
for (const path of [paths.data, paths.bots.dir]) {
if (!fs.existsSync(path)) {
logger.info(`The data dir ${path} does not exists, trying to create it`)
await fs.promises.mkdir(path)
logger.debug(`Working dir ${path} was created`)
}
}
return dir
@ -60,16 +62,19 @@ async function getProsodyFilePaths (options: RegisterServerOptions): Promise<Pro
log: path.resolve(dir, 'prosody.log'),
config: path.resolve(dir, 'prosody.cfg.lua'),
data: path.resolve(dir, 'data'),
bots: path.resolve(dir, 'bots'),
bots: {
dir: path.resolve(dir, 'bots'),
demobot: path.resolve(dir, 'bots', 'demobot.js')
},
modules: path.resolve(__dirname, '../../prosody-modules')
}
}
interface ProsodyConfigBots {
demo?: string[] // if the demo bot is activated, here are the video UUIDS where it will be.
demobot?: string[] // if the demo bot is activated, here are the video UUIDS where it will be.
}
type ProsodyConfigFilesKey = 'prosody'
type ProsodyConfigFilesKey = 'prosody' | 'demobot'
type ProsodyConfigFiles = Array<{
key: ProsodyConfigFilesKey
path: string
@ -128,6 +133,10 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
if (!/^\d+$/.test(port)) {
throw new Error('Invalid port')
}
const externalComponentsPort = (settings['prosody-component-port'] as string) || '53470'
if (!/^\d+$/.test(externalComponentsPort)) {
throw new Error('Invalid external components port')
}
const logByDefault = (settings['prosody-muc-log-by-default'] as boolean) ?? true
const logExpirationSetting = (settings['prosody-muc-expiration'] as string) ?? DEFAULTLOGEXPIRATION
const enableC2s = (settings['prosody-c2s'] as boolean) || false
@ -189,19 +198,27 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
config.setLog(logLevel)
const demoBotUUIDs = parseConfigDemoBotUUIDs((settings['chat-videos-list'] as string) || '')
let demoBotContentObj: string = JSON.stringify({})
if (demoBotUUIDs?.length > 0) {
useExternalComponents = true
const componentSecret = await getExternalComponentKey(options, 'DEMOBOT')
valuesToHideInDiagnostic.ComponentSecret = componentSecret
config.useDemoBot(componentSecret)
bots.demo = demoBotUUIDs
bots.demobot = demoBotUUIDs
demoBotContentObj = JSON.stringify({
UUIDs: demoBotUUIDs,
service: 'xmpp://127.0.0.1:' + externalComponentsPort,
domain: 'demobot.' + prosodyDomain,
mucDomain: 'room.' + prosodyDomain,
password: componentSecret
})
}
let demoBotContent = '"use strict";\n'
demoBotContent += 'Object.defineProperty(exports, "__esModule", { value: true });\n'
demoBotContent += `function getConf () { return ${demoBotContentObj}; }` + '\n'
demoBotContent += 'exports.getConf = getConf;\n'
if (useExternalComponents) {
const externalComponentsPort = (settings['prosody-component-port'] as string) || '53470'
if (!/^\d+$/.test(externalComponentsPort)) {
throw new Error('Invalid external components port')
}
config.useExternalComponents(externalComponentsPort)
}
@ -213,6 +230,11 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
key: 'prosody',
path: paths.config,
content: content
},
{
key: 'demobot',
path: paths.bots.demobot,
content: demoBotContent
}
],
paths,
@ -232,7 +254,7 @@ async function writeProsodyConfig (options: RegisterServerOptions): Promise<Pros
logger.debug('Calling writeProsodyConfig')
logger.debug('Ensuring that the working dir exists')
await ensureWorkingDir(options)
await ensureWorkingDirs(options)
logger.debug('Computing the Prosody config content')
const config = await getProsodyConfig(options)
@ -303,7 +325,7 @@ function readLogExpiration (options: RegisterServerOptions, logExpiration: strin
export {
getProsodyConfig,
getWorkingDir,
ensureWorkingDir,
ensureWorkingDirs,
getProsodyFilePaths,
writeProsodyConfig
}

View File

@ -291,13 +291,13 @@ class ProsodyConfigContent {
}
useDemoBot (componentSecret: string): void {
const demoBot = new ProsodyConfigComponent('demobot.' + this.prosodyDomain)
demoBot.set('component_secret', componentSecret)
const demoBotComponent = new ProsodyConfigComponent('demobot.' + this.prosodyDomain)
demoBotComponent.set('component_secret', componentSecret)
// If we want the bot to be moderator, should do the trick:
// this.global.add('admins', 'demobot.' + this.prosodyDomain)
this.externalComponents.push(demoBot)
this.externalComponents.push(demoBotComponent)
}
setLog (level: ProsodyLogLevel, syslog?: ProsodyLogLevel[]): void {

View File

@ -5,7 +5,10 @@ interface ProsodyFilePaths {
log: string
config: string
data: string
bots: string
bots: {
dir: string
demobot: string
}
modules: string
}