2021-02-20 19:42:41 +00:00
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs').promises
|
|
|
|
|
2019-07-26 12:56:58 +00:00
|
|
|
async function register ({
|
2021-02-20 10:55:00 +00:00
|
|
|
_registerHook,
|
2019-07-26 12:56:58 +00:00
|
|
|
registerSetting,
|
2021-02-20 19:42:41 +00:00
|
|
|
settingsManager,
|
2021-02-20 10:55:00 +00:00
|
|
|
_storageManager,
|
|
|
|
_videoCategoryManager,
|
|
|
|
_videoLicenceManager,
|
2021-02-20 17:31:21 +00:00
|
|
|
_videoLanguageManager,
|
|
|
|
getRouter,
|
|
|
|
peertubeHelpers
|
2019-07-26 12:56:58 +00:00
|
|
|
}) {
|
2021-02-19 17:21:40 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'chat-auto-display',
|
|
|
|
label: 'Automatically open the chat',
|
|
|
|
type: 'input-checkbox',
|
|
|
|
default: false,
|
|
|
|
private: false
|
|
|
|
})
|
2021-02-20 23:13:29 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'chat-open-blank',
|
|
|
|
label: 'Show the «open in new window» button',
|
|
|
|
private: false,
|
|
|
|
type: 'input-checkbox',
|
|
|
|
default: false
|
|
|
|
})
|
2021-02-20 22:37:23 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'chat-only-locals',
|
|
|
|
label: 'Chats are only available for local videos.',
|
|
|
|
type: 'input-checkbox',
|
2021-03-08 09:22:41 +00:00
|
|
|
default: false, // TODO: set to true when peertube has fixed https://github.com/Chocobozzz/PeerTube/issues/3838
|
2021-02-20 22:37:23 +00:00
|
|
|
private: false
|
|
|
|
})
|
2021-02-18 17:31:12 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'chat-all-lives',
|
|
|
|
label: 'Activate chat for all lives',
|
|
|
|
type: 'input-checkbox',
|
|
|
|
default: false,
|
2021-02-19 09:11:42 +00:00
|
|
|
descriptionHTML: 'If checked, a chat will be added to all lives.',
|
|
|
|
private: false
|
2021-02-18 17:31:12 +00:00
|
|
|
})
|
|
|
|
registerSetting({
|
|
|
|
name: 'chat-all-non-lives',
|
|
|
|
label: 'Activate chat for all non-lives',
|
|
|
|
type: 'input-checkbox',
|
|
|
|
default: false,
|
2021-02-19 09:11:42 +00:00
|
|
|
descriptionHTML: 'If checked, a chat will be added to all video that are not lives.',
|
|
|
|
private: false
|
2021-02-18 17:31:12 +00:00
|
|
|
})
|
|
|
|
registerSetting({
|
|
|
|
name: 'chat-videos-list',
|
|
|
|
label: 'Activate chat for specific videos',
|
|
|
|
type: 'input-textarea',
|
|
|
|
default: '',
|
2021-02-19 09:11:42 +00:00
|
|
|
descriptionHTML: 'Videos UUIDs for which we want a chat. ' +
|
2021-02-20 10:55:00 +00:00
|
|
|
'Can be non-live videos. One per line. <br />' +
|
2021-02-19 14:19:20 +00:00
|
|
|
'You can add comments: everything after the # character will be stripped off, and empty lines ignored.<br />' +
|
2021-02-19 09:11:42 +00:00
|
|
|
'Don\'t add private videos, the UUIDs will be send to frontend.',
|
|
|
|
private: false
|
2021-02-18 17:31:12 +00:00
|
|
|
})
|
2021-02-20 17:31:21 +00:00
|
|
|
|
|
|
|
registerSetting({
|
|
|
|
name: 'chat-use-builtin',
|
|
|
|
label: 'Use builtin ConverseJS',
|
|
|
|
type: 'input-checkbox',
|
2021-03-08 09:22:41 +00:00
|
|
|
default: false, // TODO: set to true when peertube has fixed https://github.com/Chocobozzz/PeerTube/issues/3838
|
2021-02-20 17:31:21 +00:00
|
|
|
private: false,
|
|
|
|
descriptionHTML: 'If checked, use a builtin ConverseJS iframe.<br>' +
|
2021-03-12 16:34:57 +00:00
|
|
|
'You still have to configure an external XMPP service. Please see the ' +
|
|
|
|
'<a href="https://github.com/JohnXLivingston/peertube-plugin-livechat" target="_blank">documentation<a>.<br>' +
|
|
|
|
'If you have no running webchat service, you can follow this ' +
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
'<a href="https://github.com/JohnXLivingston/peertube-plugin-livechat/blob/main/documentation/tutorials/prosody.md" target="blank_">tutorial</a>.'
|
2021-02-20 17:31:21 +00:00
|
|
|
})
|
2021-02-20 19:42:41 +00:00
|
|
|
registerSetting({
|
2021-02-20 22:12:12 +00:00
|
|
|
name: 'chat-server',
|
|
|
|
label: 'Builtin webchat: XMPP service server',
|
2021-02-20 19:42:41 +00:00
|
|
|
type: 'input',
|
|
|
|
default: '',
|
|
|
|
descriptionHTML: 'When using the built-in converseJS webchat:<br>' +
|
2021-02-20 22:12:12 +00:00
|
|
|
'Your XMPP server. Without any scheme. Example : peertube.im.your_domain.',
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
registerSetting({
|
|
|
|
name: 'chat-room',
|
|
|
|
label: 'Builtin webchat: XMPP room template',
|
|
|
|
type: 'input',
|
|
|
|
default: '',
|
|
|
|
descriptionHTML: 'When using the built-in converseJS webchat:<br>' +
|
|
|
|
'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>' +
|
|
|
|
'Example: public@room.peertube.im.your_domain<br>' +
|
|
|
|
'Example: public_{{VIDEO_UUID}}@room.peertube.im.your_domain',
|
2021-02-20 19:42:41 +00:00
|
|
|
private: true
|
|
|
|
})
|
2021-02-20 17:31:21 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'chat-bosh-uri',
|
|
|
|
label: 'Builtin webchat: BOSH uri',
|
|
|
|
type: 'input',
|
2021-02-20 19:42:41 +00:00
|
|
|
default: '',
|
2021-02-20 17:31:21 +00:00
|
|
|
descriptionHTML: 'When using the built-in converseJS webchat:<br>' +
|
2021-02-20 22:12:12 +00:00
|
|
|
'URI of the external BOSH server. Please make sure it accept cross origin request from your domain.<br>' +
|
|
|
|
'You must at least have a BOSH or a Websocket uri.',
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
registerSetting({
|
|
|
|
name: 'chat-ws-uri',
|
|
|
|
label: 'Builtin webchat: WS uri',
|
|
|
|
type: 'input',
|
|
|
|
default: '',
|
|
|
|
descriptionHTML: 'When using the built-in converseJS webchat:<br>' +
|
|
|
|
'URI of the external WS server. Please make sure it accept cross origin request from your domain.<br>' +
|
|
|
|
'You must at least have a BOSH or a Websocket uri.',
|
2021-02-20 17:31:21 +00:00
|
|
|
private: true
|
|
|
|
})
|
|
|
|
|
2021-02-18 17:31:12 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'chat-uri',
|
|
|
|
label: 'Webchat url',
|
|
|
|
type: 'input',
|
|
|
|
default: '',
|
2021-02-20 17:31:21 +00:00
|
|
|
descriptionHTML: '<b>If you dont want to use the builtin ConverseJS webchat:</b><br>' +
|
|
|
|
'Put here your webchat url. An iframe will be created pointing to this url. ' +
|
2021-02-19 09:11:42 +00:00
|
|
|
'The placeholder {{VIDEO_UUID}} will be replace by the video UUID if present. ' +
|
2021-02-20 17:31:21 +00:00
|
|
|
'Example : https://my_domain/conversejs.html?room=video_{{VIDEO_UUID}}.<br>' +
|
|
|
|
'If this field is empty, it will use the builtin ConverseJS webchat.',
|
2021-02-19 09:11:42 +00:00
|
|
|
private: false
|
2021-02-18 17:31:12 +00:00
|
|
|
})
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-02-19 17:21:40 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'chat-style',
|
|
|
|
label: 'Webchat iframe style attribute',
|
|
|
|
type: 'input-textarea',
|
|
|
|
default: '',
|
|
|
|
descriptionHTML: 'Additional styles to be added on the iframe style attribute. <br>' +
|
|
|
|
'Example: height:400px;',
|
|
|
|
private: false
|
|
|
|
})
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-02-20 19:42:41 +00:00
|
|
|
const converseJSIndex = await fs.readFile(path.resolve(__dirname, './conversejs/index.html'))
|
|
|
|
|
2021-02-20 17:31:21 +00:00
|
|
|
const router = getRouter()
|
|
|
|
router.get('/ping', (req, res) => res.json({ message: 'pong' }))
|
|
|
|
router.get('/webchat', async (req, res, next) => {
|
|
|
|
try {
|
2021-02-20 19:42:41 +00:00
|
|
|
const settings = await settingsManager.getSettings([
|
2021-02-20 22:12:12 +00:00
|
|
|
'chat-use-builtin', 'chat-room', 'chat-server',
|
|
|
|
'chat-bosh-uri', 'chat-ws-uri'
|
2021-02-20 19:42:41 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
if (!settings['chat-use-builtin']) {
|
|
|
|
throw new Error('Builtin chat disabled.')
|
|
|
|
}
|
2021-02-20 22:12:12 +00:00
|
|
|
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.')
|
2021-02-20 19:42:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 17:31:21 +00:00
|
|
|
// 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 = req.query.url
|
|
|
|
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')
|
|
|
|
}
|
2021-02-20 19:42:41 +00:00
|
|
|
|
|
|
|
let page = '' + converseJSIndex
|
|
|
|
// FIXME: Peertube should provide the static folder path. For now:
|
2021-02-20 22:12:12 +00:00
|
|
|
const staticRelative = '../static'
|
2021-02-20 19:42:41 +00:00
|
|
|
page = page.replace(/{{BASE_STATIC_URL}}/g, staticRelative)
|
2021-02-20 22:12:12 +00:00
|
|
|
page = page.replace(/{{JID}}/g, settings['chat-server'])
|
|
|
|
const room = settings['chat-room'].replace(/{{VIDEO_UUID}}/g, video.uuid)
|
|
|
|
page = page.replace(/{{ROOM}}/g, room)
|
2021-02-20 19:42:41 +00:00
|
|
|
page = page.replace(/{{BOSH_SERVICE_URL}}/g, settings['chat-bosh-uri'])
|
2021-02-20 22:12:12 +00:00
|
|
|
page = page.replace(/{{WS_SERVICE_URL}}/g, settings['chat-ws-uri'])
|
2021-02-20 19:42:41 +00:00
|
|
|
|
|
|
|
res.status(200)
|
2021-02-21 14:21:30 +00:00
|
|
|
res.type('html')
|
2021-02-20 19:42:41 +00:00
|
|
|
res.send(page)
|
2021-02-20 17:31:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
})
|
2019-07-16 14:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function unregister () {
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
register,
|
|
|
|
unregister
|
|
|
|
}
|