Chat federation: new settings to disable the feature.

This commit is contained in:
John Livingston
2023-04-20 12:13:22 +02:00
parent 4c138f3f5b
commit 01e7fdc9b6
10 changed files with 189 additions and 10 deletions

View File

@ -1,7 +1,7 @@
import type { RegisterServerOptions, VideoObject, MVideoAP, MVideoFullLight } from '@peertube/peertube-types'
import { videoHasWebchat } from '../../../shared/lib/video'
import { getBoshUri, getWSUri } from '../uri/webchat'
import { fullUri } from '../uri/full'
import { canonicalizePluginUri } from '../uri/canonicalize'
import { getProsodyDomain } from '../prosody/config/domain'
interface LiveChatVideoObject extends VideoObject {
@ -37,9 +37,14 @@ export async function initFederation (options: RegisterServerOptions): Promise<v
'chat-all-non-lives',
'chat-videos-list',
'disable-websocket',
'prosody-room-type'
'prosody-room-type',
'federation-dont-publish-remotely'
])
if (settings['federation-dont-publish-remotely']) {
return jsonld
}
const hasChat = await videoHasWebchat({
'chat-per-live-video': !!settings['chat-per-live-video'],
'chat-all-lives': !!settings['chat-all-lives'],
@ -62,7 +67,7 @@ export async function initFederation (options: RegisterServerOptions): Promise<v
const links = [{
type: 'xmpp-bosh-anonymous',
url: fullUri(options, getBoshUri(options)),
url: canonicalizePluginUri(options, getBoshUri(options), { removePluginVersion: true }),
jid: userJID
}]
if (!settings['disable-websocket']) {
@ -70,7 +75,10 @@ export async function initFederation (options: RegisterServerOptions): Promise<v
if (wsUri) {
links.push({
type: 'xmpp-websocket-anonymous',
url: fullUri(options, wsUri),
url: canonicalizePluginUri(options, wsUri, {
removePluginVersion: true,
protocol: 'ws'
}),
jid: userJID
})
}

View File

@ -3,9 +3,12 @@ import type { Response } from 'express'
import type { IncomingMessage } from 'http'
import type { Duplex } from 'stream'
const pluginVersionRegexp = /^\d+\.\d+\.\d+(?:-(?:rc|alpha|beta)\.\d+)?$/
const pluginVersionWordBreakRegex = /\b\d+\.\d+\.\d+(?:-(?:rc|alpha|beta)\.\d+)?\b/
const packagejson: any = require('../../../package.json')
const version: string = packagejson.version || ''
if (!/^\d+\.\d+\.\d+/.test(version)) {
if (!pluginVersionRegexp.test(version)) {
throw new Error('Incorrect version in package.json.')
}
const pluginName: string = packagejson.name || ''
@ -94,5 +97,7 @@ export {
isUserAdmin,
getUserNickname,
pluginName,
pluginShortName
pluginShortName,
pluginVersionRegexp,
pluginVersionWordBreakRegex
}

View File

@ -97,6 +97,29 @@ Please read
private: true
})
// ********** Federation
registerSetting({
type: 'html',
private: true,
descriptionHTML: loc('federation_description')
})
registerSetting({
name: 'federation-no-remote-chat',
label: loc('federation_no_remote_chat_label'),
descriptionHTML: loc('federation_no_remote_chat_description'),
type: 'input-checkbox',
default: false,
private: false
})
registerSetting({
name: 'federation-dont-publish-remotely',
label: loc('federation_dont_publish_remotely_label'),
descriptionHTML: loc('federation_dont_publish_remotely_description'),
type: 'input-checkbox',
default: false,
private: true
})
// ********** Chat behaviour
registerSetting({
type: 'html',

View File

@ -1,10 +1,52 @@
import type { RegisterServerOptions } from '@peertube/peertube-types'
import { pluginVersionWordBreakRegex } from '../helpers'
import * as url from 'url'
export function fullUri (options: RegisterServerOptions, path: string): string {
if (path.startsWith('https://') || path.startsWith('http://')) {
return path
const removeVersionRegex = new RegExp(
/\/plugins\/livechat\//.source +
pluginVersionWordBreakRegex.source +
/\//.source
)
interface CanonicalizeOptions {
protocol?: 'http' | 'ws'
removePluginVersion?: boolean
}
/**
* Takes a Plugin uri or route path (for example an API endpoint, the websocket route, ...),
* and returns a canonicalized version that include the host, and can handle different options
* (with the given scheme, without the plugin version, ...)
* @param options Peertube server options
* @param path the uri to canonicalize
* @param canonicalizeOptions canonicalize options
* @returns the canonicalize uri
*/
export function canonicalizePluginUri (
options: RegisterServerOptions,
path: string,
canonicalizeOptions?: CanonicalizeOptions
): string {
let uri: url.URL
if (path.match(/^(http|ws)s?:\/\//)) {
uri = new url.URL(path)
} else {
uri = new url.URL(path, options.peertubeHelpers.config.getWebserverUrl())
}
if (canonicalizeOptions?.protocol) {
// Assuming that current protocol is https? or wss?, other cases dont concern us, and will be buggy
const currentProtocolSecure = uri.protocol.endsWith('s')
if (canonicalizeOptions.protocol === 'http') {
uri.protocol = currentProtocolSecure ? 'https' : 'http'
} else if (canonicalizeOptions.protocol === 'ws') {
uri.protocol = currentProtocolSecure ? 'wss' : 'ws'
}
}
if (canonicalizeOptions?.removePluginVersion) {
uri.pathname = uri.pathname.replace(
removeVersionRegex,
'/plugins/livechat/'
)
}
const uri = new url.URL(path, options.peertubeHelpers.config.getWebserverUrl())
return uri.toString()
}