From d0b44c3486576a2343bd8a4483332cce5e7fbd4f Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 3 Jun 2021 11:46:11 +0200 Subject: [PATCH] Replacing old settings by chat-type. --- client/admin-plugin-client-plugin.ts | 27 ++++--------- client/videowatch-client-plugin.ts | 8 ++-- server/lib/diagnostic/chat-type.ts | 16 ++++---- server/lib/prosody/ctl.ts | 7 ++-- server/lib/routers/api.ts | 13 ++++--- server/lib/routers/webchat.ts | 8 ++-- server/lib/settings.ts | 57 ++++++++-------------------- 7 files changed, 54 insertions(+), 82 deletions(-) diff --git a/client/admin-plugin-client-plugin.ts b/client/admin-plugin-client-plugin.ts index e98e769c..615ae91f 100644 --- a/client/admin-plugin-client-plugin.ts +++ b/client/admin-plugin-client-plugin.ts @@ -1,7 +1,4 @@ import type { ChatType } from 'shared/lib/types' -const prosodySettings = ['prosody-port'] -const converseSettings = ['chat-server', 'chat-room', 'chat-bosh-uri', 'chat-ws-uri'] -const otherSettings: string[] = [] function register ({ registerSettingsScript }: RegisterOptions): void { registerSettingsScript({ @@ -10,30 +7,22 @@ function register ({ registerSettingsScript }: RegisterOptions): void { switch (name) { case 'chat-type-help-disabled': return options.formValues['chat-type'] !== ('disabled' as ChatType) + case 'prosody-port': case 'chat-type-help-builtin-prosody': return options.formValues['chat-type'] !== ('builtin-prosody' as ChatType) + case 'chat-server': + case 'chat-room': + case 'chat-bosh-uri': + case 'chat-ws-uri': case 'chat-type-help-builtin-converse': return options.formValues['chat-type'] !== ('builtin-converse' as ChatType) + case 'chat-uri': case 'chat-type-help-external-uri': return options.formValues['chat-type'] !== ('external-uri' as ChatType) + case 'chat-style': + return options.formValues['chat-type'] === 'disabled' } - // TODO: rewrite the code bellow. - if (prosodySettings.includes(name)) { - return options.formValues['chat-use-prosody'] !== true - } - if (name === 'chat-use-builtin') { - return options.formValues['chat-use-prosody'] === true - } - if (converseSettings.includes(name)) { - return options.formValues['chat-use-builtin'] !== true || options.formValues['chat-use-prosody'] === true - } - if (name === 'chat-uri') { - return options.formValues['chat-use-prosody'] === true || options.formValues['chat-use-builtin'] === true - } - if (otherSettings.includes(name)) { - return options.formValues['chat-use-builtin'] === true || options.formValues['chat-use-prosody'] === true - } return false } }) diff --git a/client/videowatch-client-plugin.ts b/client/videowatch-client-plugin.ts index 55790851..37a7c549 100644 --- a/client/videowatch-client-plugin.ts +++ b/client/videowatch-client-plugin.ts @@ -1,4 +1,5 @@ import { videoHasWebchat } from 'shared/lib/video' +import type { ChatType } from 'shared/lib/types' interface VideoWatchLoadedHookOptions { videojs: any @@ -33,10 +34,11 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void { return null } let iframeUri = '' - if (settings['chat-use-prosody'] || settings['chat-use-builtin']) { + const chatType: ChatType = (settings['chat-type'] ?? 'disabled') as ChatType + if (chatType === 'builtin-prosody' || chatType === 'builtin-converse') { // Using the builtin converseJS iframeUri = getBaseRoute() + '/webchat/room/' + encodeURIComponent(uuid) - } else if (!settings['chat-use-builtin']) { + } else if (chatType === 'external-uri') { iframeUri = settings['chat-uri'] || '' iframeUri = iframeUri.replace(/{{VIDEO_UUID}}/g, encodeURIComponent(uuid)) if (!/^https?:\/\//.test(iframeUri)) { @@ -44,7 +46,7 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void { return null } } else { - logger.error('Dont known which url use for the iframe.') + logger.error('Chat disabled.') return null } if (iframeUri === '') { diff --git a/server/lib/diagnostic/chat-type.ts b/server/lib/diagnostic/chat-type.ts index 6ef6b7ef..11d6200b 100644 --- a/server/lib/diagnostic/chat-type.ts +++ b/server/lib/diagnostic/chat-type.ts @@ -1,27 +1,29 @@ import { newResult, TestResult } from './utils' +import type { ChatType } from '../../../shared/lib/types' export async function diagChatType (test: string, { settingsManager }: RegisterServerOptions): Promise { const result = newResult(test) const typeSettings = await settingsManager.getSettings([ - 'chat-use-prosody', - 'chat-use-builtin', - 'chat-uri' + 'chat-type' ]) result.label = 'Webchat type' - if (typeSettings['chat-use-prosody'] as boolean) { + const chatType: ChatType = (typeSettings['chat-type'] ?? 'disabled') as ChatType + if (chatType === 'builtin-prosody') { result.messages.push('Using builtin Prosody') result.ok = true result.next = 'prosody' - } else if (typeSettings['chat-use-builtin'] as boolean) { + } else if (chatType === 'builtin-converse') { result.messages.push('Using builtin ConverseJS to connect to an external XMPP server') result.ok = true result.next = 'converse' - } else if (((typeSettings['chat-uri'] || '') as string) !== '') { + } else if (chatType === 'external-uri') { result.messages.push('Using an external uri') result.ok = true result.next = 'use-uri' + } else if (chatType === 'disabled') { + result.messages.push('Webchat disabled') } else { - result.messages.push('No webchat configuration') + result.messages.push('Unknown chat type value: ' + (chatType as string)) } return result } diff --git a/server/lib/prosody/ctl.ts b/server/lib/prosody/ctl.ts index 71c56f5c..dd033ed2 100644 --- a/server/lib/prosody/ctl.ts +++ b/server/lib/prosody/ctl.ts @@ -1,6 +1,7 @@ import { getProsodyConfig, getProsodyFilePaths, writeProsodyConfig } from './config' import { startProsodyLogRotate, stopProsodyLogRotate } from './logrotate' import { changeHttpBindRoute } from '../routers/webchat' +import type { ChatType } from '../../../shared/lib/types' import * as fs from 'fs' import * as child_process from 'child_process' @@ -142,9 +143,9 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise { } // check settings (chat enabled for this video?) const settings = await options.settingsManager.getSettings([ - 'chat-use-prosody', + 'chat-type', 'chat-only-locals', 'chat-all-lives', 'chat-all-non-lives', 'chat-videos-list' ]) - if (!settings['chat-use-prosody']) { + if (settings['chat-type'] !== ('builtin-prosody' as ChatType)) { logger.warn('Prosody chat is not active') res.sendStatus(403) return @@ -121,13 +122,13 @@ async function initApiRouter (options: RegisterServerOptions): Promise { router.get('/user/check_password', asyncMiddleware( async (req: Request, res: Response, _next: NextFunction) => { const settings = await options.settingsManager.getSettings([ - 'chat-use-prosody', + 'chat-type', 'chat-only-locals', 'chat-all-lives', 'chat-all-non-lives', 'chat-videos-list' ]) - if (!settings['chat-use-prosody']) { + if (settings['chat-type'] !== ('builtin-prosody' as ChatType)) { logger.warn('Prosody chat is not active') res.status(200).send('false') return @@ -152,13 +153,13 @@ async function initApiRouter (options: RegisterServerOptions): Promise { router.get('/user/user_exists', asyncMiddleware( async (req: Request, res: Response, _next: NextFunction) => { const settings = await options.settingsManager.getSettings([ - 'chat-use-prosody', + 'chat-type', 'chat-only-locals', 'chat-all-lives', 'chat-all-non-lives', 'chat-videos-list' ]) - if (!settings['chat-use-prosody']) { + if (settings['chat-type'] !== ('builtin-prosody' as ChatType)) { logger.warn('Prosody chat is not active') res.status(200).send('false') return diff --git a/server/lib/routers/webchat.ts b/server/lib/routers/webchat.ts index ebb5b5c5..636d04e9 100644 --- a/server/lib/routers/webchat.ts +++ b/server/lib/routers/webchat.ts @@ -1,5 +1,6 @@ import type { Router, RequestHandler, Request, Response, NextFunction } from 'express' import type { ProxyOptions } from 'express-http-proxy' +import type { ChatType } from '../../../shared/lib/types' import { getBaseRouterRoute, getBaseStaticRoute } from '../helpers' import { asyncMiddleware } from '../middlewares/async' import { getProsodyDomain } from '../prosody/config/domain' @@ -27,9 +28,10 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise' + - 'You still have to configure an external XMPP service. Please see the ' + - 'documentation.
' + - 'If you have no running webchat service, you can follow this ' + - // eslint-disable-next-line max-len - '
tutorial.' - }) registerSetting({ name: 'chat-server', - label: 'Builtin webchat: XMPP service server', + label: 'XMPP service server', type: 'input', default: '', - descriptionHTML: 'When using the built-in converseJS webchat:
' + - 'Your XMPP server. Without any scheme. Example : peertube.im.your_domain.', + descriptionHTML: 'Your XMPP server. Without any scheme. Example : peertube.im.your_domain.', private: true }) registerSetting({ name: 'chat-room', - label: 'Builtin webchat: XMPP room template', + label: 'XMPP room template', type: 'input', default: '', - descriptionHTML: 'When using the built-in converseJS webchat:
' + - 'Your XMPP room. You can use the placeholder {{VIDEO_UUID}} to add the video UUID.' + + descriptionHTML: 'Your XMPP room. You can use the placeholder {{VIDEO_UUID}} to add the video UUID.' + 'Without this placeholder, all videos will point to the same chat room.
' + 'Example: public@room.peertube.im.your_domain
' + 'Example: public_{{VIDEO_UUID}}@room.peertube.im.your_domain', @@ -219,21 +194,21 @@ Before asking for help, please use this diagnostic tool: }) registerSetting({ name: 'chat-bosh-uri', - label: 'Builtin webchat: BOSH uri', + label: 'BOSH uri', type: 'input', default: '', - descriptionHTML: 'When using the built-in converseJS webchat:
' + - 'URI of the external BOSH server. Please make sure it accept cross origin request from your domain.
' + + descriptionHTML: 'URI of the external BOSH server. ' + + 'Please make sure it accept cross origin request from your domain.
' + 'You must at least have a BOSH or a Websocket uri.', private: true }) registerSetting({ name: 'chat-ws-uri', - label: 'Builtin webchat: WS uri', + label: 'Websocket uri', type: 'input', default: '', - descriptionHTML: 'When using the built-in converseJS webchat:
' + - 'URI of the external WS server. Please make sure it accept cross origin request from your domain.
' + + descriptionHTML: 'URI of the external WS server. ' + + 'Please make sure it accept cross origin request from your domain.
' + 'You must at least have a BOSH or a Websocket uri.', private: true }) @@ -243,8 +218,7 @@ Before asking for help, please use this diagnostic tool: label: 'Webchat url', type: 'input', default: '', - descriptionHTML: 'If you dont want to use the builtin ConverseJS webchat:
' + - 'Put here your webchat url. An iframe will be created pointing to this url. ' + + descriptionHTML: 'Put here your webchat url. An iframe will be created pointing to this url. ' + 'The placeholder {{VIDEO_UUID}} will be replace by the video UUID if present. ' + 'Example : https://my_domain/conversejs.html?room=video_{{VIDEO_UUID}}.
' + 'If this field is empty, it will use the builtin ConverseJS webchat.', @@ -264,11 +238,12 @@ Before asking for help, please use this diagnostic tool: // settings changes management settingsManager.onSettingsChange(async (settings: any) => { - if ('chat-use-prosody' in settings) { - if (settings['chat-use-prosody'] === true) { + if ('chat-type' in settings) { + const chatType: ChatType = settings['chat-type'] ?? 'disabled' + if (chatType === 'builtin-prosody') { peertubeHelpers.logger.info('Saving settings, ensuring prosody is running') await ensureProsodyRunning(options) - } else if (settings['chat-use-prosody'] === false) { + } else { peertubeHelpers.logger.info('Saving settings, ensuring prosody is not running') await ensureProsodyNotRunning(options) }