Renaming 'moderation' pages to 'configuration'.

This commit is contained in:
John Livingston 2023-09-06 15:23:39 +02:00
parent 85e7598c1f
commit 5373fb1570
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
13 changed files with 132 additions and 130 deletions

View File

@ -34,13 +34,13 @@ declare const LOC_CONNECT_USING_XMPP_HELP: string
declare const LOC_SAVE: string declare const LOC_SAVE: string
declare const LOC_CANCEL: string declare const LOC_CANCEL: string
declare const LOC_SUCCESSFULLY_SAVED: string declare const LOC_SUCCESSFULLY_SAVED: string
declare const LOC_MENU_MODERATION_LABEL: string declare const LOC_MENU_CONFIGURATION_LABEL: string
declare const LOC_LIVECHAT_MODERATION_TITLE: string declare const LOC_LIVECHAT_CONFIGURATION_TITLE: string
declare const LOC_LIVECHAT_MODERATION_DESC: string declare const LOC_LIVECHAT_CONFIGURATION_DESC: string
declare const LOC_LIVECHAT_MODERATION_PLEASE_SELECT: string declare const LOC_LIVECHAT_CONFIGURATION_PLEASE_SELECT: string
declare const LOC_LIVECHAT_MODERATION_CHANNEL_TITLE: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_TITLE: string
declare const LOC_LIVECHAT_MODERATION_CHANNEL_DESC: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_DESC: string
declare const LOC_LIVECHAT_MODERATION_CHANNEL_ENABLE_BOT_LABEL: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_ENABLE_BOT_LABEL: string
declare const LOC_LIVECHAT_MODERATION_CHANNEL_BOT_OPTIONS_TITLE: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_BOT_OPTIONS_TITLE: string
declare const LOC_LIVECHAT_MODERATION_CHANNEL_FORBIDDEN_WORDS_LABEL: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL: string
declare const LOC_LIVECHAT_MODERATION_CHANNEL_BANNED_JIDS_LABEL: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_BANNED_JIDS_LABEL: string

View File

