Replacing old settings by chat-type.

This commit is contained in:
John Livingston 2021-06-03 11:46:11 +02:00
parent 36146ee76c
commit d0b44c3486
7 changed files with 54 additions and 82 deletions

View File

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

View File

@ -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 === '') {

View File

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

View File

@ -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<vo
logger.debug('Calling ensureProsodyRunning')
logger.debug('Checking if prosody should be active')
const setting = await settingsManager.getSetting('chat-use-prosody')
if (!setting) {
logger.info('Prosody is not activated, we wont launch it')
const setting = await settingsManager.getSetting('chat-type')
if (setting !== ('builtin-prosody' as ChatType)) {
logger.info('Chat type is not set to builtin-prosody, we wont launch it')
return
}

View File

@ -6,6 +6,7 @@ import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered }
import { getUserNickname } from '../helpers'
import { Affiliations, getVideoAffiliations } from '../prosody/config/affiliations'
import { getProsodyDomain } from '../prosody/config/domain'
import type { ChatType } from '../../../shared/lib/types'
// See here for description: https://modules.prosody.im/mod_muc_http_defaults.html
interface RoomDefaults {
@ -46,13 +47,13 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
}
// 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> {
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> {
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

View File

@ -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<Route
res.removeHeader('X-Frame-Options') // this route can be opened in an iframe
const settings = await settingsManager.getSettings([
'chat-use-prosody', 'chat-use-builtin', 'chat-room', 'chat-server',
'chat-type', 'chat-room', 'chat-server',
'chat-bosh-uri', 'chat-ws-uri'
])
const chatType: ChatType = (settings['chat-type'] ?? 'disabled') as ChatType
let server: string
let room: string
@ -37,7 +39,7 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
let wsUri: string
let authenticationUrl: string = ''
let advancedControls: boolean = false
if (settings['chat-use-prosody']) {
if (chatType === 'builtin-prosody') {
const prosodyDomain = await getProsodyDomain(options)
server = 'anon.' + prosodyDomain
room = '{{VIDEO_UUID}}@room.' + prosodyDomain
@ -47,7 +49,7 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
getBaseRouterRoute(options) +
'api/auth'
advancedControls = true
} else if (settings['chat-use-builtin']) {
} else if (chatType === 'builtin-converse') {
if (!settings['chat-server']) {
throw new Error('Missing chat-server settings.')
}

View File

@ -162,19 +162,9 @@ Before asking for help, please use this diagnostic tool:
private: true
})
registerSetting({
name: 'chat-use-prosody',
label: 'Use builtin Prosody XMPP Server',
type: 'input-checkbox',
// /!\ dont auto-activate on existing settups. FIXME: how to do this?
default: false, // TODO: set to true when we have a way to know if the plugin was previously installed.
private: false,
descriptionHTML: 'If checked, this will use a builtin XMPP server. This is the recommanded setup.'
})
registerSetting({
name: 'prosody-port',
label: 'Builtin prosody: Prosody port',
label: 'Prosody port',
type: 'input',
default: '52800',
private: true,
@ -183,35 +173,20 @@ Before asking for help, please use this diagnostic tool:
'You can close this port on your firewall, it will not be accessed from the outer world.'
})
registerSetting({
name: 'chat-use-builtin',
label: 'Use builtin ConverseJS',
type: 'input-checkbox',
default: false,
private: false,
descriptionHTML: 'If checked, use a builtin ConverseJS iframe.<br>' +
'You still have to configure an external XMPP service. Please see the ' +
'<a href="https://github.com/JohnXLivingston/peertube-plugin-livechat" target="_blank">documentation<a>.<br>' +
'If you have no running webchat service, you can follow this ' +
// eslint-disable-next-line max-len
'<a href="https://github.com/JohnXLivingston/peertube-plugin-livechat/blob/main/documentation/tutorials/prosody.md" target="blank_">tutorial</a>.'
})
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:<br>' +
'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:<br>' +
'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.<br>' +
'Example: public@room.peertube.im.your_domain<br>' +
'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:<br>' +
'URI of the external BOSH server. Please make sure it accept cross origin request from your domain.<br>' +
descriptionHTML: 'URI of the external BOSH server. ' +
'Please make sure it accept cross origin request from your domain.<br>' +
'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:<br>' +
'URI of the external WS server. Please make sure it accept cross origin request from your domain.<br>' +
descriptionHTML: 'URI of the external WS server. ' +
'Please make sure it accept cross origin request from your domain.<br>' +
'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: '<b>If you dont want to use the builtin ConverseJS webchat:</b><br>' +
'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}}.<br>' +
'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)
}