Chat federation: new settings to disable the feature.
This commit is contained in:
@ -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()
|
||||
}
|
||||
|
Reference in New Issue
Block a user