@ -1,6 +1,6 @@
import type { RegisterClientOptions } from '@peertube/peertube-types/client' import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { RegisterClientFormFieldOptions } from '@peertube/peertube-types' import type { RegisterClientFormFieldOptions } from '@peertube/peertube-types'
import { registerModeration } from './common/moderation/register' import { registerConfiguration } from './common/configuration/register'
async function register (clientOptions: RegisterClientOptions): Promise<void> { async function register (clientOptions: RegisterClientOptions): Promise<void> {
const { peertubeHelpers, registerHook, registerVideoField } = clientOptions const { peertubeHelpers, registerHook, registerVideoField } = clientOptions
@ -52,7 +52,7 @@ async function register (clientOptions: RegisterClientOptions): Promise<void> {
registerVideoField(webchatFieldOptions, { type: 'update' }) registerVideoField(webchatFieldOptions, { type: 'update' })
registerVideoField(webchatFieldOptions, { type: 'go-live' }) registerVideoField(webchatFieldOptions, { type: 'go-live' })
await registerModeration(clientOptions) await registerConfiguration(clientOptions)
} }
export { export {

View File

@ -1,23 +1,23 @@
import type { RegisterClientOptions } from '@peertube/peertube-types/client' import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { ChannelModerationOptions } from 'shared/lib/types' import type { ChannelConfigurationOptions } from 'shared/lib/types'
import { getBaseRoute } from '../../../videowatch/uri' import { getBaseRoute } from '../../../videowatch/uri'
/** /**
* Adds the front-end logic on the generated html for the channel moderation options. * Adds the front-end logic on the generated html for the channel configuration options.
* @param clientOptions Peertube client options * @param clientOptions Peertube client options
* @param rootEl The root element in which the template was rendered * @param rootEl The root element in which the template was rendered
*/ */
async function vivifyModerationChannel ( async function vivifyConfigurationChannel (
clientOptions: RegisterClientOptions, clientOptions: RegisterClientOptions,
rootEl: HTMLElement, rootEl: HTMLElement,
channelId: string channelId: string
): Promise<void> { ): Promise<void> {
const form = rootEl.querySelector('form[livechat-moderation-channel-options]') as HTMLFormElement const form = rootEl.querySelector('form[livechat-configuration-channel-options]') as HTMLFormElement
if (!form) { return } if (!form) { return }
const labelSaved = await clientOptions.peertubeHelpers.translate(LOC_SUCCESSFULLY_SAVED) const labelSaved = await clientOptions.peertubeHelpers.translate(LOC_SUCCESSFULLY_SAVED)
const labelError = await clientOptions.peertubeHelpers.translate(LOC_ERROR) const labelError = await clientOptions.peertubeHelpers.translate(LOC_ERROR)
const enableBotCB = form.querySelector('input[name=bot]') as HTMLInputElement const enableBotCB = form.querySelector('input[name=bot]') as HTMLInputElement
const botEnabledEl = form.querySelector('[livechat-moderation-channel-options-bot-enabled]') as HTMLElement const botEnabledEl = form.querySelector('[livechat-configuration-channel-options-bot-enabled]') as HTMLElement
const refresh: Function = () => { const refresh: Function = () => {
botEnabledEl.style.display = enableBotCB.checked ? 'initial' : 'none' botEnabledEl.style.display = enableBotCB.checked ? 'initial' : 'none'
@ -25,7 +25,7 @@ async function vivifyModerationChannel (
const submitForm: Function = async () => { const submitForm: Function = async () => {
const data = new FormData(form) const data = new FormData(form)
const channelModerationOptions: ChannelModerationOptions = { const channelConfigurationOptions: ChannelConfigurationOptions = {
bot: data.get('bot') === '1', bot: data.get('bot') === '1',
bannedJIDs: (data.get('banned_jids')?.toString() ?? '').split(/\r?\n|\r|\n/g), bannedJIDs: (data.get('banned_jids')?.toString() ?? '').split(/\r?\n|\r|\n/g),
forbiddenWords: (data.get('forbidden_words')?.toString() ?? '').split(/\r?\n|\r|\n/g) forbiddenWords: (data.get('forbidden_words')?.toString() ?? '').split(/\r?\n|\r|\n/g)
@ -35,16 +35,16 @@ async function vivifyModerationChannel (
headers['content-type'] = 'application/json;charset=UTF-8' headers['content-type'] = 'application/json;charset=UTF-8'
const response = await fetch( const response = await fetch(
getBaseRoute(clientOptions) + '/api/moderation/channel/' + encodeURIComponent(channelId), getBaseRoute(clientOptions) + '/api/configuration/channel/' + encodeURIComponent(channelId),
{ {
method: 'POST', method: 'POST',
headers, headers,
body: JSON.stringify(channelModerationOptions) body: JSON.stringify(channelConfigurationOptions)
} }
) )
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to save moderation options.') throw new Error('Failed to save configuration options.')
} }
} }
const toggleSubmit: Function = (disabled: boolean) => { const toggleSubmit: Function = (disabled: boolean) => {
@ -80,5 +80,5 @@ async function vivifyModerationChannel (
} }
export { export {
vivifyModerationChannel vivifyConfigurationChannel
} }

View File

@ -1,42 +1,42 @@
import type { RegisterClientOptions } from '@peertube/peertube-types/client' import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import { renderModerationHome } from './templates/home' import { renderConfigurationHome } from './templates/home'
import { renderModerationChannel } from './templates/channel' import { renderConfigurationChannel } from './templates/channel'
import { vivifyModerationChannel } from './logic/channel' import { vivifyConfigurationChannel } from './logic/channel'
/** /**
* Registers stuff related to the moderation settings. * Registers stuff related to the user's configuration pages.
* @param clientOptions Peertube client options * @param clientOptions Peertube client options
*/ */
async function registerModeration (clientOptions: RegisterClientOptions): Promise<void> { async function registerConfiguration (clientOptions: RegisterClientOptions): Promise<void> {
const { peertubeHelpers, registerClientRoute, registerHook } = clientOptions const { peertubeHelpers, registerClientRoute, registerHook } = clientOptions
registerClientRoute({ registerClientRoute({
route: 'livechat/moderation', route: 'livechat/configuration',
onMount: async ({ rootEl }) => { onMount: async ({ rootEl }) => {
rootEl.innerHTML = await renderModerationHome(clientOptions) rootEl.innerHTML = await renderConfigurationHome(clientOptions)
} }
}) })
registerClientRoute({ registerClientRoute({
route: 'livechat/moderation/channel', route: 'livechat/configuration/channel',
onMount: async ({ rootEl }) => { onMount: async ({ rootEl }) => {
const urlParams = new URLSearchParams(window.location.search) const urlParams = new URLSearchParams(window.location.search)
const channelId = urlParams.get('channelId') ?? '' const channelId = urlParams.get('channelId') ?? ''
const html = await renderModerationChannel(clientOptions, channelId) const html = await renderConfigurationChannel(clientOptions, channelId)
if (!html) { if (!html) {
// renderModerationChannel has already used the notifier to display an error // renderConfigurationChannel has already used the notifier to display an error
rootEl.innerHTML = '' rootEl.innerHTML = ''
return return
} }
rootEl.innerHTML = html rootEl.innerHTML = html
await vivifyModerationChannel(clientOptions, rootEl, channelId) await vivifyConfigurationChannel(clientOptions, rootEl, channelId)
} }
}) })
registerHook({ registerHook({
target: 'filter:left-menu.links.create.result', target: 'filter:left-menu.links.create.result',
handler: async (links: any) => { handler: async (links: any) => {
// Adding the links to livechat/moderation for logged users. // Adding the links to livechat/configuration for logged users.
if (!peertubeHelpers.isLoggedIn()) { return links } if (!peertubeHelpers.isLoggedIn()) { return links }
if (!Array.isArray(links)) { return links } if (!Array.isArray(links)) { return links }
@ -52,11 +52,11 @@ async function registerModeration (clientOptions: RegisterClientOptions): Promis
if (!myLibraryLinks) { return links } if (!myLibraryLinks) { return links }
if (!Array.isArray(myLibraryLinks.links)) { return links } if (!Array.isArray(myLibraryLinks.links)) { return links }
const label = await peertubeHelpers.translate(LOC_MENU_MODERATION_LABEL) const label = await peertubeHelpers.translate(LOC_MENU_CONFIGURATION_LABEL)
myLibraryLinks.links.push({ myLibraryLinks.links.push({
label, label,
shortLabel: label, shortLabel: label,
path: '/p/livechat/moderation', path: '/p/livechat/configuration',
icon: 'live' icon: 'live'
}) })
return links return links
@ -65,5 +65,5 @@ async function registerModeration (clientOptions: RegisterClientOptions): Promis
} }
export { export {
registerModeration registerConfiguration
} }

View File

@ -1,16 +1,16 @@
import type { RegisterClientOptions } from '@peertube/peertube-types/client' import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { ChannelModeration } from 'shared/lib/types' import type { ChannelConfiguration } from 'shared/lib/types'
import { getBaseRoute } from '../../../videowatch/uri' import { getBaseRoute } from '../../../videowatch/uri'
// Must use require for mustache, import seems buggy. // Must use require for mustache, import seems buggy.
const Mustache = require('mustache') const Mustache = require('mustache')
/** /**
* Renders the moderation settings page for a given channel. * Renders the configuration settings page for a given channel.
* @param registerClientOptions Peertube client options * @param registerClientOptions Peertube client options
* @param channelId The channel id * @param channelId The channel id
* @returns The page content * @returns The page content
*/ */
async function renderModerationChannel ( async function renderConfigurationChannel (
registerClientOptions: RegisterClientOptions, registerClientOptions: RegisterClientOptions,
channelId: string channelId: string
): Promise<string | false> { ): Promise<string | false> {
@ -22,63 +22,65 @@ async function renderModerationChannel (
} }
const response = await fetch( const response = await fetch(
getBaseRoute(registerClientOptions) + '/api/moderation/channel/' + encodeURIComponent(channelId), getBaseRoute(registerClientOptions) + '/api/configuration/channel/' + encodeURIComponent(channelId),
{ {
method: 'GET', method: 'GET',
headers: peertubeHelpers.getAuthHeader() headers: peertubeHelpers.getAuthHeader()
} }
) )
if (!response.ok) { if (!response.ok) {
throw new Error('Can\'t get channel moderation options.') throw new Error('Can\'t get channel configuration options.')
} }
const channelModeration: ChannelModeration = await (response).json() const channelConfiguration: ChannelConfiguration = await (response).json()
// Basic testing that channelModeration has the correct format // Basic testing that channelConfiguration has the correct format
if ((typeof channelModeration !== 'object') || !channelModeration.channel) { if ((typeof channelConfiguration !== 'object') || !channelConfiguration.channel) {
throw new Error('Invalid channel moderation options.') throw new Error('Invalid channel configuration options.')
} }
const view = { const view = {
title: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_TITLE), title: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_TITLE),
description: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_DESC), description: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_DESC),
enableBot: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_ENABLE_BOT_LABEL), enableBot: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_ENABLE_BOT_LABEL),
botOptions: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_BOT_OPTIONS_TITLE), botOptions: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_BOT_OPTIONS_TITLE),
forbiddenWords: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_FORBIDDEN_WORDS_LABEL), forbiddenWords: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL),
bannedJIDs: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_BANNED_JIDS_LABEL), bannedJIDs: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_BANNED_JIDS_LABEL),
save: await peertubeHelpers.translate(LOC_SAVE), save: await peertubeHelpers.translate(LOC_SAVE),
cancel: await peertubeHelpers.translate(LOC_CANCEL), cancel: await peertubeHelpers.translate(LOC_CANCEL),
channelModeration channelConfiguration
} }
return Mustache.render(` return Mustache.render(`
<div class="margin-content"> <div class="margin-content">
<h1>{{title}} {{channelModeration.channel.displayName}}</h1> <h1>{{title}} {{channelConfiguration.channel.displayName}}</h1>
<p>{{description}}</p> <p>{{description}}</p>
<form livechat-moderation-channel-options> <form livechat-configuration-channel-options>
<fieldset> <fieldset>
<label> <label>
<input <input
type="checkbox" name="bot" type="checkbox" name="bot"
value="1" value="1"
{{#channelModeration.moderation.bot}} checked="checked" {{/channelModeration.moderation.bot}} {{#channelConfiguration.configuration.bot}}
checked="checked"
{{/channelConfiguration.configuration.bot}}
/> />
{{enableBot}} {{enableBot}}
</label> </label>
</fieldset> </fieldset>
<fieldset livechat-moderation-channel-options-bot-enabled> <fieldset livechat-configuration-channel-options-bot-enabled>
<legend>{{botOptions}}</legend> <legend>{{botOptions}}</legend>
<label> <label>
{{forbiddenWords}} {{forbiddenWords}}
<textarea name="forbidden_words"> <textarea name="forbidden_words">
{{#channelModeration.moderation.forbiddenWords}}{{.}} {{#channelConfiguration.configuration.forbiddenWords}}{{.}}
{{/channelModeration.moderation.forbiddenWords}} {{/channelConfiguration.configuration.forbiddenWords}}
</textarea> </textarea>
</label> </label>
<label> <label>
{{bannedJIDs}} {{bannedJIDs}}
<textarea name="banned_jids"> <textarea name="banned_jids">
{{#channelModeration.moderation.bannedJIDs}}{{.}} {{#channelConfiguration.configuration.bannedJIDs}}{{.}}
{{/channelModeration.moderation.bannedJIDs}} {{/channelConfiguration.configuration.bannedJIDs}}
</textarea> </textarea>
</label> </label>
</fieldset> </fieldset>
@ -94,5 +96,5 @@ async function renderModerationChannel (
} }
export { export {
renderModerationChannel renderConfigurationChannel
} }

View File

@ -3,11 +3,11 @@ import type { RegisterClientOptions } from '@peertube/peertube-types/client'
const Mustache = require('mustache') const Mustache = require('mustache')
/** /**
* Renders the livechat moderation setup home page. * Renders the livechat configuration setup home page.
* @param registerClientOptions Peertube client options * @param registerClientOptions Peertube client options
* @returns The page content * @returns The page content
*/ */
async function renderModerationHome (registerClientOptions: RegisterClientOptions): Promise<string> { async function renderConfigurationHome (registerClientOptions: RegisterClientOptions): Promise<string> {
const { peertubeHelpers } = registerClientOptions const { peertubeHelpers } = registerClientOptions
try { try {
@ -29,13 +29,13 @@ async function renderModerationHome (registerClientOptions: RegisterClientOption
} }
for (const channel of channels.data) { for (const channel of channels.data) {
channel.livechatModerationUri = '/p/livechat/moderation/channel?channelId=' + encodeURIComponent(channel.id) channel.livechatConfigurationUri = '/p/livechat/configuration/channel?channelId=' + encodeURIComponent(channel.id)
} }
const view = { const view = {
title: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_TITLE), title: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_TITLE),
description: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_DESC), description: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_DESC),
please_select: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_PLEASE_SELECT), please_select: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_PLEASE_SELECT),
channels: channels.data channels: channels.data
} }
@ -47,7 +47,7 @@ async function renderModerationHome (registerClientOptions: RegisterClientOption
<ul> <ul>
{{#channels}} {{#channels}}
<li> <li>
<a href="{{livechatModerationUri}}"> <a href="{{livechatConfigurationUri}}">
{{displayName}} {{displayName}}
</a> </a>
</li> </li>
@ -62,5 +62,5 @@ async function renderModerationHome (registerClientOptions: RegisterClientOption
} }
export { export {
renderModerationHome renderConfigurationHome
} }

View File

@ -296,14 +296,14 @@ prosody_components_list_description: |
save: "Save" save: "Save"
cancel: "Cancel" cancel: "Cancel"
successfully_saved: "Successfully saved" successfully_saved: "Successfully saved"
menu_moderation_label: "Chatrooms" menu_configuration_label: "Chatrooms"
livechat_moderation_title: "Configure your live's chatrooms moderation policies" livechat_configuration_title: "Configure your live's chatrooms"
livechat_moderation_desc: "Here you can configure some advanced options for chatrooms associated to your live streams." livechat_configuration_desc: "Here you can configure some advanced options for chatrooms associated to your live streams."
livechat_moderation_please_select: "Please select bellow one of your channel, to setup its chatting options." livechat_configuration_please_select: "Please select bellow one of your channel, to setup its chatting options."
livechat_moderation_channel_title: "Moderation policies for channel:" livechat_configuration_channel_title: "Moderation policies for channel:"
livechat_moderation_channel_desc: "You can setup here your moderation policies for this channel." livechat_configuration_channel_desc: "You can setup here your moderation policies for this channel."
livechat_moderation_channel_enable_bot_label: "Enable moderation bot" livechat_configuration_channel_enable_bot_label: "Enable moderation bot"
livechat_moderation_channel_bot_options_title: "Moderation bot options" livechat_configuration_channel_bot_options_title: "Moderation bot options"
livechat_moderation_channel_forbidden_words_label: "Forbidden words or expressions" livechat_configuration_channel_forbidden_words_label: "Forbidden words or expressions"
livechat_moderation_channel_banned_jids_label: "Banned users and patterns" livechat_configuration_channel_banned_jids_label: "Banned users and patterns"

View File

@ -1,19 +1,19 @@
import type { RegisterServerOptions } from '@peertube/peertube-types' import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { ChannelModerationOptions, ChannelInfos } from '../../../../shared/lib/types' import type { ChannelConfigurationOptions, ChannelInfos } from '../../../../shared/lib/types'
/** /**
* Sanitize data so that they can safely be used/stored for channel moderation configuration. * Sanitize data so that they can safely be used/stored for channel configuration configuration.
* Throw an error if the format is obviously wrong. * Throw an error if the format is obviously wrong.
* Cleans data (removing empty values, ...) * Cleans data (removing empty values, ...)
* @param options Peertube server options * @param options Peertube server options
* @param _channelInfos Channel infos * @param _channelInfos Channel infos
* @param data Input data * @param data Input data
*/ */
async function sanitizeChannelModerationOptions ( async function sanitizeChannelConfigurationOptions (
_options: RegisterServerOptions, _options: RegisterServerOptions,
_channelInfos: ChannelInfos, _channelInfos: ChannelInfos,
data: any data: any
): Promise<ChannelModerationOptions> { ): Promise<ChannelConfigurationOptions> {
const result = { const result = {
bot: false, bot: false,
bannedJIDs: [], bannedJIDs: [],
@ -28,7 +28,7 @@ async function sanitizeChannelModerationOptions (
if (!(f in data) || (typeof data[f] !== 'boolean')) { if (!(f in data) || (typeof data[f] !== 'boolean')) {
throw new Error('Invalid data type for field ' + f) throw new Error('Invalid data type for field ' + f)
} }
result[f as keyof ChannelModerationOptions] = data[f] result[f as keyof ChannelConfigurationOptions] = data[f]
} }
// value/regexp array fields // value/regexp array fields
for (const f of ['bannedJIDs', 'forbiddenWords']) { for (const f of ['bannedJIDs', 'forbiddenWords']) {
@ -50,7 +50,7 @@ async function sanitizeChannelModerationOptions (
} catch (_err) { } catch (_err) {
throw new Error('Invalid value in field ' + f) throw new Error('Invalid value in field ' + f)
} }
(result[f as keyof ChannelModerationOptions] as string[]).push(v) (result[f as keyof ChannelConfigurationOptions] as string[]).push(v)
} }
} }
@ -58,5 +58,5 @@ async function sanitizeChannelModerationOptions (
} }
export { export {
sanitizeChannelModerationOptions sanitizeChannelConfigurationOptions
} }

View File

@ -1,27 +1,27 @@
import type { RegisterServerOptions } from '@peertube/peertube-types' import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { ChannelModeration, ChannelInfos } from '../../../../shared/lib/types' import type { ChannelConfiguration, ChannelInfos } from '../../../../shared/lib/types'
import { sanitizeChannelModerationOptions } from '../../moderation/channel/sanitize' import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
import * as fs from 'fs' import * as fs from 'fs'
import * as path from 'path' import * as path from 'path'
/** /**
* Get saved moderation options for the given channel. * Get saved configuration options for the given channel.
* Can throw an exception. * Can throw an exception.
* @param options Peertube server options * @param options Peertube server options
* @param channelInfos Info from channel from which we want to get infos * @param channelInfos Info from channel from which we want to get infos
* @returns Channel moderation data * @returns Channel configuration data
*/ */
async function getChannelModerationOptions ( async function getChannelConfigurationOptions (
options: RegisterServerOptions, options: RegisterServerOptions,
channelInfos: ChannelInfos channelInfos: ChannelInfos
): Promise<ChannelModeration> { ): Promise<ChannelConfiguration> {
const logger = options.peertubeHelpers.logger const logger = options.peertubeHelpers.logger
const filePath = _getFilePath(options, channelInfos) const filePath = _getFilePath(options, channelInfos)
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
logger.debug('No stored data for channel, returning default values') logger.debug('No stored data for channel, returning default values')
return { return {
channel: channelInfos, channel: channelInfos,
moderation: { configuration: {
bot: false, bot: false,
bannedJIDs: [], bannedJIDs: [],
forbiddenWords: [] forbiddenWords: []
@ -31,24 +31,24 @@ async function getChannelModerationOptions (
const content = await fs.promises.readFile(filePath, { const content = await fs.promises.readFile(filePath, {
encoding: 'utf-8' encoding: 'utf-8'
}) })
const sanitized = await sanitizeChannelModerationOptions(options, channelInfos, JSON.parse(content)) const sanitized = await sanitizeChannelConfigurationOptions(options, channelInfos, JSON.parse(content))
return { return {
channel: channelInfos, channel: channelInfos,
moderation: sanitized configuration: sanitized
} }
} }
/** /**
* Save channel moderation options. * Save channel configuration options.
* Can throw an exception. * Can throw an exception.
* @param _options Peertube server options * @param options Peertube server options
* @param _channelModeration data to save * @param channelConfiguration data to save
*/ */
async function storeChannelModerationOptions ( async function storeChannelConfigurationOptions (
options: RegisterServerOptions, options: RegisterServerOptions,
channelModeration: ChannelModeration channelConfiguration: ChannelConfiguration
): Promise<void> { ): Promise<void> {
const channelInfos = channelModeration.channel const channelInfos = channelConfiguration.channel
const filePath = _getFilePath(options, channelInfos) const filePath = _getFilePath(options, channelInfos)
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
@ -58,7 +58,7 @@ async function storeChannelModerationOptions (
} }
} }
const jsonContent = JSON.stringify(channelModeration.moderation) const jsonContent = JSON.stringify(channelConfiguration.configuration)
await fs.promises.writeFile(filePath, jsonContent, { await fs.promises.writeFile(filePath, jsonContent, {
encoding: 'utf-8' encoding: 'utf-8'
@ -77,12 +77,12 @@ function _getFilePath (
return path.resolve( return path.resolve(
options.peertubeHelpers.plugin.getDataDirectoryPath(), options.peertubeHelpers.plugin.getDataDirectoryPath(),
'channelModerationOptions', 'channelConfigurationOptions',
channelId.toString() + '.json' channelId.toString() + '.json'
) )
} }
export { export {
getChannelModerationOptions, getChannelConfigurationOptions,
storeChannelModerationOptions storeChannelConfigurationOptions
} }

View File

@ -6,11 +6,11 @@ import { isUserAdminOrModerator } from '../../helpers'
/** /**
* Returns a middleware handler to get the channelInfos from the channel parameter. * Returns a middleware handler to get the channelInfos from the channel parameter.
* This is used in api related to channel moderation options. * This is used in api related to channel configuration options.
* @param options Peertube server options * @param options Peertube server options
* @returns middleware function * @returns middleware function
*/ */
function getCheckModerationChannelMiddleware (options: RegisterServerOptions): RequestPromiseHandler { function getCheckConfigurationChannelMiddleware (options: RegisterServerOptions): RequestPromiseHandler {
return async (req: Request, res: Response, next: NextFunction) => { return async (req: Request, res: Response, next: NextFunction) => {
const logger = options.peertubeHelpers.logger const logger = options.peertubeHelpers.logger
const channelId = req.params.channelId const channelId = req.params.channelId
@ -42,12 +42,12 @@ function getCheckModerationChannelMiddleware (options: RegisterServerOptions): R
return return
} }
logger.debug('User can access the moderation channel api.') logger.debug('User can access the configuration channel api.')
res.locals.channelInfos = channelInfos res.locals.channelInfos = channelInfos
next() next()
} }
} }
export { export {
getCheckModerationChannelMiddleware getCheckConfigurationChannelMiddleware
} }

View File

@ -7,7 +7,7 @@ import { isDebugMode } from '../debug'
import { initRoomApiRouter } from './api/room' import { initRoomApiRouter } from './api/room'
import { initAuthApiRouter, initUserAuthApiRouter } from './api/auth' import { initAuthApiRouter, initUserAuthApiRouter } from './api/auth'
import { initFederationServerInfosApiRouter } from './api/federation-server-infos' import { initFederationServerInfosApiRouter } from './api/federation-server-infos'
import { initModerationApiRouter } from './api/moderation' import { initConfigurationApiRouter } from './api/configuration'
/** /**
* Initiate API routes * Initiate API routes
@ -53,7 +53,7 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
)) ))
} }
router.use('/moderation', await initModerationApiRouter(options)) router.use('/configuration', await initConfigurationApiRouter(options))
return router return router
} }

View File

@ -2,16 +2,16 @@ import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { Router, Request, Response, NextFunction } from 'express' import type { Router, Request, Response, NextFunction } from 'express'
import type { ChannelInfos } from '../../../../shared/lib/types' import type { ChannelInfos } from '../../../../shared/lib/types'
import { asyncMiddleware } from '../../middlewares/async' import { asyncMiddleware } from '../../middlewares/async'
import { getCheckModerationChannelMiddleware } from '../../middlewares/moderation/channel' import { getCheckConfigurationChannelMiddleware } from '../../middlewares/configuration/channel'
import { getChannelModerationOptions, storeChannelModerationOptions } from '../../moderation/channel/storage' import { getChannelConfigurationOptions, storeChannelConfigurationOptions } from '../../configuration/channel/storage'
import { sanitizeChannelModerationOptions } from '../../moderation/channel/sanitize' import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
async function initModerationApiRouter (options: RegisterServerOptions): Promise<Router> { async function initConfigurationApiRouter (options: RegisterServerOptions): Promise<Router> {
const router = options.getRouter() const router = options.getRouter()
const logger = options.peertubeHelpers.logger const logger = options.peertubeHelpers.logger
router.get('/channel/:channelId', asyncMiddleware([ router.get('/channel/:channelId', asyncMiddleware([
getCheckModerationChannelMiddleware(options), getCheckConfigurationChannelMiddleware(options),
async (req: Request, res: Response, _next: NextFunction): Promise<void> => { async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
if (!res.locals.channelInfos) { if (!res.locals.channelInfos) {
logger.error('Missing channelInfos in res.locals, should not happen') logger.error('Missing channelInfos in res.locals, should not happen')
@ -20,14 +20,14 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
} }
const channelInfos = res.locals.channelInfos as ChannelInfos const channelInfos = res.locals.channelInfos as ChannelInfos
const result = await getChannelModerationOptions(options, channelInfos) const result = await getChannelConfigurationOptions(options, channelInfos)
res.status(200) res.status(200)
res.json(result) res.json(result)
} }
])) ]))
router.post('/channel/:channelId', asyncMiddleware([ router.post('/channel/:channelId', asyncMiddleware([
getCheckModerationChannelMiddleware(options), getCheckConfigurationChannelMiddleware(options),
async (req: Request, res: Response, _next: NextFunction): Promise<void> => { async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
if (!res.locals.channelInfos) { if (!res.locals.channelInfos) {
logger.error('Missing channelInfos in res.locals, should not happen') logger.error('Missing channelInfos in res.locals, should not happen')
@ -35,11 +35,11 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
return return
} }
const channelInfos = res.locals.channelInfos as ChannelInfos const channelInfos = res.locals.channelInfos as ChannelInfos
logger.debug('Trying to save ChannelModerationOptions') logger.debug('Trying to save ChannelConfigurationOptions')
let moderation let configuration
try { try {
moderation = await sanitizeChannelModerationOptions(options, channelInfos, req.body) configuration = await sanitizeChannelConfigurationOptions(options, channelInfos, req.body)
} catch (err) { } catch (err) {
logger.warn(err) logger.warn(err)
res.sendStatus(400) res.sendStatus(400)
@ -49,9 +49,9 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
logger.debug('Data seems ok, storing them.') logger.debug('Data seems ok, storing them.')
const result = { const result = {
channel: channelInfos, channel: channelInfos,
moderation configuration
} }
await storeChannelModerationOptions(options, result) await storeChannelConfigurationOptions(options, result)
res.status(200) res.status(200)
res.json(result) res.json(result)
} }
@ -61,5 +61,5 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
} }
export { export {
initModerationApiRouter initConfigurationApiRouter
} }

View File

@ -52,15 +52,15 @@ interface ChannelInfos {
displayName: string displayName: string
} }
interface ChannelModerationOptions { interface ChannelConfigurationOptions {
bot: boolean bot: boolean
forbiddenWords: string[] forbiddenWords: string[]
bannedJIDs: string[] bannedJIDs: string[]
} }
interface ChannelModeration { interface ChannelConfiguration {
channel: ChannelInfos channel: ChannelInfos
moderation: ChannelModerationOptions configuration: ChannelConfigurationOptions
} }
export type { export type {
@ -69,6 +69,6 @@ export type {
ProsodyListRoomsResult, ProsodyListRoomsResult,
ProsodyListRoomsResultRoom, ProsodyListRoomsResultRoom,
ChannelInfos, ChannelInfos,
ChannelModerationOptions, ChannelConfigurationOptions,
ChannelModeration ChannelConfiguration
} }