Builtin ConverseJS on external XMPP server: new placeholders for the room name: CHANNEL_ID, CHANNEL_NAME.

This commit is contained in:
John Livingston 2021-08-04 00:22:19 +02:00
parent 98a9515a50
commit a3579abb81
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
7 changed files with 66 additions and 5 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## ???
### Features
* Builtin ConverseJS on external XMPP server: new placeholders for the room name: CHANNEL_ID, CHANNEL_NAME.
## v3.2.0
### Features

View File

@ -24,10 +24,18 @@ The ```peertube.im``` is part of the example, you have to replace the entire val
#### XMPP room template (mandatory)
The room to join on your XMPP server.
You can have a single room for all webchats, or you can use the placeholder ```{{{VIDEO_UUID}}}``` to insert the video UUID and have a custom room for each video.
You can have a single room for all webchats, or you can use any of there placeholders:
- ```{{{VIDEO_UUID}}}``` to insert the video UUID and have a custom room for each video.
- ```{{{CHANNEL_ID}}}``` to insert the channel numerical ID and have a custom room for each channel.
- ```{{{CHANNEL_NAME}}}``` to insert the channel name (see the Peertube's documentation for possible characters) and have a custom room for each channel.
You can mix several placeholder.
Example: ```room_{{VIDEO_UUID}}@room.peertube.im.your_domain```
NB: when using CHANNEL_ID or CHANNEL_NAME with remote videos, you can have unexpected results. You should consider disabling webchat for remote videos.
#### BOSH uri OR Websocket uri
You have to provide at least one of these two settings.

View File

@ -1,7 +1,7 @@
{
"name": "peertube-plugin-livechat",
"description": "PeerTube plugin livechat",
"version": "3.2.0",
"version": "3.3.0",
"author": "John Livingston",
"bugs": "https://github.com/JohnXLivingston/peertube-plugin-livechat/issues",
"clientScripts": [

View File

@ -0,0 +1,26 @@
async function getChannelNameById (options: RegisterServerOptions, channelId: number): Promise<string | null> {
if (!channelId) {
throw new Error('Missing channelId')
}
if (!Number.isInteger(channelId)) {
throw new Error('Invalid channelId: not an integer')
}
const [results] = await options.peertubeHelpers.database.query(
'SELECT "actor"."preferredUsername"' +
' FROM "videoChannel"' +
' RIGHT JOIN "actor" ON "actor"."id" = "videoChannel"."actorId"' +
' WHERE "videoChannel"."id" = ' + channelId.toString()
)
if (!Array.isArray(results)) {
throw new Error('getChannelNameById: query result is not an array.')
}
if (!results[0]) {
options.peertubeHelpers.logger.debug(`getChannelNameById: channel ${channelId} not found.`)
return null
}
return (results[0].preferredUsername ?? null) as string
}
export {
getChannelNameById
}

View File

@ -32,7 +32,7 @@ export async function diagConverse (test: string, { settingsManager }: RegisterS
result.messages.push('Missing chat room configuration')
isBuiltinError = true
} else if (
!/^(\w|{{VIDEO_UUID}})+@([a-z0-9.]+)+[a-z0-9]+$/
!/^(\w|{{(VIDEO_UUID|CHANNEL_ID|CHANNEL_NAME)}})+@([a-z0-9.]+)+[a-z0-9]+$/
.test(chatRoom)
) {
result.messages.push(

View File

@ -5,6 +5,7 @@ import { getBaseRouterRoute, getBaseStaticRoute, isUserAdmin } from '../helpers'
import { asyncMiddleware } from '../middlewares/async'
import { getProsodyDomain } from '../prosody/config/domain'
import { getAPIKey } from '../apikey'
import { getChannelNameById } from '../database/channel'
import * as path from 'path'
const bodyParser = require('body-parser')
const got = require('got')
@ -84,7 +85,22 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
const baseStaticUrl = getBaseStaticRoute(options)
page = page.replace(/{{BASE_STATIC_URL}}/g, baseStaticUrl)
page = page.replace(/{{JID}}/g, server)
// Computing the room name...
room = room.replace(/{{VIDEO_UUID}}/g, video.uuid)
room = room.replace(/{{CHANNEL_ID}}/g, `${video.channelId}`)
if (room.includes('CHANNEL_NAME')) {
const channelName = await getChannelNameById(options, video.channelId)
if (channelName === null) {
throw new Error('Channel not found')
}
if (!/^[a-zA-Z0-9_.]+$/.test(channelName)) {
// FIXME: see if there is a response here https://github.com/Chocobozzz/PeerTube/issues/4301 for allowed chars
peertubeHelpers.logger.error(`Invalid channel name, contains unauthorized chars: '${channelName}'`)
throw new Error('Invalid channel name, contains unauthorized chars')
}
room = room.replace(/{{CHANNEL_NAME}}/g, channelName)
}
// ... then inject it in the page.
page = page.replace(/{{ROOM}}/g, room)
page = page.replace(/{{BOSH_SERVICE_URL}}/g, boshUri)
page = page.replace(/{{WS_SERVICE_URL}}/g, wsUri)

View File

@ -124,8 +124,13 @@ You can close this port on your firewall, it will not be accessed from the outer
type: 'input',
default: '',
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>
`Your XMPP room. You can use following placeholders to inject video metadata in the room name:
<ul>
<li>{{VIDEO_UUID}} to add the video UUID.</li>
<li>{{CHANNEL_ID}} to add the CHANNEL numerical ID.</li>
<li>{{CHANNEL_NAME}} to add the channel name (see the Peertube's documentation for possible characters).</li>
</ul>
Without any 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`,
private: true