Refactoring diagnostic tests in multiple files.

This commit is contained in:
John Livingston 2021-04-12 17:53:12 +02:00
parent 6bc1f66cf1
commit d1ede8d3ee
9 changed files with 240 additions and 157 deletions

View File

@ -0,0 +1,9 @@
import { newResult, TestResult } from './utils'
export async function diagBackend (test: string, _settingsManager: PluginSettingsManager): Promise<TestResult> {
const result = newResult(test)
result.label = 'Backend connection'
result.ok = true
result.next = 'webchat-video'
return result
}

View File

@ -0,0 +1,27 @@
import { newResult, TestResult } from './utils'
export async function diagChatType (test: string, settingsManager: PluginSettingsManager): Promise<TestResult> {
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
}

View File

@ -0,0 +1,76 @@
import { newResult, TestResult } from './utils'
export async function diagConverse (test: string, settingsManager: PluginSettingsManager): Promise<TestResult> {
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
}

View File

@ -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<TestResult> {
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
}

View File

@ -0,0 +1,9 @@
import { newResult, TestResult } from './utils'
export async function diagProsody (test: string, _settingsManager: PluginSettingsManager): Promise<TestResult> {
const result = newResult(test)
result.ok = false
result.label = 'Builtin Prosody and ConverseJS'
result.messages.push('Not Implemented Yet')
return result
}

View File

@ -0,0 +1,15 @@
import { newResult, TestResult } from './utils'
export async function diagUri (test: string, settingsManager: PluginSettingsManager): Promise<TestResult> {
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
}

View File

@ -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
}
}

View File

@ -0,0 +1,54 @@
import { newResult, TestResult } from './utils'
export async function diagVideo (test: string, settingsManager: PluginSettingsManager): Promise<TestResult> {
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
}

View File

@ -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)