2021-05-01 17:06:05 +00:00
|
|
|
import { videoHasWebchat } from 'shared/lib/video'
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
interface VideoCache {[key: string]: Video}
|
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
interface VideoWatchLoadedHookOptions {
|
|
|
|
videojs: any
|
|
|
|
video?: Video // comes with Peertube 3.2.0
|
|
|
|
playlist?: any // comes with Peertube 3.2.0
|
|
|
|
}
|
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
|
2021-02-20 17:31:21 +00:00
|
|
|
const logger = {
|
2021-04-07 16:14:58 +00:00
|
|
|
log: (s: string) => console.log('[peertube-plugin-livechat] ' + s),
|
|
|
|
info: (s: string) => console.info('[peertube-plugin-livechat] ' + s),
|
|
|
|
error: (s: string) => console.error('[peertube-plugin-livechat] ' + s),
|
|
|
|
warn: (s: string) => console.warn('[peertube-plugin-livechat] ' + s)
|
2021-02-19 17:21:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
// This is for backward compatibility with Peertube < 3.2.0.
|
2021-04-07 16:14:58 +00:00
|
|
|
const videoCache: VideoCache = {}
|
|
|
|
let lastUUID: string | null = null
|
2021-05-07 16:52:01 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
let settings: any = {}
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
function getBaseRoute (): string {
|
2021-02-20 17:31:21 +00:00
|
|
|
// FIXME: should be provided by PeertubeHelpers (does not exists for now)
|
2021-02-20 19:42:41 +00:00
|
|
|
// 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')
|
2021-02-20 17:31:21 +00:00
|
|
|
}
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
function getIframeUri (uuid: string): string | null {
|
2021-02-20 17:31:21 +00:00
|
|
|
if (!settings) {
|
|
|
|
logger.error('Settings are not initialized, too soon to compute the iframeUri')
|
|
|
|
return null
|
2021-02-19 17:21:40 +00:00
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
let iframeUri = ''
|
2021-04-14 16:47:23 +00:00
|
|
|
if (settings['chat-use-prosody'] || settings['chat-use-builtin']) {
|
2021-02-20 17:31:21 +00:00
|
|
|
// Using the builtin converseJS
|
2021-04-30 15:39:27 +00:00
|
|
|
iframeUri = getBaseRoute() + '/webchat/room/' + encodeURIComponent(uuid)
|
2021-04-14 16:47:23 +00:00
|
|
|
} else if (!settings['chat-use-builtin']) {
|
|
|
|
iframeUri = settings['chat-uri'] || ''
|
2021-04-30 15:39:27 +00:00
|
|
|
iframeUri = iframeUri.replace(/{{VIDEO_UUID}}/g, encodeURIComponent(uuid))
|
2021-04-14 16:47:23 +00:00
|
|
|
if (!/^https?:\/\//.test(iframeUri)) {
|
|
|
|
logger.error('The webchaturi must begin with https://')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.error('Dont known which url use for the iframe.')
|
|
|
|
return null
|
2021-02-19 17:21:40 +00:00
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
if (iframeUri === '') {
|
|
|
|
logger.error('No iframe uri')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return iframeUri
|
|
|
|
}
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
function displayButton (
|
|
|
|
buttonContainer: HTMLElement,
|
|
|
|
name: string,
|
|
|
|
label: string,
|
|
|
|
callback: () => void | boolean,
|
|
|
|
icon: string | null
|
|
|
|
): void {
|
2021-02-20 17:31:21 +00:00
|
|
|
const button = document.createElement('button')
|
|
|
|
button.classList.add(
|
2021-03-01 17:38:39 +00:00
|
|
|
'peertube-plugin-livechat-button',
|
2021-02-20 17:31:21 +00:00
|
|
|
'peertube-plugin-livechat-button-' + name
|
2021-02-20 14:41:00 +00:00
|
|
|
)
|
2021-02-20 17:31:21 +00:00
|
|
|
button.onclick = callback
|
2021-03-01 17:38:39 +00:00
|
|
|
if (icon) {
|
2021-04-07 16:14:58 +00:00
|
|
|
// FIXME: remove «as string» when peertube types will be available
|
2021-04-09 11:08:20 +00:00
|
|
|
const iconUrl = peertubeHelpers.getBaseStaticRoute() + '/images/' + icon
|
2021-03-01 17:38:39 +00:00
|
|
|
const iconEl = document.createElement('span')
|
|
|
|
iconEl.classList.add('peertube-plugin-livechat-button-icon')
|
|
|
|
iconEl.setAttribute('style',
|
|
|
|
'background-image: url(\'' + iconUrl + '\');'
|
|
|
|
)
|
|
|
|
button.prepend(iconEl)
|
|
|
|
button.setAttribute('title', label)
|
|
|
|
} else {
|
|
|
|
button.textContent = label
|
|
|
|
}
|
|
|
|
buttonContainer.append(button)
|
2021-02-20 17:31:21 +00:00
|
|
|
}
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
async function insertChatDom (container: HTMLElement, uuid: string, showOpenBlank: boolean): Promise<void> {
|
2021-03-01 17:38:39 +00:00
|
|
|
logger.log('Adding livechat in the DOM...')
|
2021-04-07 16:14:58 +00:00
|
|
|
const p = new Promise<void>((resolve, reject) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2021-02-20 17:31:21 +00:00
|
|
|
Promise.all([
|
|
|
|
peertubeHelpers.translate('Open chat'),
|
|
|
|
peertubeHelpers.translate('Open chat in a new window'),
|
|
|
|
peertubeHelpers.translate('Close chat')
|
|
|
|
]).then(labels => {
|
|
|
|
const labelOpen = labels[0]
|
|
|
|
const labelOpenBlank = labels[1]
|
|
|
|
const labelClose = labels[2]
|
|
|
|
|
|
|
|
const iframeUri = getIframeUri(uuid)
|
|
|
|
if (!iframeUri) {
|
|
|
|
return reject(new Error('No uri, cant display the buttons.'))
|
|
|
|
}
|
2021-03-01 17:38:39 +00:00
|
|
|
|
|
|
|
const buttonContainer = document.createElement('div')
|
|
|
|
buttonContainer.classList.add('peertube-plugin-livechat-buttons')
|
|
|
|
container.append(buttonContainer)
|
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
displayButton(buttonContainer, 'open', labelOpen, () => openChat(uuid), 'talking.svg')
|
2021-02-20 23:13:29 +00:00
|
|
|
if (showOpenBlank) {
|
2021-03-01 17:38:39 +00:00
|
|
|
displayButton(buttonContainer, 'openblank', labelOpenBlank, () => {
|
2021-02-20 23:13:29 +00:00
|
|
|
closeChat()
|
|
|
|
window.open(iframeUri)
|
2021-03-08 10:33:09 +00:00
|
|
|
}, 'talking-new-window.svg')
|
2021-02-20 23:13:29 +00:00
|
|
|
}
|
2021-03-08 10:33:09 +00:00
|
|
|
displayButton(buttonContainer, 'close', labelClose, () => closeChat(), 'bye.svg')
|
2021-02-20 17:31:21 +00:00
|
|
|
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
function openChat (uuid: string): void | boolean {
|
2021-04-07 16:14:58 +00:00
|
|
|
if (!uuid) {
|
|
|
|
logger.log('No current uuid.')
|
|
|
|
return false
|
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
logger.info('Trying to load the chat for video ' + uuid + '.')
|
|
|
|
const iframeUri = getIframeUri(uuid)
|
|
|
|
if (!iframeUri) {
|
|
|
|
logger.error('Incorrect iframe uri')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const additionalStyles = settings['chat-style'] || ''
|
2021-03-01 17:38:39 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
logger.info('Opening the chat...')
|
|
|
|
const container = document.getElementById('peertube-plugin-livechat-container')
|
|
|
|
if (!container) {
|
|
|
|
logger.error('Cant found the livechat container.')
|
|
|
|
return false
|
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
if (container.querySelector('iframe')) {
|
|
|
|
logger.error('Seems that there is already an iframe in the container.')
|
|
|
|
return false
|
|
|
|
}
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
// Creating the iframe...
|
|
|
|
const iframe = document.createElement('iframe')
|
|
|
|
iframe.setAttribute('src', iframeUri)
|
|
|
|
iframe.setAttribute('sandbox', 'allow-same-origin allow-scripts allow-popups allow-forms')
|
|
|
|
iframe.setAttribute('frameborder', '0')
|
|
|
|
if (additionalStyles) {
|
|
|
|
iframe.setAttribute('style', additionalStyles)
|
|
|
|
}
|
|
|
|
container.append(iframe)
|
|
|
|
container.setAttribute('peertube-plugin-livechat-state', 'open')
|
2021-02-20 15:03:44 +00:00
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
function closeChat (): void {
|
2021-03-01 17:38:39 +00:00
|
|
|
const container = document.getElementById('peertube-plugin-livechat-container')
|
|
|
|
if (!container) {
|
|
|
|
logger.error('Cant close livechat, container not found.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
container.querySelectorAll('iframe')
|
2021-02-20 17:31:21 +00:00
|
|
|
.forEach(dom => dom.remove())
|
|
|
|
|
2021-03-01 17:38:39 +00:00
|
|
|
container.setAttribute('peertube-plugin-livechat-state', 'closed')
|
2021-02-20 14:41:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
function initChat (video: Video): void {
|
|
|
|
if (!video) {
|
|
|
|
logger.error('No video provided')
|
|
|
|
return
|
|
|
|
}
|
2021-03-01 17:38:39 +00:00
|
|
|
const videoWrapper = document.querySelector('#video-wrapper')
|
|
|
|
if (!videoWrapper) {
|
2021-02-20 17:31:21 +00:00
|
|
|
logger.error('The required div is not present in the DOM.')
|
2021-02-20 14:41:00 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-01 17:38:39 +00:00
|
|
|
let container = videoWrapper.querySelector('#peertube-plugin-livechat-container')
|
|
|
|
if (container) {
|
2021-02-20 17:31:21 +00:00
|
|
|
logger.log('The chat seems already initialized...')
|
2021-02-20 14:41:00 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-01 17:38:39 +00:00
|
|
|
container = document.createElement('div')
|
|
|
|
container.setAttribute('id', 'peertube-plugin-livechat-container')
|
|
|
|
container.setAttribute('peertube-plugin-livechat-state', 'initializing')
|
|
|
|
videoWrapper.append(container)
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
peertubeHelpers.getSettings().then((s: any) => {
|
2021-02-20 17:31:21 +00:00
|
|
|
settings = s
|
2021-02-20 14:41:00 +00:00
|
|
|
|
2021-02-20 17:31:21 +00:00
|
|
|
logger.log('Checking if this video should have a chat...')
|
2021-05-01 17:06:05 +00:00
|
|
|
if (!videoHasWebchat(s, video)) {
|
|
|
|
logger.log('This video has no webchat')
|
2021-02-20 17:31:21 +00:00
|
|
|
return
|
2021-02-20 14:41:00 +00:00
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
insertChatDom(container as HTMLElement, video.uuid, !!settings['chat-open-blank']).then(() => {
|
2021-02-20 17:31:21 +00:00
|
|
|
if (settings['chat-auto-display']) {
|
2021-05-07 16:52:01 +00:00
|
|
|
openChat(video.uuid)
|
2021-04-07 16:14:58 +00:00
|
|
|
} else if (container) {
|
2021-03-01 17:38:39 +00:00
|
|
|
container.setAttribute('peertube-plugin-livechat-state', 'closed')
|
2021-02-20 17:31:21 +00:00
|
|
|
}
|
2021-05-01 17:06:05 +00:00
|
|
|
}, () => {
|
|
|
|
logger.error('insertChatDom has failed')
|
2021-02-20 17:31:21 +00:00
|
|
|
})
|
2021-05-01 16:30:21 +00:00
|
|
|
}, () => {
|
|
|
|
logger.error('Cant get settings')
|
2021-02-20 14:41:00 +00:00
|
|
|
})
|
2021-02-20 17:31:21 +00:00
|
|
|
}
|
2021-02-20 14:41:00 +00:00
|
|
|
|
2021-02-18 17:31:12 +00:00
|
|
|
registerHook({
|
2021-02-19 17:21:40 +00:00
|
|
|
target: 'filter:api.video-watch.video.get.result',
|
2021-04-07 16:14:58 +00:00
|
|
|
handler: (video: Video) => {
|
2021-05-07 16:52:01 +00:00
|
|
|
// For Peertube < 3.2.0, hooks for action:video-watch... did not receive the video object
|
2021-02-19 17:21:40 +00:00
|
|
|
// So we store video objects in videoCache
|
|
|
|
videoCache[video.uuid] = video
|
|
|
|
lastUUID = video.uuid
|
|
|
|
return video
|
|
|
|
}
|
|
|
|
})
|
2021-05-07 16:52:01 +00:00
|
|
|
registerHook({
|
|
|
|
target: 'action:video-watch.video.loaded',
|
|
|
|
handler: ({
|
|
|
|
video,
|
|
|
|
playlist
|
|
|
|
}: VideoWatchLoadedHookOptions) => {
|
|
|
|
if (!video) {
|
|
|
|
logger.info('It seems we are using Peertube < 3.2.0. Using a cache to get the video object')
|
|
|
|
const uuid = lastUUID
|
|
|
|
if (!uuid) {
|
|
|
|
logger.error('There is no lastUUID.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
video = videoCache[uuid]
|
|
|
|
if (!video) {
|
|
|
|
logger.error('Can\'t find the video ' + uuid + ' in the videoCache')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (playlist) {
|
|
|
|
logger.info('We are in a playlist, we will not use the webchat')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
initChat(video)
|
|
|
|
}
|
|
|
|
})
|
2021-02-18 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
register
|
|
|
|
}
|