Channel conf UI WIP

This commit is contained in:
John Livingston
2023-09-22 16:30:12 +02:00
parent af115e984b
commit 562073fc09
6 changed files with 204 additions and 68 deletions

View File

@ -103,6 +103,21 @@ class BotConfiguration {
return singleton
}
/**
* Get the current room conf content.
* @param roomJIDParam room JID (local or full)
* @returns the room conf, or null if does not exist
*/
public async getRoom (roomJIDParam: string): Promise<ChannelCommonRoomConf | null> {
const roomJID = this._canonicJID(roomJIDParam)
if (!roomJID) {
this.logger.error('Invalid room JID')
return null
}
const conf = await this._getRoomConf(roomJID)
return conf
}
/**
* Update the bot configuration for a given room.
* @param roomJIDParam Room full or local JID

View File

@ -166,9 +166,9 @@ function _readQuotes (botData: any): ChannelConfigurationOptions['bot']['quotes'
throw new Error('Invalid quotes data')
}
const result: ChannelConfigurationOptions['bot']['quotes'] = []
for (const fw of botData.quotes) {
const messages = _readStringArray(fw, 'message')
const delay = _readInteger(fw, 'delay', 1, 6000)
for (const qs of botData.quotes) {
const messages = _readStringArray(qs, 'messages')
const delay = _readInteger(qs, 'delay', 1, 6000)
result.push({
messages,
@ -183,9 +183,9 @@ function _readCommands (botData: any): ChannelConfigurationOptions['bot']['comma
throw new Error('Invalid commands data')
}
const result: ChannelConfigurationOptions['bot']['commands'] = []
for (const fw of botData.commands) {
const message = _readSimpleInput(fw, 'message')
const command = _readSimpleInput(fw, 'command')
for (const cs of botData.commands) {
const message = _readSimpleInput(cs, 'message')
const command = _readSimpleInput(cs, 'command')
result.push({
message,

View File

@ -39,39 +39,9 @@ function getDefaultChannelConfigurationOptions (_options: RegisterServerOptions)
bot: {
enabled: false,
nickname: 'Sepia',
// Note: we are instanciating several data for forbiddenWords, quotes and commands.
// This will be used by the frontend to instanciates requires fields
forbiddenWords: [
{
entries: []
},
{
entries: []
},
{
entries: []
}
],
quotes: [
{
messages: [],
delay: 5 * 60 // seconds to minutes
}
],
commands: [
{
command: '',
message: ''
},
{
command: '',
message: ''
},
{
command: '',
message: ''
}
]
forbiddenWords: [],
quotes: [],
commands: []
}
}
}
@ -109,22 +79,47 @@ async function storeChannelConfigurationOptions (
* Converts the channel configuration to the bot room configuration object (minus the room JID and domain)
* @param options server options
* @param channelConfigurationOptions The channel configuration
* @param previousRoomConf the previous saved room conf, if available. Used to merge handlers.
* @returns Partial bot room configuration
*/
function channelConfigurationOptionsToBotRoomConf (
options: RegisterServerOptions,
channelConfigurationOptions: ChannelConfigurationOptions
channelConfigurationOptions: ChannelConfigurationOptions,
previousRoomConf: ChannelCommonRoomConf | null
): ChannelCommonRoomConf {
// Note concerning handlers:
// If we want the bot to correctly enable/disable the handlers,
// we must always define all handlers, even if not used.
// That's why we are gathering handlers ids in handlersId, and disabling missing handlers at the end of this function.
const handlersIds: Map<string, true> = new Map<string, true>()
const handlers: ConfigHandlers = []
channelConfigurationOptions.bot.forbiddenWords.forEach((v, i) => {
handlers.push(_getForbiddenWordsHandler(
'forbidden_words_' + i.toString(),
channelConfigurationOptions.bot.forbiddenWords[i]
))
const id = 'forbidden_words_' + i.toString()
handlersIds.set(id, true)
handlers.push(_getForbiddenWordsHandler(id, v))
})
channelConfigurationOptions.bot.quotes.forEach((v, i) => {
const id = 'quote_' + i.toString()
handlersIds.set(id, true)
handlers.push(_getQuotesHandler(id, v))
})
channelConfigurationOptions.bot.commands.forEach((v, i) => {
const id = 'command_' + i.toString()
handlersIds.set(id, true)
handlers.push(_getCommandsHandler(id, v))
})
// Disabling missing handlers:
if (previousRoomConf) {
for (const handler of previousRoomConf.handlers) {
if (!handlersIds.has(handler.id)) {
// cloning to avoid issues...
const disabledHandler = JSON.parse(JSON.stringify(handler))
disabledHandler.enabled = false
handlers.push(disabledHandler)
}
}
}
const roomConf: ChannelCommonRoomConf = {
enabled: channelConfigurationOptions.bot.enabled,
@ -192,6 +187,52 @@ function _getForbiddenWordsHandler (
return handler
}
function _getQuotesHandler (
id: string,
quotes: ChannelConfigurationOptions['bot']['quotes'][0]
): ConfigHandler {
const handler: ConfigHandler = {
type: 'quotes_random',
id,
enabled: false,
options: {
quotes: [],
delay: 5 * 60
}
}
if (quotes.messages.length === 0) {
return handler
}
handler.enabled = true
handler.options.quotes = quotes.messages
handler.options.delay = quotes.delay
return handler
}
function _getCommandsHandler (
id: string,
command: ChannelConfigurationOptions['bot']['commands'][0]
): ConfigHandler {
const handler: ConfigHandler = {
type: 'command_say',
id,
enabled: false,
options: {
quotes: [],
command: 'undefined' // This is arbitrary, and does not matter as enabled=false
}
}
if (!command.message || command.message === '') {
return handler
}
handler.enabled = true
handler.options.command = command.command
handler.options.quotes = [command.message]
return handler
}
const stringToWordRegexpSpecials = [
// order matters for these
'-', '[', ']',

View File

@ -334,12 +334,13 @@ class RoomChannel {
}
this.logger.info(`Room ${roomJID} has associated channel options, writing it`)
const previousRoomConf = await BotConfiguration.singleton().getRoom(roomJID)
const botConf: RoomConf = Object.assign(
{
local: roomJID,
domain: this.mucDomain
},
channelConfigurationOptionsToBotRoomConf(this.options, channelConfigurationOptions)
channelConfigurationOptionsToBotRoomConf(this.options, channelConfigurationOptions, previousRoomConf)
)
await BotConfiguration.singleton().updateRoom(roomJID, botConf)