Moderation configuration screen: store options.
This commit is contained in:
parent
0987a036a0
commit
7e89e430d0
@ -1,25 +1,85 @@
|
|||||||
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
||||||
import type { ChannelModeration, ChannelInfos } from '../../../../shared/lib/types'
|
import type { ChannelModeration, ChannelInfos } from '../../../../shared/lib/types'
|
||||||
|
import { sanitizeChannelModerationOptions } from '../../moderation/channel/sanitize'
|
||||||
|
import * as fs from 'fs'
|
||||||
|
import * as path from 'path'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get saved moderation 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
|
||||||
|
* @returns Channel moderation data
|
||||||
|
*/
|
||||||
async function getChannelModerationOptions (
|
async function getChannelModerationOptions (
|
||||||
options: RegisterServerOptions,
|
options: RegisterServerOptions,
|
||||||
channelInfos: ChannelInfos
|
channelInfos: ChannelInfos
|
||||||
): Promise<ChannelModeration> {
|
): Promise<ChannelModeration> {
|
||||||
|
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,
|
||||||
|
moderation: {
|
||||||
|
bot: false,
|
||||||
|
bannedJIDs: [],
|
||||||
|
forbiddenWords: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const content = await fs.promises.readFile(filePath, {
|
||||||
|
encoding: 'utf-8'
|
||||||
|
})
|
||||||
|
const sanitized = await sanitizeChannelModerationOptions(options, channelInfos, JSON.parse(content))
|
||||||
return {
|
return {
|
||||||
channel: channelInfos,
|
channel: channelInfos,
|
||||||
moderation: {
|
moderation: sanitized
|
||||||
bot: false,
|
|
||||||
bannedJIDs: [],
|
|
||||||
forbiddenWords: []
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save channel moderation options.
|
||||||
|
* Can throw an exception.
|
||||||
|
* @param _options Peertube server options
|
||||||
|
* @param _channelModeration data to save
|
||||||
|
*/
|
||||||
async function storeChannelModerationOptions (
|
async function storeChannelModerationOptions (
|
||||||
_options: RegisterServerOptions,
|
options: RegisterServerOptions,
|
||||||
_channelModeration: ChannelModeration
|
channelModeration: ChannelModeration
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
throw new Error('Not implemented yet')
|
const channelInfos = channelModeration.channel
|
||||||
|
const filePath = _getFilePath(options, channelInfos)
|
||||||
|
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
const dir = path.dirname(filePath)
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const jsonContent = JSON.stringify(channelModeration.moderation)
|
||||||
|
|
||||||
|
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(),
|
||||||
|
'channelModerationOptions',
|
||||||
|
channelId.toString() + '.json'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
@ -47,10 +47,11 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('Data seems ok, storing them.')
|
logger.debug('Data seems ok, storing them.')
|
||||||
const result = await storeChannelModerationOptions(options, {
|
const result = {
|
||||||
channel: channelInfos,
|
channel: channelInfos,
|
||||||
moderation
|
moderation
|
||||||
})
|
}
|
||||||
|
await storeChannelModerationOptions(options, result)
|
||||||
res.status(200)
|
res.status(200)
|
||||||
res.json(result)
|
res.json(result)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user