2021-06-03 09:46:11 +00:00
|
|
|
import type { ChatType } from 'shared/lib/types'
|
2021-11-19 15:45:10 +00:00
|
|
|
import { videoHasWebchat } from 'shared/lib/video'
|
|
|
|
import { AutoColors, isAutoColorsAvailable, areAutoColorsValid } from 'shared/lib/autocolors'
|
2021-12-14 14:41:34 +00:00
|
|
|
import { logger } from './videowatch/logger'
|
|
|
|
import { closeSVG, openBlankChatSVG, openChatSVG, shareChatUrlSVG } from './videowatch/buttons'
|
|
|
|
import { displayButton, displayButtonOptions } from './videowatch/button'
|
|
|
|
import { shareChatUrl } from './videowatch/share'
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
interface VideoWatchLoadedHookOptions {
|
|
|
|
videojs: any
|
2021-06-02 10:20:15 +00:00
|
|
|
video: Video
|
|
|
|
playlist?: any
|
2021-05-07 16:52:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 14:41:34 +00:00
|
|
|
function register (registerOptions: RegisterOptions): void {
|
|
|
|
const { registerHook, peertubeHelpers } = registerOptions
|
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-06-02 13:18:09 +00:00
|
|
|
// NB: this will come with Peertube > 3.2.1 (3.3.0?)
|
|
|
|
if (peertubeHelpers.getBaseRouterRoute) {
|
|
|
|
return peertubeHelpers.getBaseRouterRoute()
|
|
|
|
}
|
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-08-04 15:38:26 +00:00
|
|
|
function getIframeUri (video: Video): 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-06-03 09:46:11 +00:00
|
|
|
const chatType: ChatType = (settings['chat-type'] ?? 'disabled') as ChatType
|
|
|
|
if (chatType === 'builtin-prosody' || chatType === 'builtin-converse') {
|
2021-02-20 17:31:21 +00:00
|
|
|
// Using the builtin converseJS
|
2021-08-04 15:38:26 +00:00
|
|
|
iframeUri = getBaseRoute() + '/webchat/room/' + encodeURIComponent(video.uuid)
|
2021-06-03 09:46:11 +00:00
|
|
|
} else if (chatType === 'external-uri') {
|
2021-04-14 16:47:23 +00:00
|
|
|
iframeUri = settings['chat-uri'] || ''
|
2021-08-04 15:38:26 +00:00
|
|
|
iframeUri = iframeUri.replace(/{{VIDEO_UUID}}/g, encodeURIComponent(video.uuid))
|
|
|
|
if (iframeUri.includes('{{CHANNEL_ID}}')) {
|
|
|
|
if (!video.channel || !video.channel.id) {
|
|
|
|
logger.error('Missing channel info in video object.')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
iframeUri = iframeUri.replace(/{{CHANNEL_ID}}/g, encodeURIComponent(video.channel.id))
|
|
|
|
}
|
2021-04-14 16:47:23 +00:00
|
|
|
if (!/^https?:\/\//.test(iframeUri)) {
|
|
|
|
logger.error('The webchaturi must begin with https://')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-03 09:46:11 +00:00
|
|
|
logger.error('Chat disabled.')
|
2021-04-14 16:47:23 +00:00
|
|
|
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
|
|
|
|
}
|
2021-11-19 15:45:10 +00:00
|
|
|
|
2021-11-24 17:21:35 +00:00
|
|
|
if (
|
|
|
|
settings['converse-autocolors'] &&
|
|
|
|
isAutoColorsAvailable(settings['chat-type'] as ChatType, settings['converse-theme'])
|
|
|
|
) {
|
2021-11-19 15:45:10 +00:00
|
|
|
logger.info('We have to try to compute autocolors.')
|
|
|
|
try {
|
|
|
|
const autocolors = computeAutoColors()
|
|
|
|
if (autocolors) {
|
|
|
|
const url = new URL(iframeUri, window.location.origin)
|
|
|
|
for (const p in autocolors) {
|
|
|
|
url.searchParams.set('_ac_' + p, autocolors[p as keyof AutoColors])
|
|
|
|
}
|
|
|
|
iframeUri = url.href
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(`Failed computing autocolors: '${err as string}'`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 17:31:21 +00:00
|
|
|
return iframeUri
|
|
|
|
}
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2021-11-19 15:45:10 +00:00
|
|
|
function computeAutoColors (): AutoColors | null {
|
|
|
|
if (!window.getComputedStyle) {
|
|
|
|
logger.warn('[AutoColors] getComputedStyle is not available, aborting.')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-11-22 12:30:55 +00:00
|
|
|
const styles = window.getComputedStyle(document.body)
|
2021-11-19 15:45:10 +00:00
|
|
|
|
2021-11-22 13:02:25 +00:00
|
|
|
// Peertube has no CSS variable for the button color...
|
|
|
|
// Computing by hand.
|
|
|
|
// Searching for one of these button:
|
|
|
|
const button = document.querySelector('.publish-button') ?? document.querySelector('.peertube-button-link')
|
|
|
|
if (!button) {
|
|
|
|
logger.warn('[AutoColors] Cant find a button, aborting.')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
const buttonStyles = window.getComputedStyle(button)
|
|
|
|
|
2021-11-19 15:45:10 +00:00
|
|
|
const autocolors: AutoColors = {
|
2021-11-22 13:02:25 +00:00
|
|
|
mainForeground: styles.getPropertyValue('--mainForegroundColor'),
|
2021-11-22 12:30:55 +00:00
|
|
|
mainBackground: styles.getPropertyValue('--mainBackgroundColor'),
|
|
|
|
greyForeground: styles.getPropertyValue('--greyForegroundColor'),
|
|
|
|
greyBackground: styles.getPropertyValue('--greyBackgroundColor'),
|
|
|
|
menuForeground: styles.getPropertyValue('--menuForegroundColor'),
|
|
|
|
menuBackground: styles.getPropertyValue('--menuBackgroundColor'),
|
|
|
|
inputForeground: styles.getPropertyValue('--inputForegroundColor'),
|
|
|
|
inputBackground: styles.getPropertyValue('--inputBackgroundColor'),
|
2021-11-22 13:02:25 +00:00
|
|
|
buttonForeground: buttonStyles.color,
|
|
|
|
buttonBackground: styles.getPropertyValue('--mainColor'),
|
2021-11-22 12:30:55 +00:00
|
|
|
link: styles.getPropertyValue('--mainForegroundColor'),
|
|
|
|
linkHover: styles.getPropertyValue('--mainForegroundColor')
|
2021-11-19 15:45:10 +00:00
|
|
|
}
|
|
|
|
const autoColorsTest = areAutoColorsValid(autocolors)
|
|
|
|
if (autoColorsTest !== true) {
|
|
|
|
logger.warn('[AutoColors] Computed colors are not valid, dropping. Invalid values: ' + autoColorsTest.join(', '))
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return autocolors
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:41:34 +00:00
|
|
|
async function insertChatDom (
|
|
|
|
container: HTMLElement, video: Video, showOpenBlank: boolean, showShareUrlButton: 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'),
|
2021-12-14 14:41:34 +00:00
|
|
|
peertubeHelpers.translate('Close chat'),
|
|
|
|
peertubeHelpers.translate('Share link')
|
2021-02-20 17:31:21 +00:00
|
|
|
]).then(labels => {
|
|
|
|
const labelOpen = labels[0]
|
|
|
|
const labelOpenBlank = labels[1]
|
|
|
|
const labelClose = labels[2]
|
2021-12-14 14:41:34 +00:00
|
|
|
const labelShareUrl = labels[3]
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-08-04 15:38:26 +00:00
|
|
|
const iframeUri = getIframeUri(video)
|
2021-02-20 17:31:21 +00:00
|
|
|
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-12-14 14:41:34 +00:00
|
|
|
// Here are buttons that are magically merged
|
|
|
|
const groupButtons: displayButtonOptions[] = []
|
|
|
|
groupButtons.push({
|
|
|
|
buttonContainer,
|
|
|
|
name: 'open',
|
|
|
|
label: labelOpen,
|
|
|
|
callback: () => openChat(video),
|
|
|
|
icon: openChatSVG,
|
|
|
|
additionalClasses: []
|
|
|
|
})
|
2021-02-20 23:13:29 +00:00
|
|
|
if (showOpenBlank) {
|
2021-12-14 14:41:34 +00:00
|
|
|
groupButtons.push({
|
|
|
|
buttonContainer,
|
|
|
|
name: 'openblank',
|
|
|
|
label: labelOpenBlank,
|
|
|
|
callback: () => {
|
2021-11-25 15:56:32 +00:00
|
|
|
closeChat()
|
|
|
|
window.open(iframeUri)
|
|
|
|
},
|
2021-12-14 14:41:34 +00:00
|
|
|
icon: openBlankChatSVG,
|
|
|
|
additionalClasses: []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (showShareUrlButton) {
|
|
|
|
groupButtons.push({
|
|
|
|
buttonContainer,
|
|
|
|
name: 'shareurl',
|
|
|
|
label: labelShareUrl,
|
|
|
|
callback: () => {
|
|
|
|
shareChatUrl(registerOptions)
|
|
|
|
},
|
|
|
|
icon: shareChatUrlSVG,
|
|
|
|
additionalClasses: []
|
|
|
|
})
|
2021-02-20 23:13:29 +00:00
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-12-14 14:41:34 +00:00
|
|
|
// If more than one groupButtons:
|
|
|
|
// - the first must have class 'peertube-plugin-livechat-multi-button-main'
|
|
|
|
// - middle ones must have 'peertube-plugin-livechat-multi-button-secondary'
|
|
|
|
// - the last must have 'peertube-plugin-livechat-multi-button-last-secondary'
|
|
|
|
if (groupButtons.length > 1) {
|
|
|
|
groupButtons[0].additionalClasses?.push('peertube-plugin-livechat-multi-button-main')
|
|
|
|
for (let i = 1; i < groupButtons.length - 1; i++) { // middle
|
|
|
|
groupButtons[i].additionalClasses?.push('peertube-plugin-livechat-multi-button-secondary')
|
|
|
|
}
|
|
|
|
groupButtons[groupButtons.length - 1]
|
|
|
|
.additionalClasses?.push('peertube-plugin-livechat-multi-button-last-secondary')
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const button of groupButtons) {
|
|
|
|
displayButton(button)
|
|
|
|
}
|
|
|
|
|
|
|
|
displayButton({
|
|
|
|
buttonContainer,
|
|
|
|
name: 'close',
|
|
|
|
label: labelClose,
|
|
|
|
callback: () => closeChat(),
|
|
|
|
icon: closeSVG
|
|
|
|
})
|
2021-02-20 17:31:21 +00:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:38:26 +00:00
|
|
|
function openChat (video: Video): void | boolean {
|
|
|
|
if (!video) {
|
|
|
|
logger.log('No video.')
|
2021-04-07 16:14:58 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2021-08-04 15:38:26 +00:00
|
|
|
logger.info('Trying to load the chat for video ' + video.uuid + '.')
|
|
|
|
const iframeUri = getIframeUri(video)
|
2021-04-07 16:14:58 +00:00
|
|
|
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-11-17 13:41:25 +00:00
|
|
|
|
|
|
|
// Hacking styles...
|
|
|
|
hackStyles(true)
|
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-11-17 13:41:25 +00:00
|
|
|
|
|
|
|
// Un-Hacking styles...
|
|
|
|
hackStyles(false)
|
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-06-02 10:20:15 +00:00
|
|
|
const placeholder = document.getElementById('plugin-placeholder-player-next')
|
2021-05-18 18:35:19 +00:00
|
|
|
if (!placeholder) {
|
|
|
|
logger.error('The required placeholder div is not present in the DOM.')
|
2021-02-20 14:41:00 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-18 18:35:19 +00:00
|
|
|
|
|
|
|
let container = placeholder.querySelector('#peertube-plugin-livechat-container')
|
2021-03-01 17:38:39 +00:00
|
|
|
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')
|
2021-12-08 18:33:28 +00:00
|
|
|
container.setAttribute('peertube-plugin-livechat-current-url', window.location.href)
|
2021-05-18 18:35:19 +00:00
|
|
|
placeholder.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-12-14 14:41:34 +00:00
|
|
|
let showShareUrlButton: boolean = false
|
|
|
|
if (settings['chat-type'] === 'builtin-prosody') {
|
|
|
|
// FIXME: showShareUrlButton should only be true for video owner and instance moderators.
|
|
|
|
showShareUrlButton = true
|
|
|
|
}
|
|
|
|
insertChatDom(container as HTMLElement, video, !!settings['chat-open-blank'], showShareUrlButton).then(() => {
|
2021-02-20 17:31:21 +00:00
|
|
|
if (settings['chat-auto-display']) {
|
2021-08-04 15:38:26 +00:00
|
|
|
openChat(video)
|
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-11-17 13:41:25 +00:00
|
|
|
let savedMyPluginFlexGrow: string | undefined
|
|
|
|
function hackStyles (on: boolean): void {
|
|
|
|
try {
|
2021-11-18 14:35:13 +00:00
|
|
|
document.querySelectorAll('.peertube-plugin-livechat-buttons').forEach(buttons => {
|
|
|
|
if (on) {
|
|
|
|
buttons.classList.add('peertube-plugin-livechat-buttons-open')
|
|
|
|
} else {
|
|
|
|
buttons.classList.remove('peertube-plugin-livechat-buttons-open')
|
|
|
|
}
|
|
|
|
})
|
2021-11-17 13:41:25 +00:00
|
|
|
const myPluginPlaceholder: HTMLElement | null = document.querySelector('my-plugin-placeholder')
|
|
|
|
if (on) {
|
|
|
|
// Saving current style attributes and maximazing space for the chat
|
|
|
|
if (myPluginPlaceholder) {
|
|
|
|
savedMyPluginFlexGrow = myPluginPlaceholder.style.flexGrow // Should be "", but can be anything else.
|
|
|
|
myPluginPlaceholder.style.flexGrow = '1'
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// restoring values...
|
|
|
|
if (savedMyPluginFlexGrow !== undefined && myPluginPlaceholder) {
|
|
|
|
myPluginPlaceholder.style.flexGrow = savedMyPluginFlexGrow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(`Failed hacking styles: '${err as string}'`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:52:01 +00:00
|
|
|
registerHook({
|
|
|
|
target: 'action:video-watch.video.loaded',
|
|
|
|
handler: ({
|
|
|
|
video,
|
|
|
|
playlist
|
|
|
|
}: VideoWatchLoadedHookOptions) => {
|
|
|
|
if (!video) {
|
2021-06-02 10:20:15 +00:00
|
|
|
logger.error('No video argument in hook action:video-watch.video.loaded')
|
|
|
|
return
|
2021-05-07 16:52:01 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|