Builtin ConverseJS:

* Building with webpack. TODO: include assets.
* Page template
This commit is contained in:
John Livingston 2021-02-20 20:42:41 +01:00
parent fbfb38392d
commit d9e461f984
7 changed files with 129 additions and 8 deletions

View File

@ -27,7 +27,10 @@ function register ({ registerHook, peertubeHelpers }) {
function getBaseRoute () {
// FIXME: should be provided by PeertubeHelpers (does not exists for now)
return '/plugins/livechat/router'
// We are guessing the route with the correct plugin version with this trick:
const staticBase = peertubeHelpers.getBaseStaticRoute()
// we can't use '/plugins/livechat/router', because the loaded html page needs correct relative paths.
return staticBase.replace(/\/static.*$/, '/router')
}
function getIframeUri (uuid) {
@ -38,7 +41,7 @@ function register ({ registerHook, peertubeHelpers }) {
let iframeUri = ''
if (!settings['chat-use-builtin']) {
iframeUri = settings['chat-uri'] || ''
iframeUri = iframeUri.replace('{{VIDEO_UUID}}', uuid)
iframeUri = iframeUri.replace(/{{VIDEO_UUID}}/g, uuid)
if (!/^https?:\/\//.test(iframeUri)) {
logger.error('The webchaturi must begin with https://')
return null

41
conversejs/conversejs.js Normal file
View File

@ -0,0 +1,41 @@
require('converse.js')
window.initConverse = function initConverse ({
baseStaticUrl,
room,
boshServiceUrl
}) {
window.converse.initialize({
assets_path: baseStaticUrl,
authentication: 'anonymous',
auto_login: true,
auto_join_rooms: [
room
],
bosh_service_url: boshServiceUrl,
jid: 'peertube.im.your_domain',
notify_all_room_messages: [
room
],
singleton: true,
auto_focus: false,
hide_muc_participants: false,
keepalive: true,
play_sounds: false,
muc_mention_autocomplete_min_chars: 3,
muc_mention_autocomplete_filter: 'contains',
modtools_disable_assign: true,
muc_disable_slash_commands: [
'admin', 'ban', 'clear', 'deop', 'destroy', 'kick',
'member', 'modtools', 'mute', 'op', 'owner', 'register',
'revoke', 'subject', 'topic', 'voice'
],
muc_instant_rooms: true,
show_client_info: false,
allow_adhoc_commands: false,
allow_contact_requests: false,
show_controlbox_by_default: false,
view_mode: 'fullscreen'
})
}

26
conversejs/index.html Normal file
View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html class="no-js">
<head>
<title>Converse</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content="Converse XMPP/Jabber Chat"/>
<script src="{{BASE_STATIC_URL}}/conversejs.js"></script>
<style type="text/css">
#conversejs .chat-head {
display: none;
}
</style>
</head>
<body class="converse-fullscreen">
<noscript>You need to enable JavaScript to run the Converse.js chat app.</noscript>
<div id="conversejs-bg"></div>
<script>
initConverse({
baseStaticUrl : '{{BASE_STATIC_URL}}',
room: '{{ROOM}}',
boshServiceUrl: '{{BOSH_SERVICE_URL}}'
})
</script>
</body>
</html>

40
main.js
View File

@ -1,7 +1,10 @@
const path = require('path')
const fs = require('fs').promises
async function register ({
_registerHook,
registerSetting,
_settingsManager,
settingsManager,
_storageManager,
_videoCategoryManager,
_videoLicenceManager,
@ -53,11 +56,20 @@ async function register ({
descriptionHTML: 'If checked, use a builtin ConverseJS iframe.<br>' +
'You still have to configure an external XMPP service. Please see the documentation.'
})
registerSetting({
name: 'chat-room-server',
label: 'Builtin webchat: XMPP room service server',
type: 'input',
default: '',
descriptionHTML: 'When using the built-in converseJS webchat:<br>' +
'Your XMPP room server. Example : room.peertube.im.your_domain.',
private: true
})
registerSetting({
name: 'chat-bosh-uri',
label: 'Builtin webchat: BOSH uri',
type: 'input',
default: true,
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.',
private: true
@ -86,10 +98,23 @@ async function register ({
private: false
})
const converseJSIndex = await fs.readFile(path.resolve(__dirname, './conversejs/index.html'))
const router = getRouter()
router.get('/ping', (req, res) => res.json({ message: 'pong' }))
router.get('/webchat', async (req, res, next) => {
try {
const settings = await settingsManager.getSettings([
'chat-use-builtin', 'chat-room-server', 'chat-bosh-uri'
])
if (!settings['chat-use-builtin']) {
throw new Error('Builtin chat disabled.')
}
if (!settings['chat-room-server']) {
throw new Error('Missing chat-room-server settings.')
}
// FIXME: with Peertube 3.0.1 the following method is not available...
// When loadByIdOrUUID is available, change the entry point to
// be /webchat/:videoId
@ -109,7 +134,16 @@ async function register ({
if (!video) {
throw new Error('Video not found')
}
res.send('ok')
let page = '' + converseJSIndex
// FIXME: Peertube should provide the static folder path. For now:
const staticRelative = '../static/conversejs'
page = page.replace(/{{BASE_STATIC_URL}}/g, staticRelative)
page = page.replace(/{{ROOM}}/g, 'public_' + video.uuid + '@' + settings['chat-room-server'])
page = page.replace(/{{BOSH_SERVICE_URL}}/g, settings['chat-bosh-uri'])
res.status(200)
res.send(page)
} catch (error) {
return next(error)
}

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "peertube-plugin-livechat",
"version": "0.0.5",
"version": "0.0.8",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -995,6 +995,12 @@
"integrity": "sha1-/ozxhP9mcLa67wGp1IYaXL7EEgo=",
"dev": true
},
"converse.js": {
"version": "7.0.4",
"resolved": "https://registry.npmjs.org/converse.js/-/converse.js-7.0.4.tgz",
"integrity": "sha512-U07RJB71nG88IRlfhlcxOlJAnH4pVNH1H8jF39OANxZbqSikOX3qJO3e3fV0Tem0I9oNgDqkRW60pI22zjeP0w==",
"dev": true
},
"copy-concurrently": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/copy-concurrently/-/copy-concurrently-1.0.5.tgz",

View File

@ -1,7 +1,7 @@
{
"name": "peertube-plugin-livechat",
"description": "PeerTube plugin livechat",
"version": "0.0.8",
"version": "0.1.0",
"author": "John Livingston",
"bugs": "https://github.com/JohnXLivingston/peertube-plugin-livechat/issues",
"clientScripts": [
@ -23,6 +23,7 @@
],
"devDependencies": {
"@purtuga/esm-webpack-plugin": "^1.1.1",
"converse.js": "^7.0.4",
"eslint": "^7.20.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
@ -45,7 +46,9 @@
"build": "webpack --mode=production",
"lint": "npx eslint --ext .js ."
},
"staticDirs": {},
"staticDirs": {
"conversejs": "dist/conversejs"
},
"translations": {
"fr-FR": "./languages/fr.json"
}

View File

@ -18,4 +18,12 @@ let config = clientFiles.map(f => ({
plugins: [ new EsmWebpackPlugin() ]
}))
config.push({
entry: "./conversejs/conversejs.js",
output: {
path: path.resolve(__dirname, "./dist/conversejs"),
filename: "./conversejs.js"
}
})
module.exports = config