From d1ede8d3ee4b65c864fafcbc10358abeb9ebd82d Mon Sep 17 00:00:00 2001 From: John Livingston Date: Mon, 12 Apr 2021 17:53:12 +0200 Subject: [PATCH] Refactoring diagnostic tests in multiple files. --- server/lib/diagnostic/backend.ts | 9 ++ server/lib/diagnostic/chat-type.ts | 27 +++++ server/lib/diagnostic/converse.ts | 76 ++++++++++++++ server/lib/diagnostic/index.ts | 30 ++++++ server/lib/diagnostic/prosody.ts | 9 ++ server/lib/diagnostic/uri.ts | 15 +++ server/lib/diagnostic/utils.ts | 18 ++++ server/lib/diagnostic/video.ts | 54 ++++++++++ server/lib/routers/settings.ts | 159 +---------------------------- 9 files changed, 240 insertions(+), 157 deletions(-) create mode 100644 server/lib/diagnostic/backend.ts create mode 100644 server/lib/diagnostic/chat-type.ts create mode 100644 server/lib/diagnostic/converse.ts create mode 100644 server/lib/diagnostic/index.ts create mode 100644 server/lib/diagnostic/prosody.ts create mode 100644 server/lib/diagnostic/uri.ts create mode 100644 server/lib/diagnostic/utils.ts create mode 100644 server/lib/diagnostic/video.ts diff --git a/server/lib/diagnostic/backend.ts b/server/lib/diagnostic/backend.ts new file mode 100644 index 00000000..15af4b83 --- /dev/null +++ b/server/lib/diagnostic/backend.ts @@ -0,0 +1,9 @@ +import { newResult, TestResult } from './utils' + +export async function diagBackend (test: string, _settingsManager: PluginSettingsManager): Promise { + const result = newResult(test) + result.label = 'Backend connection' + result.ok = true + result.next = 'webchat-video' + return result +} diff --git a/server/lib/diagnostic/chat-type.ts b/server/lib/diagnostic/chat-type.ts new file mode 100644 index 00000000..0e41230e --- /dev/null +++ b/server/lib/diagnostic/chat-type.ts @@ -0,0 +1,27 @@ +import { newResult, TestResult } from './utils' + +export async function diagChatType (test: string, settingsManager: PluginSettingsManager): Promise { + const result = newResult(test) + const typeSettings = await settingsManager.getSettings([ + 'chat-use-prosody', + 'chat-use-builtin', + 'chat-uri' + ]) + result.label = 'Webchat type' + if (typeSettings['chat-use-prosody'] as boolean) { + result.messages.push('Using builtin Prosody') + result.ok = true + result.next = 'prosody' + } else if (typeSettings['chat-use-builtin'] as boolean) { + 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) !== '') { + result.messages.push('Using an external uri') + result.ok = true + result.next = 'use-uri' + } else { + result.messages.push('No webchat configuration') + } + return result +} diff --git a/server/lib/diagnostic/converse.ts b/server/lib/diagnostic/converse.ts new file mode 100644 index 00000000..8453af3a --- /dev/null +++ b/server/lib/diagnostic/converse.ts @@ -0,0 +1,76 @@ +import { newResult, TestResult } from './utils' + +export async function diagConverse (test: string, settingsManager: PluginSettingsManager): Promise { + const result = newResult(test) + result.label = 'Builtin ConverseJS on XMPP service' + const builtinSettings = await settingsManager.getSettings([ + 'chat-server', + 'chat-room', + 'chat-bosh-uri', + 'chat-ws-uri' + ]) + + let isBuiltinError = false + + const chatServer: string = (builtinSettings['chat-server'] as string) || '' + if (chatServer === '') { + result.messages.push('Missing chat server configuration') + isBuiltinError = true + } else if (!/^([a-z0-9.]+)+[a-z0-9]+$/.test(chatServer)) { + result.messages.push( + 'Invalid value for the webchat server: "' + + chatServer + + '"' + ) + isBuiltinError = true + } else { + result.messages.push('Chat server is correct') + } + + const chatRoom: string = (builtinSettings['chat-room'] as string) || '' + if (chatRoom === '') { + result.messages.push('Missing chat room configuration') + isBuiltinError = true + } else if ( + !/^(\w|{{VIDEO_UUID}})+@([a-z0-9.]+)+[a-z0-9]+$/ + .test(chatRoom) + ) { + result.messages.push( + 'Invalid value for the webchat room: "' + + chatRoom + + '"' + ) + isBuiltinError = true + } else { + result.messages.push('Chat room is correct') + } + + const chatBoshUri: string = (builtinSettings['chat-bosh-uri'] as string) || '' + const chatWsUri: string = (builtinSettings['chat-ws-uri'] as string) || '' + if (chatBoshUri === '' && chatWsUri === '') { + result.messages.push('Missing BOSH or Websocket uri') + isBuiltinError = true + } + if (chatBoshUri !== '') { + if (!/^https?:\/\//.test(chatBoshUri)) { + result.messages.push('Invalid BOSH Uri, should begin with https://') + isBuiltinError = true + } else { + result.messages.push('Valid Bosh Uri') + } + } + if (chatWsUri !== '') { + if (!/^wss?:\/\//.test(chatWsUri)) { + result.messages.push('Invalid Websocket Uri, should begin with wss://') + isBuiltinError = true + } else { + result.messages.push('Valid Websocket Uri') + } + } + + if (!isBuiltinError) { + result.messages.push('Builtin converse is correctly configured') + result.ok = true + } + return result +} diff --git a/server/lib/diagnostic/index.ts b/server/lib/diagnostic/index.ts new file mode 100644 index 00000000..dbb5c7b9 --- /dev/null +++ b/server/lib/diagnostic/index.ts @@ -0,0 +1,30 @@ +import { diagBackend } from './backend' +import { diagConverse } from './converse' +import { diagChatType } from './chat-type' +import { TestResult, newResult } from './utils' +import { diagProsody } from './prosody' +import { diagUri } from './uri' +import { diagVideo } from './video' + +export async function diag (test: string, settingsManager: PluginSettingsManager): Promise { + let result: TestResult + + if (test === 'backend') { + result = await diagBackend(test, settingsManager) + } else if (test === 'webchat-video') { + result = await diagVideo(test, settingsManager) + } else if (test === 'webchat-type') { + result = await diagChatType(test, settingsManager) + } else if (test === 'prosody') { + result = await diagProsody(test, settingsManager) + } else if (test === 'converse') { + result = await diagConverse(test, settingsManager) + } else if (test === 'use-uri') { + result = await diagUri(test, settingsManager) + } else { + result = newResult(test) + result.messages.push('Unknown test') + } + + return result +} diff --git a/server/lib/diagnostic/prosody.ts b/server/lib/diagnostic/prosody.ts new file mode 100644 index 00000000..b6bbfa3d --- /dev/null +++ b/server/lib/diagnostic/prosody.ts @@ -0,0 +1,9 @@ +import { newResult, TestResult } from './utils' + +export async function diagProsody (test: string, _settingsManager: PluginSettingsManager): Promise { + const result = newResult(test) + result.ok = false + result.label = 'Builtin Prosody and ConverseJS' + result.messages.push('Not Implemented Yet') + return result +} diff --git a/server/lib/diagnostic/uri.ts b/server/lib/diagnostic/uri.ts new file mode 100644 index 00000000..f0ccc7f9 --- /dev/null +++ b/server/lib/diagnostic/uri.ts @@ -0,0 +1,15 @@ +import { newResult, TestResult } from './utils' + +export async function diagUri (test: string, settingsManager: PluginSettingsManager): Promise { + const result = newResult(test) + result.label = 'External Webchat using an iframe' + const settings = await settingsManager.getSettings([ + 'chat-uri' + ]) + if (/^https:\/\//.test(settings['chat-uri'] as string)) { + result.ok = true + } else { + result.messages.push('Incorrect value for the uri (it does not start with https://)') + } + return result +} diff --git a/server/lib/diagnostic/utils.ts b/server/lib/diagnostic/utils.ts new file mode 100644 index 00000000..f49cd977 --- /dev/null +++ b/server/lib/diagnostic/utils.ts @@ -0,0 +1,18 @@ +type nextValue = 'backend' | 'webchat-video' | 'webchat-type' | 'prosody' | 'converse' | 'use-uri' + +export interface TestResult { + label?: string + messages: string[] + next: nextValue | null + ok: boolean + test: string +} + +export function newResult (test: string): TestResult { + return { + test: test, + ok: false, + messages: [], + next: null + } +} diff --git a/server/lib/diagnostic/video.ts b/server/lib/diagnostic/video.ts new file mode 100644 index 00000000..4a5a0882 --- /dev/null +++ b/server/lib/diagnostic/video.ts @@ -0,0 +1,54 @@ +import { newResult, TestResult } from './utils' + +export async function diagVideo (test: string, settingsManager: PluginSettingsManager): Promise { + const result = newResult(test) + result.label = 'Webchat activated on videos' + const videoSettings = await settingsManager.getSettings([ + 'chat-auto-display', + 'chat-open-blank', + 'chat-only-locals', + 'chat-all-lives', + 'chat-all-non-lives', + 'chat-videos-list' + ]) + if (videoSettings['chat-auto-display']) { + result.messages.push('Chat will open automatically') + } else { + result.messages.push('Chat will not open automatically') + } + + if (videoSettings['chat-open-blank']) { + result.messages.push('Displaying «open in new window» button') + } + + if (videoSettings['chat-only-locals']) { + result.messages.push('Chat will only be available for local videos') + } + + let atLeastOne: boolean = false + if (videoSettings['chat-all-lives']) { + result.messages.push('Chat is enabled for all lives.') + atLeastOne = true + } + if (videoSettings['chat-all-non-lives']) { + result.messages.push('Chat is enabled for all non-lives.') + atLeastOne = true + } + if ((videoSettings['chat-videos-list'] ?? '') !== '') { + const lines = ((videoSettings['chat-videos-list'] ?? '') as string).split('\n') + for (let i = 0; i < lines.length; i++) { + if (/^\s*(-|\w)+\s*($|#)/.test(lines[i])) { + result.messages.push('Chat is activated for a specific videos.') + atLeastOne = true + } + } + } + if (atLeastOne) { + result.ok = true + result.next = 'webchat-type' + } else { + result.ok = false + result.messages.push('Chat is activate for no video.') + } + return result +} diff --git a/server/lib/routers/settings.ts b/server/lib/routers/settings.ts index fa3b8391..e58e852a 100644 --- a/server/lib/routers/settings.ts +++ b/server/lib/routers/settings.ts @@ -1,14 +1,7 @@ import type { Router, Request, Response, NextFunction } from 'express' +import { diag } from '../diagnostic' import { getBaseStaticRoute, isUserAdmin } from '../helpers' -interface Result { - label?: string - messages: string[] - next?: string - ok: boolean - test: string -} - async function initSettingsRouter ({ peertubeHelpers, getRouter, @@ -41,155 +34,7 @@ async function initSettingsRouter ({ const test: string = req.body.test || '' logger.info('Accessing peertube-plugin-livechat diagnostic tool, test "' + test + '".') - const result: Result = { - test: test, - ok: false, - messages: [], - next: undefined - } - if (test === 'backend') { - result.label = 'Backend connection' - result.ok = true - result.next = 'webchat-video' - } else if (test === 'webchat-video') { - result.label = 'Webchat activated on videos' - const videoSettings = await settingsManager.getSettings([ - 'chat-auto-display', - 'chat-open-blank', - 'chat-only-locals', - 'chat-all-lives', - 'chat-all-non-lives', - 'chat-videos-list' - ]) - if (videoSettings['chat-auto-display']) { - result.messages.push('Chat will open automatically') - } else { - result.messages.push('Chat will not open automatically') - } - - if (videoSettings['chat-open-blank']) { - result.messages.push('Displaying «open in new window» button') - } - - if (videoSettings['chat-only-locals']) { - result.messages.push('Chat will only be available for local videos') - } - - let atLeastOne: boolean = false - if (videoSettings['chat-all-lives']) { - result.messages.push('Chat is enabled for all lives.') - atLeastOne = true - } - if (videoSettings['chat-all-non-lives']) { - result.messages.push('Chat is enabled for all non-lives.') - atLeastOne = true - } - if ((videoSettings['chat-videos-list'] ?? '') !== '') { - const lines = ((videoSettings['chat-videos-list'] ?? '') as string).split('\n') - for (let i = 0; i < lines.length; i++) { - if (/^\s*(-|\w)+\s*($|#)/.test(lines[i])) { - result.messages.push('Chat is activated for a specific videos.') - atLeastOne = true - } - } - } - if (atLeastOne) { - result.ok = true - result.next = 'webchat-type' - } else { - result.ok = false - result.messages.push('Chat is activate for no video.') - } - } else if (test === 'webchat-type') { - const typeSettings = await settingsManager.getSettings([ - 'chat-use-prosody', - 'chat-use-builtin', - 'chat-use-uri' - ]) - result.label = 'Webchat type' - if (typeSettings['chat-use-prosody'] as boolean) { - result.messages.push('Using builtin Prosody') - result.ok = true - } else if (typeSettings['chat-use-builtin'] as boolean) { - result.messages.push('Using builtin ConverseJS to connect to an external XMPP server') - const builtinSettings = await settingsManager.getSettings([ - 'chat-server', - 'chat-room', - 'chat-bosh-uri', - 'chat-ws-uri' - ]) - - let isBuiltinError = false - - const chatServer: string = (builtinSettings['chat-server'] as string) || '' - if (chatServer === '') { - result.messages.push('Missing chat server configuration') - isBuiltinError = true - } else if (!/^([a-z0-9.]+)+[a-z0-9]+$/.test(chatServer)) { - result.messages.push( - 'Invalid value for the webchat server: "' + - chatServer + - '"' - ) - isBuiltinError = true - } else { - result.messages.push('Chat server is correct') - } - - const chatRoom: string = (builtinSettings['chat-room'] as string) || '' - if (chatRoom === '') { - result.messages.push('Missing chat room configuration') - isBuiltinError = true - } else if ( - !/^(\w|{{VIDEO_UUID}})+@([a-z0-9.]+)+[a-z0-9]+$/ - .test(chatRoom) - ) { - result.messages.push( - 'Invalid value for the webchat room: "' + - chatRoom + - '"' - ) - isBuiltinError = true - } else { - result.messages.push('Chat room is correct') - } - - const chatBoshUri: string = (builtinSettings['chat-bosh-uri'] as string) || '' - const chatWsUri: string = (builtinSettings['chat-ws-uri'] as string) || '' - if (chatBoshUri === '' && chatWsUri === '') { - result.messages.push('Missing BOSH or Websocket uri') - isBuiltinError = true - } - if (chatBoshUri !== '') { - if (!/^https?:\/\//.test(chatBoshUri)) { - result.messages.push('Invalid BOSH Uri, should begin with https://') - isBuiltinError = true - } else { - result.messages.push('Valid Bosh Uri') - } - } - if (chatWsUri !== '') { - if (!/^wss?:\/\//.test(chatWsUri)) { - result.messages.push('Invalid Websocket Uri, should begin with wss://') - isBuiltinError = true - } else { - result.messages.push('Valid Websocket Uri') - } - } - - if (!isBuiltinError) { - result.messages.push('Builtin converse is correctly configured') - result.ok = true - } - } else if ((typeSettings['chat-use-uri'] as string) !== '') { - result.messages.push('Using an external uri') - result.ok = true - } else { - result.messages.push('No webchat configuration') - } - } else { - result.messages.push('Unknown test') - } + const result = await diag(test, settingsManager) res.status(200) res.json(result)