From a3579abb8160cfdba192be106e15033bbd826616 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Wed, 4 Aug 2021 00:22:19 +0200 Subject: [PATCH] Builtin ConverseJS on external XMPP server: new placeholders for the room name: CHANNEL_ID, CHANNEL_NAME. --- CHANGELOG.md | 6 ++++++ documentation/conversejs.md | 10 +++++++++- package.json | 2 +- server/lib/database/channel.ts | 26 ++++++++++++++++++++++++++ server/lib/diagnostic/converse.ts | 2 +- server/lib/routers/webchat.ts | 16 ++++++++++++++++ server/lib/settings.ts | 9 +++++++-- 7 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 server/lib/database/channel.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a2f49a60..b0b861ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/documentation/conversejs.md b/documentation/conversejs.md index 735a146c..db38b4f3 100644 --- a/documentation/conversejs.md +++ b/documentation/conversejs.md @@ -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. diff --git a/package.json b/package.json index 7fb12978..ff0f0798 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/server/lib/database/channel.ts b/server/lib/database/channel.ts new file mode 100644 index 00000000..fdbcfee2 --- /dev/null +++ b/server/lib/database/channel.ts @@ -0,0 +1,26 @@ +async function getChannelNameById (options: RegisterServerOptions, channelId: number): Promise { + 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 +} diff --git a/server/lib/diagnostic/converse.ts b/server/lib/diagnostic/converse.ts index 41da1f92..77bd08fd 100644 --- a/server/lib/diagnostic/converse.ts +++ b/server/lib/diagnostic/converse.ts @@ -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( diff --git a/server/lib/routers/webchat.ts b/server/lib/routers/webchat.ts index aa23019c..50862a1a 100644 --- a/server/lib/routers/webchat.ts +++ b/server/lib/routers/webchat.ts @@ -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 +`Your XMPP room. You can use following placeholders to inject video metadata in the room name: +
    +
  • {{VIDEO_UUID}} to add the video UUID.
  • +
  • {{CHANNEL_ID}} to add the CHANNEL numerical ID.
  • +
  • {{CHANNEL_NAME}} to add the channel name (see the Peertube's documentation for possible characters).
  • +
+Without any placeholder, all videos will point to the same chat room.
Example: public@room.peertube.im.your_domain
Example: public_{{VIDEO_UUID}}@room.peertube.im.your_domain`, private: true