peertube-plugin-livechat/client/common/configuration/templates/channel.ts

324 lines
14 KiB
TypeScript
Raw Normal View History

2023-08-08 16:26:40 +00:00
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { ChannelConfiguration } from 'shared/lib/types'
2023-08-08 16:26:40 +00:00
import { getBaseRoute } from '../../../videowatch/uri'
2023-09-20 16:48:02 +00:00
import { localizedHelpUrl } from '../../../utils/help'
import { helpButtonSVG } from '../../../videowatch/buttons'
2023-08-08 16:26:40 +00:00
// Must use require for mustache, import seems buggy.
const Mustache = require('mustache')
/**
* Renders the configuration settings page for a given channel.
2023-08-08 16:26:40 +00:00
* @param registerClientOptions Peertube client options
* @param channelId The channel id
* @returns The page content
*/
async function renderConfigurationChannel (
2023-08-08 16:26:40 +00:00
registerClientOptions: RegisterClientOptions,
channelId: string
2023-08-09 10:20:19 +00:00
): Promise<string | false> {
2023-08-08 16:26:40 +00:00
const { peertubeHelpers } = registerClientOptions
try {
if (!channelId || !/^\d+$/.test(channelId)) {
throw new Error('Missing or invalid channel id.')
}
2023-08-09 14:16:02 +00:00
const response = await fetch(
getBaseRoute(registerClientOptions) + '/api/configuration/channel/' + encodeURIComponent(channelId),
2023-08-08 16:26:40 +00:00
{
method: 'GET',
headers: peertubeHelpers.getAuthHeader()
}
2023-08-09 14:16:02 +00:00
)
if (!response.ok) {
throw new Error('Can\'t get channel configuration options.')
2023-08-08 16:26:40 +00:00
}
const channelConfiguration: ChannelConfiguration = await (response).json()
2023-08-09 14:16:02 +00:00
// Basic testing that channelConfiguration has the correct format
if ((typeof channelConfiguration !== 'object') || !channelConfiguration.channel) {
throw new Error('Invalid channel configuration options.')
2023-08-09 14:16:02 +00:00
}
2023-08-08 16:26:40 +00:00
2023-09-20 16:48:02 +00:00
const helpUrl = await localizedHelpUrl(registerClientOptions, {
page: 'documentation/user/streamers/' // FIXME: this is not the good link
})
const helpIcon = helpButtonSVG()
const helpButton = `<a
href="${helpUrl}"
target=_blank
class="orange-button peertube-button-link"
>${helpIcon}</a>`
2023-09-21 14:09:50 +00:00
const helpButtonForbiddenWords = helpButton
const helpButtonQuotes = helpButton
const helpButtonCommands = helpButton
2023-09-20 16:48:02 +00:00
2023-08-08 16:26:40 +00:00
const view = {
title: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_TITLE),
description: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_DESC),
enableBot: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_ENABLE_BOT_LABEL),
botOptions: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_BOT_OPTIONS_TITLE),
forbiddenWords: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL),
2023-09-20 16:48:02 +00:00
forbiddenWordsDesc: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_DESC),
forbiddenWordsDesc2: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_DESC2),
forbiddenWordsReason: await peertubeHelpers.translate(
LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REASON_LABEL
),
forbiddenWordsReasonDesc: await peertubeHelpers.translate(
LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REASON_DESC
),
forbiddenWordsRegexp: await peertubeHelpers.translate(
LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REGEXP_LABEL
),
forbiddenWordsRegexpDesc: await peertubeHelpers.translate(
LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REGEXP_DESC
),
2023-09-21 14:09:50 +00:00
forbiddenWordsApplyToModerators: await peertubeHelpers.translate(
LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_APPLYTOMODERATORS_LABEL
),
forbiddenWordsApplyToModeratorsDesc: await peertubeHelpers.translate(
LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_APPLYTOMODERATORS_DESC
),
2023-09-20 16:48:02 +00:00
quoteLabel: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_LABEL),
quoteDesc: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_DESC),
2023-09-21 14:09:50 +00:00
quoteDesc2: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_DESC2),
2023-09-20 16:48:02 +00:00
quoteDelayLabel: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_DELAY_LABEL),
quoteDelayDesc: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_DELAY_DESC),
2023-09-21 14:09:50 +00:00
commandLabel: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_LABEL),
commandDesc: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_DESC),
commandCmdLabel: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_CMD_LABEL),
commandCmdDesc: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_CMD_DESC),
commandMessageLabel: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_MESSAGE_LABEL),
commandMessageDesc: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_MESSAGE_DESC),
// bannedJIDs: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_BANNED_JIDS_LABEL),
2023-08-09 10:20:19 +00:00
save: await peertubeHelpers.translate(LOC_SAVE),
cancel: await peertubeHelpers.translate(LOC_CANCEL),
2023-09-19 16:56:39 +00:00
botNickname: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_BOT_NICKNAME),
2023-09-21 14:09:50 +00:00
moreInfo: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FOR_MORE_INFO),
2023-09-20 16:48:02 +00:00
forbiddenWordsArray: [0, 1, 2].map(count => {
return {
displayNumber: count + 1,
fieldNumber: count,
displayHelp: count === 0
}
}),
2023-09-21 14:09:50 +00:00
quotesArray: [0].map(count => {
return {
displayNumber: count + 1,
fieldNumber: count,
displayHelp: count === 0
}
}),
cmdsArray: [0, 1, 2].map(count => {
2023-09-20 16:48:02 +00:00
return {
displayNumber: count + 1,
fieldNumber: count,
displayHelp: count === 0
}
}),
helpButton,
2023-09-21 14:09:50 +00:00
helpButtonForbiddenWords,
helpButtonCommands,
helpButtonQuotes,
channelConfiguration
2023-08-08 16:26:40 +00:00
}
return Mustache.render(`
<div class="margin-content peertube-plugin-livechat-configuration peertube-plugin-livechat-configuration-channel">
<h1>
{{title}}:
<span class="peertube-plugin-livechat-configuration-channel-info">
<span>{{channelConfiguration.channel.displayName}}</span>
<span>{{channelConfiguration.channel.name}}</span>
</span>
2023-09-21 14:09:50 +00:00
{{{helpButton}}}
</h1>
2023-08-08 16:26:40 +00:00
<p>{{description}}</p>
<form livechat-configuration-channel-options role="form">
<div class="row mt-3">
<div class="col-12 col-lg-4 col-xl-3">
<h2>{{botOptions}}</h2>
</div>
<div class="col-12 col-lg-8 col-xl-9">
2023-09-20 16:48:02 +00:00
<div class="form-group">
<label>
<input
type="checkbox"
name="bot"
id="peertube-livechat-bot"
value="1"
{{#channelConfiguration.configuration.bot}}
checked="checked"
{{/channelConfiguration.configuration.bot}}
/>
{{enableBot}}
</label>
</div>
<div class="form-group" livechat-configuration-channel-options-bot-enabled>
<label for="peertube-livechat-bot-nickname">{{botNickname}}</label>
<input
type="text"
name="bot_nickname"
class="form-control"
id="peertube-livechat-bot-nickname"
value="{{channelConfiguration.configuration.botNickname}}"
/>
</div>
</div>
</div>
{{#forbiddenWordsArray}}{{! iterating on forbiddenWordsArray to display N fields }}
<div class="row mt-5" livechat-configuration-channel-options-bot-enabled>
<div class="col-12 col-lg-4 col-xl-3">
<h2>{{forbiddenWords}} #{{displayNumber}}</h2>
{{#displayHelp}}
2023-09-21 14:09:50 +00:00
<p>{{forbiddenWordsDesc}} {{moreInfo}}</p>
{{{HelpButtonForbiddenWords}}}
2023-09-20 16:48:02 +00:00
{{/displayHelp}}
</div>
<div class="col-12 col-lg-8 col-xl-9">
<div class="form-group">
<label for="peertube-livechat-forbidden-words-{{fieldNumber}}">{{forbiddenWords}}</label>
{{! warning: don't add extra line break in textarea! }}
<textarea
name="forbidden_words_{{fieldNumber}}"
id="peertube-livechat-forbidden-words-{{fieldNumber}}"
class="form-control"
>{{
#channelConfiguration.configuration.forbiddenWords
}}{{.}}\n{{
/channelConfiguration.configuration.forbiddenWords
}}</textarea>
<p class="form-group-description">{{forbiddenWordsDesc2}}</p>
</div>
<div class="form-group">
<label>
<input
type="checkbox"
2023-09-20 16:48:02 +00:00
name="forbidden_words_regexp_{{fieldNumber}}"
value="1"
/>
2023-09-20 16:48:02 +00:00
{{forbiddenWordsRegexp}}
</label>
2023-09-20 16:48:02 +00:00
<p class="form-group-description">{{forbiddenWordsRegexpDesc}}</p>
</div>
2023-09-21 14:09:50 +00:00
<div class="form-group">
<label>
<input
type="checkbox"
name="forbidden_words_applytomoderators_{{fieldNumber}}"
value="1"
/>
{{forbiddenWordsApplyToModerators}}
</label>
<p class="form-group-description">{{forbiddenWordsApplyToModeratorsDesc}}</p>
</div>
<div class="form-group">
2023-09-20 16:48:02 +00:00
<label for="peertube-livechat-forbidden-words-reason-{{fieldNumber}}">{{forbiddenWordsReason}}</label>
<input
type="text"
2023-09-20 16:48:02 +00:00
name="forbidden_words_reason_{{fieldNumber}}"
class="form-control"
2023-09-20 16:48:02 +00:00
id="peertube-livechat-forbidden-words-reason-{{fieldNumber}}"
value=""
/>
2023-09-20 16:48:02 +00:00
<p class="form-group-description">{{forbiddenWordsReasonDesc}}</p>
</div>
2023-09-20 16:48:02 +00:00
</div>
</div>
{{/forbiddenWordsArray}}
{{#quotesArray}}{{! iterating on quotesArray to display N fields }}
<div class="row mt-5" livechat-configuration-channel-options-bot-enabled>
<div class="col-12 col-lg-4 col-xl-3">
<h2>{{quoteLabel}} #{{displayNumber}}</h2>
{{#displayHelp}}
2023-09-21 14:09:50 +00:00
<p>{{quoteDesc}} {{moreInfo}}</p>
{{{helpButtonQuotes}}}
2023-09-20 16:48:02 +00:00
{{/displayHelp}}
</div>
<div class="col-12 col-lg-8 col-xl-9">
<div class="form-group">
2023-09-20 16:48:02 +00:00
<label for="peertube-livechat-quote-{{fieldNumber}}">{{quoteLabel}}</label>
2023-09-21 14:09:50 +00:00
{{! warning: don't add extra line break in textarea! }}
<textarea
2023-09-20 16:48:02 +00:00
name="quote_{{fieldNumber}}"
id="peertube-livechat-quote-{{fieldNumber}}"
2023-09-21 14:09:50 +00:00
class="form-control"
>{{
#channelConfiguration.configuration.TODO
}}{{.}}\n{{
/channelConfiguration.configuration.TODO
}}</textarea>
<p class="form-group-description">{{quoteDesc2}}</p>
</div>
2023-09-20 16:48:02 +00:00
<div class="form-group">
<label for="peertube-livechat-quote-delay-{{fieldNumber}}">{{quoteDelayLabel}}</label>
<input
type="number"
2023-09-21 14:09:50 +00:00
min="1"
2023-09-20 16:48:02 +00:00
max="6000"
step="1"
name="quote_delay_{{fieldNumber}}"
class="form-control"
id="peertube-livechat-quote-delay-{{fieldNumber}}"
2023-09-21 14:09:50 +00:00
value="5"
2023-09-20 16:48:02 +00:00
/>
<p class="form-group-description">{{quoteDelayDesc}}</p>
</div>
2023-09-21 14:09:50 +00:00
</div>
</div>
{{/quotesArray}}
{{#cmdsArray}}{{! iterating on cmdsArray to display N fields }}
<div class="row mt-5" livechat-configuration-channel-options-bot-enabled>
<div class="col-12 col-lg-4 col-xl-3">
<h2>{{commandLabel}} #{{displayNumber}}</h2>
{{#displayHelp}}
<p>{{commandDesc}} {{moreInfo}}</p>
{{{helpButtonCommands}}}
{{/displayHelp}}
</div>
<div class="col-12 col-lg-8 col-xl-9">
<div class="form-group">
<label for="peertube-livechat-command-{{fieldNumber}}">{{commandCmdLabel}}</label>
<input
type="text"
name="command_{{fieldNumber}}"
class="form-control"
id="peertube-livechat-command-{{fieldNumber}}"
value=""
/>
<p class="form-group-description">{{commandCmdDesc}}</p>
</div>
2023-09-20 16:48:02 +00:00
<div class="form-group">
2023-09-21 14:09:50 +00:00
<label for="peertube-livechat-command-message-{{fieldNumber}}">{{commandMessageLabel}}</label>
2023-09-20 16:48:02 +00:00
<input
type="text"
2023-09-21 14:09:50 +00:00
name="command_message_{{fieldNumber}}"
2023-09-20 16:48:02 +00:00
class="form-control"
2023-09-21 14:09:50 +00:00
id="peertube-livechat-command-message-{{fieldNumber}}"
2023-09-20 16:48:02 +00:00
value=""
/>
2023-09-21 14:09:50 +00:00
<p class="form-group-description">{{commandMessageDesc}}</p>
2023-09-20 16:48:02 +00:00
</div>
</div>
</div>
2023-09-21 14:09:50 +00:00
{{/cmdsArray}}
<div class="form-group">
<input type="submit" value="{{save}}" />
<input type="reset" value="{{cancel}}" />
</div>
2023-08-09 10:20:19 +00:00
</form>
2023-08-08 16:26:40 +00:00
</div>
`, view) as string
} catch (err: any) {
peertubeHelpers.notifier.error(err.toString())
return ''
}
}
export {
renderConfigurationChannel
2023-08-08 16:26:40 +00:00
}