2024-05-23 15:17:28 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 Mehdi Benadel <https://mehdibenadel.com>
|
2024-06-05 16:27:57 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
2024-05-23 15:17:28 +00:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-05-23 14:56:11 +00:00
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
2024-05-28 15:56:24 +00:00
|
|
|
import type {
|
2024-06-20 09:14:00 +00:00
|
|
|
ChannelLiveChatInfos, ChannelConfiguration, ChannelConfigurationOptions, ChannelEmojisConfiguration, ChannelEmojis,
|
|
|
|
CustomEmojiDefinition
|
2024-05-28 15:56:24 +00:00
|
|
|
} from 'shared/lib/types'
|
2024-06-05 16:27:57 +00:00
|
|
|
import { ValidationError, ValidationErrorType } from '../../lib/models/validation'
|
2024-05-23 20:52:39 +00:00
|
|
|
import { getBaseRoute } from '../../../utils/uri'
|
2024-06-20 09:14:00 +00:00
|
|
|
import { maxEmojisPerChannel } from 'shared/lib/emojis'
|
2024-06-21 16:18:11 +00:00
|
|
|
import { channelTermsMaxLength } from 'shared/lib/constants'
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-23 12:41:11 +00:00
|
|
|
export class ChannelDetailsService {
|
2024-05-23 00:26:38 +00:00
|
|
|
public _registerClientOptions: RegisterClientOptions
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _headers: any = {}
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
constructor (registerClientOptions: RegisterClientOptions) {
|
2024-05-23 00:26:38 +00:00
|
|
|
this._registerClientOptions = registerClientOptions
|
|
|
|
|
|
|
|
this._headers = this._registerClientOptions.peertubeHelpers.getAuthHeader() ?? {}
|
|
|
|
this._headers['content-type'] = 'application/json;charset=UTF-8'
|
|
|
|
}
|
|
|
|
|
2024-06-05 16:27:57 +00:00
|
|
|
validateOptions = async (channelConfigurationOptions: ChannelConfigurationOptions): Promise<boolean> => {
|
|
|
|
const propertiesError: ValidationError['properties'] = {}
|
|
|
|
|
2024-06-21 16:18:11 +00:00
|
|
|
if (channelConfigurationOptions.terms && channelConfigurationOptions.terms.length > channelTermsMaxLength) {
|
|
|
|
propertiesError.terms = [ValidationErrorType.TooLong]
|
|
|
|
}
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
const botConf = channelConfigurationOptions.bot
|
|
|
|
const slowModeDuration = channelConfigurationOptions.slowMode.duration
|
2024-07-09 14:15:07 +00:00
|
|
|
const moderationDelay = channelConfigurationOptions.moderation.delay
|
2024-05-26 03:06:28 +00:00
|
|
|
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError['slowMode.duration'] = []
|
2024-07-09 14:15:07 +00:00
|
|
|
propertiesError['moderation.delay'] = []
|
2024-05-26 03:06:28 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
(typeof slowModeDuration !== 'number') ||
|
2024-06-05 16:27:57 +00:00
|
|
|
isNaN(slowModeDuration)
|
|
|
|
) {
|
|
|
|
propertiesError['slowMode.duration'].push(ValidationErrorType.WrongType)
|
2024-05-26 03:06:28 +00:00
|
|
|
} else if (
|
|
|
|
slowModeDuration < 0 ||
|
|
|
|
slowModeDuration > 1000
|
|
|
|
) {
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError['slowMode.duration'].push(ValidationErrorType.NotInRange)
|
2024-05-26 03:06:28 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 14:15:07 +00:00
|
|
|
if (
|
|
|
|
(typeof moderationDelay !== 'number') ||
|
|
|
|
isNaN(moderationDelay)
|
|
|
|
) {
|
|
|
|
propertiesError['moderation.delay'].push(ValidationErrorType.WrongType)
|
|
|
|
} else if (
|
|
|
|
moderationDelay < 0 ||
|
|
|
|
moderationDelay > 60
|
|
|
|
) {
|
|
|
|
propertiesError['moderation.delay'].push(ValidationErrorType.NotInRange)
|
|
|
|
}
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
// If !bot.enabled, we don't have to validate these fields:
|
|
|
|
// The backend will ignore those values.
|
|
|
|
if (botConf.enabled) {
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError['bot.nickname'] = []
|
2024-05-26 03:06:28 +00:00
|
|
|
|
|
|
|
if (/[^\p{L}\p{N}\p{Z}_-]/u.test(botConf.nickname ?? '')) {
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError['bot.nickname'].push(ValidationErrorType.WrongFormat)
|
2024-05-26 03:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const [i, fw] of botConf.forbiddenWords.entries()) {
|
|
|
|
for (const v of fw.entries) {
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError[`bot.forbiddenWords.${i}.entries`] = []
|
2024-05-26 03:06:28 +00:00
|
|
|
if (fw.regexp) {
|
|
|
|
if (v.trim() !== '') {
|
|
|
|
try {
|
2024-05-28 10:45:35 +00:00
|
|
|
// eslint-disable-next-line no-new
|
|
|
|
new RegExp(v)
|
2024-05-26 03:06:28 +00:00
|
|
|
} catch (_) {
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError[`bot.forbiddenWords.${i}.entries`]
|
2024-05-26 03:06:28 +00:00
|
|
|
.push(ValidationErrorType.WrongFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [i, cd] of botConf.commands.entries()) {
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError[`bot.commands.${i}.command`] = []
|
2024-05-26 03:06:28 +00:00
|
|
|
|
|
|
|
if (/\s+/.test(cd.command)) {
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError[`bot.commands.${i}.command`].push(ValidationErrorType.WrongFormat)
|
2024-05-26 03:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 16:27:57 +00:00
|
|
|
if (Object.values(propertiesError).find(e => e.length > 0)) {
|
|
|
|
const validationError = new ValidationError(
|
|
|
|
'ChannelConfigurationOptionsValidationError',
|
|
|
|
await this._registerClientOptions.peertubeHelpers.translate(LOC_VALIDATION_ERROR),
|
|
|
|
propertiesError
|
|
|
|
)
|
2024-05-26 03:06:28 +00:00
|
|
|
throw validationError
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
saveOptions = async (channelId: number,
|
|
|
|
channelConfigurationOptions: ChannelConfigurationOptions): Promise<Response> => {
|
2024-05-23 00:26:38 +00:00
|
|
|
if (!await this.validateOptions(channelConfigurationOptions)) {
|
|
|
|
throw new Error('Invalid form data')
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
getBaseRoute(this._registerClientOptions) + '/api/configuration/channel/' + encodeURIComponent(channelId),
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: this._headers,
|
|
|
|
body: JSON.stringify(channelConfigurationOptions)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to save configuration options.')
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
return response.json()
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 12:41:11 +00:00
|
|
|
fetchUserChannels = async (username: string): Promise<ChannelLiveChatInfos[]> => {
|
|
|
|
// FIXME: if more than 100 channels, loop (or add a pagination)
|
|
|
|
const channels = await (await fetch(
|
|
|
|
'/api/v1/accounts/' + encodeURIComponent(username) + '/video-channels?start=0&count=100&sort=name',
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
headers: this._headers
|
|
|
|
}
|
|
|
|
)).json()
|
|
|
|
if (!channels || !('data' in channels) || !Array.isArray(channels.data)) {
|
|
|
|
throw new Error('Can\'t get the channel list.')
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const channel of channels.data) {
|
|
|
|
channel.livechatConfigurationUri = '/p/livechat/configuration/channel?channelId=' + encodeURIComponent(channel.id)
|
|
|
|
|
|
|
|
// Note: since Peertube v6.0.0, channel.avatar is dropped, and we have to use channel.avatars.
|
|
|
|
// So, if !channel.avatar, we will search a suitable one in channel.avatars, and fill channel.avatar.
|
|
|
|
if (!channel.avatar && channel.avatars && Array.isArray(channel.avatars)) {
|
|
|
|
for (const avatar of channel.avatars) {
|
|
|
|
if (avatar.width === 120) {
|
|
|
|
channel.avatar = avatar
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return channels.data
|
|
|
|
}
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
fetchConfiguration = async (channelId: number): Promise<ChannelConfiguration> => {
|
|
|
|
const response = await fetch(
|
|
|
|
getBaseRoute(this._registerClientOptions) + '/api/configuration/channel/' + encodeURIComponent(channelId),
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
headers: this._headers
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Can\'t get channel configuration options.')
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
return response.json()
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
2024-05-28 15:56:24 +00:00
|
|
|
|
2024-06-05 16:27:57 +00:00
|
|
|
public async fetchEmojisConfiguration (channelId: number): Promise<ChannelEmojisConfiguration> {
|
2024-05-28 15:56:24 +00:00
|
|
|
const response = await fetch(
|
|
|
|
getBaseRoute(this._registerClientOptions) +
|
|
|
|
'/api/configuration/channel/emojis/' +
|
|
|
|
encodeURIComponent(channelId),
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
headers: this._headers
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Can\'t get channel emojis options.')
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
}
|
2024-06-05 16:27:57 +00:00
|
|
|
|
|
|
|
public async validateEmojisConfiguration (channelEmojis: ChannelEmojis): Promise<boolean> {
|
|
|
|
const propertiesError: ValidationError['properties'] = {}
|
|
|
|
|
2024-06-20 09:38:04 +00:00
|
|
|
if (channelEmojis.customEmojis.length > maxEmojisPerChannel) {
|
|
|
|
// This can happen when using the import function.
|
|
|
|
const validationError = new ValidationError(
|
|
|
|
'ChannelEmojisValidationError',
|
|
|
|
await this._registerClientOptions.peertubeHelpers.translate(LOC_TOO_MANY_ENTRIES),
|
|
|
|
propertiesError
|
|
|
|
)
|
|
|
|
throw validationError
|
|
|
|
}
|
|
|
|
|
2024-06-06 13:03:12 +00:00
|
|
|
const seen = new Map<string, true>()
|
2024-06-05 16:27:57 +00:00
|
|
|
for (const [i, e] of channelEmojis.customEmojis.entries()) {
|
|
|
|
propertiesError[`emojis.${i}.sn`] = []
|
2024-06-06 09:36:07 +00:00
|
|
|
if (e.sn === '') {
|
|
|
|
propertiesError[`emojis.${i}.sn`].push(ValidationErrorType.Missing)
|
2024-06-25 15:12:46 +00:00
|
|
|
} else if (!/^:?[\w-]+:?$/.test(e.sn)) { // optional ':' at the beggining and at the end
|
2024-06-05 16:27:57 +00:00
|
|
|
propertiesError[`emojis.${i}.sn`].push(ValidationErrorType.WrongFormat)
|
2024-06-06 13:03:12 +00:00
|
|
|
} else if (seen.has(e.sn)) {
|
|
|
|
propertiesError[`emojis.${i}.sn`].push(ValidationErrorType.Duplicate)
|
|
|
|
} else {
|
|
|
|
seen.set(e.sn, true)
|
2024-06-05 16:27:57 +00:00
|
|
|
}
|
2024-06-06 09:36:07 +00:00
|
|
|
|
|
|
|
propertiesError[`emojis.${i}.url`] = []
|
|
|
|
if (!e.url) {
|
|
|
|
propertiesError[`emojis.${i}.url`].push(ValidationErrorType.Missing)
|
|
|
|
}
|
2024-06-05 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.values(propertiesError).find(e => e.length > 0)) {
|
|
|
|
const validationError = new ValidationError(
|
|
|
|
'ChannelEmojisValidationError',
|
|
|
|
await this._registerClientOptions.peertubeHelpers.translate(LOC_VALIDATION_ERROR),
|
|
|
|
propertiesError
|
|
|
|
)
|
|
|
|
throw validationError
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
public async saveEmojisConfiguration (
|
|
|
|
channelId: number,
|
|
|
|
channelEmojis: ChannelEmojis
|
2024-06-20 09:14:00 +00:00
|
|
|
): Promise<ChannelEmojisConfiguration> {
|
2024-06-05 16:27:57 +00:00
|
|
|
if (!await this.validateEmojisConfiguration(channelEmojis)) {
|
|
|
|
throw new Error('Invalid form data')
|
|
|
|
}
|
|
|
|
|
2024-06-20 09:14:00 +00:00
|
|
|
// Note: API request body size is limited to 100Kb (expressjs body-parser defaut limit, and Peertube nginx config).
|
|
|
|
// So we must send new emojis 1 by 1, to be sure to not reach the limit.
|
|
|
|
if (!channelEmojis.customEmojis.find(e => e.url.startsWith('data:'))) {
|
|
|
|
// No new emojis, just saving.
|
|
|
|
return this._saveEmojisConfiguration(channelId, channelEmojis)
|
|
|
|
}
|
|
|
|
|
|
|
|
let lastResult: ChannelEmojisConfiguration | undefined
|
|
|
|
let customEmojis: CustomEmojiDefinition[] = [...channelEmojis.customEmojis] // copy the original array
|
|
|
|
let i = customEmojis.findIndex(e => e.url.startsWith('data:'))
|
|
|
|
let watchDog = 0
|
|
|
|
while (i >= 0) {
|
|
|
|
watchDog++
|
2024-06-20 09:38:04 +00:00
|
|
|
if (watchDog > channelEmojis.customEmojis.length + 10) { // just to avoid infinite loop
|
2024-06-20 09:14:00 +00:00
|
|
|
throw new Error('Seems we have sent too many emojis, this was not expected')
|
|
|
|
}
|
|
|
|
const data: CustomEmojiDefinition[] = customEmojis.slice(0, i + 1) // all elements until first new file
|
|
|
|
data.push(
|
|
|
|
// all remaining elements that where already uploaded (to not loose them):
|
|
|
|
...customEmojis.slice(i + 1).filter((e) => !e.url.startsWith('data:'))
|
|
|
|
)
|
|
|
|
lastResult = await this._saveEmojisConfiguration(channelId, {
|
|
|
|
customEmojis: data
|
|
|
|
})
|
|
|
|
|
|
|
|
// Must inject the result in customEmojis
|
|
|
|
const temp = lastResult.emojis.customEmojis.slice(0, i + 1) // last element should have been replace by a http url
|
|
|
|
temp.push(
|
|
|
|
...customEmojis.slice(i + 1) // remaining elements in the previous array
|
|
|
|
)
|
|
|
|
customEmojis = temp
|
|
|
|
|
|
|
|
// and searching again next new emojis
|
|
|
|
i = customEmojis.findIndex(e => e.url.startsWith('data:'))
|
|
|
|
}
|
|
|
|
if (!lastResult) {
|
|
|
|
// This should not happen...
|
|
|
|
throw new Error('Unexpected: no last result')
|
|
|
|
}
|
|
|
|
return lastResult
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _saveEmojisConfiguration (
|
|
|
|
channelId: number,
|
|
|
|
channelEmojis: ChannelEmojis
|
|
|
|
): Promise<ChannelEmojisConfiguration> {
|
2024-06-05 16:27:57 +00:00
|
|
|
const response = await fetch(
|
|
|
|
getBaseRoute(this._registerClientOptions) +
|
|
|
|
'/api/configuration/channel/emojis/' +
|
|
|
|
encodeURIComponent(channelId),
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: this._headers,
|
|
|
|
body: JSON.stringify(channelEmojis)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Can\'t get channel emojis options.')
|
|
|
|
}
|
2024-06-20 09:14:00 +00:00
|
|
|
|
|
|
|
return response.json()
|
2024-06-05 16:27:57 +00:00
|
|
|
}
|
2024-05-23 20:52:39 +00:00
|
|
|
}
|