peertube-plugin-livechat/server/lib/configuration/channel/storage.ts

111 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-08-09 14:16:02 +00:00
import type { RegisterServerOptions } from '@peertube/peertube-types'
2023-09-18 10:23:35 +00:00
import type { ChannelConfigurationOptions } from '../../../../shared/lib/types'
import type { ChannelCommonRoomConf } from '../../configuration/bot'
import { RoomChannel } from '../../room-channel'
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
import * as fs from 'fs'
import * as path from 'path'
2023-08-09 14:16:02 +00:00
/**
* Get saved configuration options for the given channel.
* Can throw an exception.
* @param options Peertube server options
* @param channelInfos Info from channel from which we want to get infos
2023-09-18 10:23:35 +00:00
* @returns Channel configuration data, or null if nothing is stored
*/
async function getChannelConfigurationOptions (
2023-08-09 14:16:02 +00:00
options: RegisterServerOptions,
2023-09-18 10:23:35 +00:00
channelId: number | string
): Promise<ChannelConfigurationOptions | null> {
const logger = options.peertubeHelpers.logger
2023-09-18 10:23:35 +00:00
const filePath = _getFilePath(options, channelId)
if (!fs.existsSync(filePath)) {
logger.debug('No stored data for channel, returning default values')
2023-09-18 10:23:35 +00:00
return null
}
const content = await fs.promises.readFile(filePath, {
encoding: 'utf-8'
})
2023-09-18 10:23:35 +00:00
const sanitized = await sanitizeChannelConfigurationOptions(options, channelId, JSON.parse(content))
return sanitized
}
function getDefaultChannelConfigurationOptions (_options: RegisterServerOptions): ChannelConfigurationOptions {
2023-08-09 14:16:02 +00:00
return {
2023-09-18 10:23:35 +00:00
bot: false,
bannedJIDs: [],
forbiddenWords: []
2023-08-09 14:16:02 +00:00
}
}
/**
* Save channel configuration options.
* Can throw an exception.
* @param options Peertube server options
2023-09-18 10:23:35 +00:00
* @param ChannelConfigurationOptions data to save
*/
async function storeChannelConfigurationOptions (
options: RegisterServerOptions,
2023-09-18 10:23:35 +00:00
channelId: number | string,
channelConfigurationOptions: ChannelConfigurationOptions
2023-08-09 14:16:02 +00:00
): Promise<void> {
2023-09-18 10:23:35 +00:00
const filePath = _getFilePath(options, channelId)
if (!fs.existsSync(filePath)) {
const dir = path.dirname(filePath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
2023-09-18 10:23:35 +00:00
const jsonContent = JSON.stringify(channelConfigurationOptions)
await fs.promises.writeFile(filePath, jsonContent, {
encoding: 'utf-8'
})
2023-09-15 15:55:07 +00:00
2023-09-18 10:23:35 +00:00
RoomChannel.singleton().refreshChannelConfigurationOptions(channelId)
}
/**
* 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
* @returns Partial bot room configuration
*/
function channelConfigurationOptionsToBotRoomConf (
options: RegisterServerOptions,
channelConfigurationOptions: ChannelConfigurationOptions
): ChannelCommonRoomConf {
2023-09-15 15:55:07 +00:00
const roomConf = {
2023-09-18 10:23:35 +00:00
enabled: channelConfigurationOptions.bot,
2023-09-15 15:55:07 +00:00
// TODO: nick
handlers: []
}
2023-09-18 10:23:35 +00:00
return roomConf
}
function _getFilePath (
options: RegisterServerOptions,
2023-09-18 10:23:35 +00:00
channelId: number | string
): string {
// some sanitization, just in case...
2023-09-18 10:23:35 +00:00
channelId = parseInt(channelId.toString())
if (isNaN(channelId)) {
throw new Error(`Invalid channelId: ${channelId}`)
}
return path.resolve(
options.peertubeHelpers.plugin.getDataDirectoryPath(),
'channelConfigurationOptions',
channelId.toString() + '.json'
)
2023-08-09 14:16:02 +00:00
}
export {
getChannelConfigurationOptions,
2023-09-18 10:23:35 +00:00
getDefaultChannelConfigurationOptions,
channelConfigurationOptionsToBotRoomConf,
storeChannelConfigurationOptions
2023-08-09 14:16:02 +00:00
}