2021-04-09 17:29:44 +00:00
|
|
|
import type { Router, Request, Response, NextFunction } from 'express'
|
2021-04-14 16:47:23 +00:00
|
|
|
import { getBaseRouter } from '../helpers'
|
2021-04-08 00:43:13 +00:00
|
|
|
import * as path from 'path'
|
|
|
|
const fs = require('fs').promises
|
|
|
|
|
2021-04-09 17:29:44 +00:00
|
|
|
async function initWebchatRouter ({
|
2021-04-08 00:43:13 +00:00
|
|
|
getRouter,
|
2021-04-09 17:29:44 +00:00
|
|
|
peertubeHelpers,
|
|
|
|
settingsManager
|
2021-04-09 17:39:40 +00:00
|
|
|
}: RegisterServerOptions): Promise<Router> {
|
2021-04-09 17:29:44 +00:00
|
|
|
const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../../conversejs/index.html'))
|
2021-04-08 00:43:13 +00:00
|
|
|
|
|
|
|
const router = getRouter()
|
2021-04-09 17:29:44 +00:00
|
|
|
router.get('/', async (req: Request, res: Response, next: NextFunction) => {
|
2021-04-08 00:43:13 +00:00
|
|
|
try {
|
|
|
|
const settings = await settingsManager.getSettings([
|
2021-04-14 16:47:23 +00:00
|
|
|
'chat-use-prosody', 'chat-use-builtin', 'chat-room', 'chat-server',
|
2021-04-08 00:43:13 +00:00
|
|
|
'chat-bosh-uri', 'chat-ws-uri'
|
|
|
|
])
|
|
|
|
|
2021-04-14 16:47:23 +00:00
|
|
|
let server: string
|
|
|
|
let room: string
|
|
|
|
let boshUri: string
|
|
|
|
let wsUri: string
|
|
|
|
if (settings['chat-use-prosody']) {
|
|
|
|
server = 'localhost'
|
|
|
|
room = '{{VIDEO_UUID}}@room.localhost'
|
|
|
|
boshUri = getBaseRouter() + 'http-bind'
|
|
|
|
wsUri = ''
|
|
|
|
} else if (settings['chat-use-builtin']) {
|
|
|
|
if (!settings['chat-server']) {
|
|
|
|
throw new Error('Missing chat-server settings.')
|
|
|
|
}
|
|
|
|
if (!settings['chat-room']) {
|
|
|
|
throw new Error('Missing chat-room settings.')
|
|
|
|
}
|
|
|
|
if (!settings['chat-bosh-uri'] && !settings['chat-ws-uri']) {
|
|
|
|
throw new Error('Missing BOSH or Websocket uri.')
|
|
|
|
}
|
|
|
|
server = settings['chat-server'] as string
|
|
|
|
room = settings['chat-room'] as string
|
|
|
|
boshUri = settings['chat-bosh-uri'] as string
|
|
|
|
wsUri = settings['chat-ws-uri'] as string
|
|
|
|
} else {
|
2021-04-08 00:43:13 +00:00
|
|
|
throw new Error('Builtin chat disabled.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: with Peertube 3.0.1 the following method is not available...
|
|
|
|
// When loadByIdOrUUID is available, change the entry point to
|
|
|
|
// be /webchat/:videoId
|
|
|
|
// const id = req.param('videoId')
|
|
|
|
// const video = await peertubeHelpers.videos.loadByIdOrUUID(id)
|
|
|
|
let url: string = req.query.url as string || ''
|
|
|
|
if (!url) {
|
|
|
|
throw new Error('Missing url parameter)')
|
|
|
|
}
|
|
|
|
let video = await peertubeHelpers.videos.loadByUrl(url)
|
|
|
|
if (!video) {
|
|
|
|
// FIXME: remove this when loadByIdOrUUID will be available...
|
|
|
|
// This is a dirty Hack for dev environnements...
|
|
|
|
url = url.replace(/^https:/, 'http:')
|
|
|
|
video = await peertubeHelpers.videos.loadByUrl(url)
|
|
|
|
}
|
|
|
|
if (!video) {
|
|
|
|
throw new Error('Video not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
let page = '' + (converseJSIndex as string)
|
|
|
|
// FIXME: Peertube should provide the static folder path. For now:
|
|
|
|
const staticRelative = '../static'
|
|
|
|
page = page.replace(/{{BASE_STATIC_URL}}/g, staticRelative)
|
2021-04-14 16:47:23 +00:00
|
|
|
page = page.replace(/{{JID}}/g, server)
|
|
|
|
room = room.replace(/{{VIDEO_UUID}}/g, video.uuid)
|
2021-04-08 00:43:13 +00:00
|
|
|
page = page.replace(/{{ROOM}}/g, room)
|
2021-04-14 16:47:23 +00:00
|
|
|
page = page.replace(/{{BOSH_SERVICE_URL}}/g, boshUri)
|
|
|
|
page = page.replace(/{{WS_SERVICE_URL}}/g, wsUri)
|
2021-04-08 00:43:13 +00:00
|
|
|
|
|
|
|
res.status(200)
|
|
|
|
res.type('html')
|
|
|
|
res.send(page)
|
|
|
|
} catch (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
})
|
2021-04-09 17:29:44 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
initWebchatRouter
|
2021-04-08 00:43:13 +00:00
|
|
|
}
|