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

99 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-08-08 16:26:40 +00:00
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
2023-08-09 14:16:02 +00:00
import type { ChannelModeration } 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')
/**
* Renders the moderation settings page for a given channel.
* @param registerClientOptions Peertube client options
* @param channelId The channel id
* @returns The page content
*/
async function renderModerationChannel (
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-08-08 16:26:40 +00:00
getBaseRoute(registerClientOptions) + '/api/moderation/channel/' + encodeURIComponent(channelId),
{
method: 'GET',
headers: peertubeHelpers.getAuthHeader()
}
2023-08-09 14:16:02 +00:00
)
if (!response.ok) {
2023-08-08 16:26:40 +00:00
throw new Error('Can\'t get channel moderation options.')
}
2023-08-09 14:16:02 +00:00
const channelModeration: ChannelModeration = await (response).json()
// Basic testing that channelModeration has the correct format
if ((typeof channelModeration !== 'object') || !channelModeration.channel) {
throw new Error('Invalid channel moderation options.')
}
2023-08-08 16:26:40 +00:00
const view = {
2023-08-09 10:20:19 +00:00
title: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_TITLE),
description: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_DESC),
enableBot: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_ENABLE_BOT_LABEL),
botOptions: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_BOT_OPTIONS_TITLE),
forbiddenWords: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_FORBIDDEN_WORDS_LABEL),
bannedJIDs: await peertubeHelpers.translate(LOC_LIVECHAT_MODERATION_CHANNEL_BANNED_JIDS_LABEL),
save: await peertubeHelpers.translate(LOC_SAVE),
cancel: await peertubeHelpers.translate(LOC_CANCEL),
2023-08-09 14:16:02 +00:00
channelModeration
2023-08-08 16:26:40 +00:00
}
return Mustache.render(`
<div class="margin-content">
2023-08-09 14:43:01 +00:00
<h1>{{title}} {{channelModeration.channel.displayName}}</h1>
2023-08-08 16:26:40 +00:00
<p>{{description}}</p>
2023-08-09 10:20:19 +00:00
<form livechat-moderation-channel-options>
<fieldset>
<label>
<input
type="checkbox" name="bot"
value="1"
2023-08-09 14:16:02 +00:00
{{#channelModeration.moderation.bot}} checked="checked" {{/channelModeration.moderation.bot}}
2023-08-09 10:20:19 +00:00
/>
{{enableBot}}
</label>
</fieldset>
<fieldset livechat-moderation-channel-options-bot-enabled>
<legend>{{botOptions}}</legend>
<label>
{{forbiddenWords}}
<textarea name="forbidden_words">
2023-08-09 14:16:02 +00:00
{{#channelModeration.moderation.forbiddenWords}}{{.}}
{{/channelModeration.moderation.forbiddenWords}}
2023-08-09 10:20:19 +00:00
</textarea>
</label>
<label>
{{bannedJIDs}}
<textarea name="banned_jids">
2023-08-09 14:16:02 +00:00
{{#channelModeration.moderation.bannedJIDs}}{{.}}
{{/channelModeration.moderation.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 {
renderModerationChannel
}