2023-08-09 14:16:02 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2023-09-06 13:23:39 +00:00
|
|
|
import type { ChannelConfiguration, ChannelInfos } from '../../../../shared/lib/types'
|
|
|
|
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
|
2023-08-09 14:39:12 +00:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as path from 'path'
|
2023-08-09 14:16:02 +00:00
|
|
|
|
2023-08-09 14:39:12 +00:00
|
|
|
/**
|
2023-09-06 13:23:39 +00:00
|
|
|
* Get saved configuration options for the given channel.
|
2023-08-09 14:39:12 +00:00
|
|
|
* Can throw an exception.
|
|
|
|
* @param options Peertube server options
|
|
|
|
* @param channelInfos Info from channel from which we want to get infos
|
2023-09-06 13:23:39 +00:00
|
|
|
* @returns Channel configuration data
|
2023-08-09 14:39:12 +00:00
|
|
|
*/
|
2023-09-06 13:23:39 +00:00
|
|
|
async function getChannelConfigurationOptions (
|
2023-08-09 14:16:02 +00:00
|
|
|
options: RegisterServerOptions,
|
|
|
|
channelInfos: ChannelInfos
|
2023-09-06 13:23:39 +00:00
|
|
|
): Promise<ChannelConfiguration> {
|
2023-08-09 14:39:12 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
const filePath = _getFilePath(options, channelInfos)
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
logger.debug('No stored data for channel, returning default values')
|
|
|
|
return {
|
|
|
|
channel: channelInfos,
|
2023-09-06 13:23:39 +00:00
|
|
|
configuration: {
|
2023-08-09 14:39:12 +00:00
|
|
|
bot: false,
|
|
|
|
bannedJIDs: [],
|
|
|
|
forbiddenWords: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const content = await fs.promises.readFile(filePath, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
})
|
2023-09-06 13:23:39 +00:00
|
|
|
const sanitized = await sanitizeChannelConfigurationOptions(options, channelInfos, JSON.parse(content))
|
2023-08-09 14:16:02 +00:00
|
|
|
return {
|
|
|
|
channel: channelInfos,
|
2023-09-06 13:23:39 +00:00
|
|
|
configuration: sanitized
|
2023-08-09 14:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 14:39:12 +00:00
|
|
|
/**
|
2023-09-06 13:23:39 +00:00
|
|
|
* Save channel configuration options.
|
2023-08-09 14:39:12 +00:00
|
|
|
* Can throw an exception.
|
2023-09-06 13:23:39 +00:00
|
|
|
* @param options Peertube server options
|
|
|
|
* @param channelConfiguration data to save
|
2023-08-09 14:39:12 +00:00
|
|
|
*/
|
2023-09-06 13:23:39 +00:00
|
|
|
async function storeChannelConfigurationOptions (
|
2023-08-09 14:39:12 +00:00
|
|
|
options: RegisterServerOptions,
|
2023-09-06 13:23:39 +00:00
|
|
|
channelConfiguration: ChannelConfiguration
|
2023-08-09 14:16:02 +00:00
|
|
|
): Promise<void> {
|
2023-09-06 13:23:39 +00:00
|
|
|
const channelInfos = channelConfiguration.channel
|
2023-08-09 14:39:12 +00:00
|
|
|
const filePath = _getFilePath(options, channelInfos)
|
|
|
|
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
const dir = path.dirname(filePath)
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
fs.mkdirSync(dir, { recursive: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 13:23:39 +00:00
|
|
|
const jsonContent = JSON.stringify(channelConfiguration.configuration)
|
2023-08-09 14:39:12 +00:00
|
|
|
|
|
|
|
await fs.promises.writeFile(filePath, jsonContent, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getFilePath (
|
|
|
|
options: RegisterServerOptions,
|
|
|
|
channelInfos: ChannelInfos
|
|
|
|
): string {
|
|
|
|
const channelId = channelInfos.id
|
|
|
|
// some sanitization, just in case...
|
|
|
|
if (!/^\d+$/.test(channelId.toString())) {
|
|
|
|
throw new Error(`Invalid channelId: ${channelId}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.resolve(
|
|
|
|
options.peertubeHelpers.plugin.getDataDirectoryPath(),
|
2023-09-06 13:23:39 +00:00
|
|
|
'channelConfigurationOptions',
|
2023-08-09 14:39:12 +00:00
|
|
|
channelId.toString() + '.json'
|
|
|
|
)
|
2023-08-09 14:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2023-09-06 13:23:39 +00:00
|
|
|
getChannelConfigurationOptions,
|
|
|
|
storeChannelConfigurationOptions
|
2023-08-09 14:16:02 +00:00
|
|
|
}
|