2023-08-08 16:26:40 +00:00
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
2023-09-06 13:23:39 +00:00
|
|
|
import type { ChannelConfiguration } from 'shared/lib/types'
|
2023-08-08 16:26:40 +00:00
|
|
|
import { getBaseRoute } from '../../../videowatch/uri'
|
|
|
|
// Must use require for mustache, import seems buggy.
|
|
|
|
const Mustache = require('mustache')
|
|
|
|
|
|
|
|
/**
|
2023-09-06 13:23:39 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2023-09-06 13:23:39 +00:00
|
|
|
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(
|
2023-09-06 13:23:39 +00:00
|
|
|
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) {
|
2023-09-06 13:23:39 +00:00
|
|
|
throw new Error('Can\'t get channel configuration options.')
|
2023-08-08 16:26:40 +00:00
|
|
|
}
|
2023-09-06 13:23:39 +00:00
|
|
|
const channelConfiguration: ChannelConfiguration = await (response).json()
|
2023-08-09 14:16:02 +00:00
|
|
|
|
2023-09-06 13:23:39 +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
|
|
|
|
|
|
|
const view = {
|
2023-09-06 13:23:39 +00:00
|
|
|
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),
|
|
|
|
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-06 13:23:39 +00:00
|
|
|
channelConfiguration
|
2023-08-08 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Mustache.render(`
|
2023-09-20 12:59:01 +00:00
|
|
|
<div class="margin-content peertube-plugin-livechat-configuration">
|
2023-09-06 13:23:39 +00:00
|
|
|
<h1>{{title}} {{channelConfiguration.channel.displayName}}</h1>
|
2023-08-08 16:26:40 +00:00
|
|
|
<p>{{description}}</p>
|
2023-09-06 13:23:39 +00:00
|
|
|
<form livechat-configuration-channel-options>
|
2023-08-09 10:20:19 +00:00
|
|
|
<fieldset>
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="checkbox" name="bot"
|
|
|
|
value="1"
|
2023-09-06 13:23:39 +00:00
|
|
|
{{#channelConfiguration.configuration.bot}}
|
|
|
|
checked="checked"
|
|
|
|
{{/channelConfiguration.configuration.bot}}
|
2023-08-09 10:20:19 +00:00
|
|
|
/>
|
|
|
|
{{enableBot}}
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
2023-09-06 13:23:39 +00:00
|
|
|
<fieldset livechat-configuration-channel-options-bot-enabled>
|
2023-08-09 10:20:19 +00:00
|
|
|
<legend>{{botOptions}}</legend>
|
2023-09-19 16:56:39 +00:00
|
|
|
<label>
|
|
|
|
{{botNickname}}
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
name="bot_nickname"
|
|
|
|
value="{{channelConfiguration.configuration.botNickname}}"
|
|
|
|
/>
|
|
|
|
</label>
|
2023-08-09 10:20:19 +00:00
|
|
|
<label>
|
|
|
|
{{forbiddenWords}}
|
|
|
|
<textarea name="forbidden_words">
|
2023-09-06 13:23:39 +00:00
|
|
|
{{#channelConfiguration.configuration.forbiddenWords}}{{.}}
|
|
|
|
{{/channelConfiguration.configuration.forbiddenWords}}
|
2023-08-09 10:20:19 +00:00
|
|
|
</textarea>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
{{bannedJIDs}}
|
|
|
|
<textarea name="banned_jids">
|
2023-09-06 13:23:39 +00:00
|
|
|
{{#channelConfiguration.configuration.bannedJIDs}}{{.}}
|
|
|
|
{{/channelConfiguration.configuration.bannedJIDs}}
|
2023-08-09 10:20:19 +00:00
|
|
|
</textarea>
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
<input type="submit" value="{{save}}" />
|
|
|
|
<input type="reset" value="{{cancel}}" />
|
|
|
|
</form>
|
2023-08-08 16:26:40 +00:00
|
|
|
</div>
|
|
|
|
`, view) as string
|
|
|
|
} catch (err: any) {
|
|
|
|
peertubeHelpers.notifier.error(err.toString())
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2023-09-06 13:23:39 +00:00
|
|
|
renderConfigurationChannel
|
2023-08-08 16:26:40 +00:00
|
|
|
}
|