2022-01-11 00:29:33 +00:00
|
|
|
import type { Video } from '@peertube/peertube-types'
|
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
2023-04-21 14:56:48 +00:00
|
|
|
import { videoHasWebchat, videoHasRemoteWebchat } from 'shared/lib/video'
|
2023-09-06 15:58:54 +00:00
|
|
|
import { localizedHelpUrl } from './utils/help'
|
2024-01-08 10:56:44 +00:00
|
|
|
import { logger } from './utils/logger'
|
2023-07-26 16:16:30 +00:00
|
|
|
import { closeSVG, openBlankChatSVG, openChatSVG, shareChatUrlSVG, helpButtonSVG } from './videowatch/buttons'
|
2021-12-14 14:41:34 +00:00
|
|
|
import { displayButton, displayButtonOptions } from './videowatch/button'
|
|
|
|
import { shareChatUrl } from './videowatch/share'
|
2024-03-28 14:06:15 +00:00
|
|
|
import { displayConverseJS } from './utils/conversejs'
|
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
|
|
|
}
|
|
|
|
|
2022-04-11 16:12:12 +00:00
|
|
|
function isAnonymousUser (registerOptions: RegisterClientOptions): boolean {
|
|
|
|
return !registerOptions.peertubeHelpers.isLoggedIn()
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
function guessIsMine (registerOptions: RegisterClientOptions, video: Video): boolean {
|
2021-12-21 18:54:37 +00:00
|
|
|
// Note: this is not safe, but it is not a problem:
|
|
|
|
// this function is used for non critical functions
|
|
|
|
try {
|
|
|
|
if (!video) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!video.isLocal) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!window.localStorage) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const username = window.localStorage.getItem('username') ?? ''
|
|
|
|
if (!username) {
|
|
|
|
return false
|
|
|
|
}
|
2022-01-11 00:29:33 +00:00
|
|
|
if (username !== video.account?.name) {
|
2021-12-21 18:54:37 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err as string)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
function guessIamIModerator (_registerOptions: RegisterClientOptions): boolean {
|
2021-12-21 18:54:37 +00:00
|
|
|
// Note: this is not safe, but it is not a problem:
|
|
|
|
// this function is used for non critical functions
|
|
|
|
try {
|
|
|
|
if (!window.localStorage) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const role = window.localStorage.getItem('role') ?? ''
|
|
|
|
if (!role) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (role !== '0' && role !== '1') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err as string)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
function register (registerOptions: RegisterClientOptions): void {
|
2021-12-14 14:41:34 +00:00
|
|
|
const { registerHook, peertubeHelpers } = registerOptions
|
2024-03-28 14:06:15 +00:00
|
|
|
let settings: any = {} // will be loaded later
|
2021-02-19 17:21:40 +00:00
|
|
|
|
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...')
|
2023-09-06 15:58:54 +00:00
|
|
|
const viewersDocumentationHelpUrl = await localizedHelpUrl(registerOptions, {
|
|
|
|
page: 'documentation/user/viewers'
|
|
|
|
})
|
2024-03-28 14:06:15 +00:00
|
|
|
const p = new Promise<void>((resolve) => {
|
2021-04-07 16:14:58 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2021-02-20 17:31:21 +00:00
|
|
|
Promise.all([
|
2023-06-12 17:26:28 +00:00
|
|
|
peertubeHelpers.translate(LOC_OPEN_CHAT),
|
|
|
|
peertubeHelpers.translate(LOC_OPEN_CHAT_NEW_WINDOW),
|
|
|
|
peertubeHelpers.translate(LOC_CLOSE_CHAT),
|
2023-07-26 16:16:30 +00:00
|
|
|
peertubeHelpers.translate(LOC_SHARE_CHAT_LINK),
|
|
|
|
peertubeHelpers.translate(LOC_ONLINE_HELP)
|
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]
|
2023-07-26 16:16:30 +00:00
|
|
|
const labelHelp = labels[4]
|
2021-02-20 17:31:21 +00:00
|
|
|
|
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,
|
2024-03-28 14:06:15 +00:00
|
|
|
callback: () => {
|
|
|
|
openChat(video).then(() => {}, () => {})
|
|
|
|
},
|
2021-12-14 14:41:34 +00:00
|
|
|
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()
|
2024-03-28 14:06:15 +00:00
|
|
|
window.open('/p/livechat/room?room=' + encodeURIComponent(video.uuid))
|
2021-11-25 15:56:32 +00:00
|
|
|
},
|
2021-12-14 14:41:34 +00:00
|
|
|
icon: openBlankChatSVG,
|
|
|
|
additionalClasses: []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (showShareUrlButton) {
|
|
|
|
groupButtons.push({
|
|
|
|
buttonContainer,
|
|
|
|
name: 'shareurl',
|
|
|
|
label: labelShareUrl,
|
|
|
|
callback: () => {
|
2021-12-14 16:46:07 +00:00
|
|
|
shareChatUrl(registerOptions, settings, video).then(() => {}, () => {})
|
2021-12-14 14:41:34 +00:00
|
|
|
},
|
|
|
|
icon: shareChatUrlSVG,
|
|
|
|
additionalClasses: []
|
|
|
|
})
|
2021-02-20 23:13:29 +00:00
|
|
|
}
|
2023-07-26 16:16:30 +00:00
|
|
|
groupButtons.push({
|
|
|
|
buttonContainer,
|
|
|
|
name: 'help',
|
|
|
|
label: labelHelp,
|
2023-09-06 15:58:54 +00:00
|
|
|
href: viewersDocumentationHelpUrl,
|
2023-07-26 16:16:30 +00:00
|
|
|
targetBlank: true,
|
|
|
|
icon: helpButtonSVG,
|
|
|
|
additionalClasses: []
|
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
async function openChat (video: Video): Promise<void | false> {
|
2021-08-04 15:38:26 +00:00
|
|
|
if (!video) {
|
|
|
|
logger.log('No video.')
|
2021-04-07 16:14:58 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
logger.info(`Trying to load the chat for video ${video.uuid}.`)
|
2024-03-28 14:06:15 +00:00
|
|
|
// here the room key is always the video uuid, a backend API will translate to channel id if relevant.
|
|
|
|
const roomkey = video.uuid
|
|
|
|
if (!roomkey) {
|
|
|
|
logger.error('Can\'t get room xmpp addr')
|
2021-04-07 16:14:58 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-03-28 14:06:15 +00:00
|
|
|
|
2021-04-07 16:14:58 +00:00
|
|
|
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')
|
2021-02-20 17:31:21 +00:00
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
try {
|
|
|
|
if (!container) {
|
|
|
|
logger.error('Cant found the livechat container.')
|
|
|
|
return false
|
|
|
|
}
|
2021-02-19 17:21:40 +00:00
|
|
|
|
2024-04-02 15:40:43 +00:00
|
|
|
if (container.getElementsByTagName('converse-root').length) {
|
2024-03-28 14:06:15 +00:00
|
|
|
logger.error('Seems that there is already a ConverseJS in the container.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalStyles) {
|
|
|
|
container.setAttribute('style', additionalStyles)
|
|
|
|
}
|
|
|
|
container.setAttribute('peertube-plugin-livechat-state', 'open')
|
|
|
|
|
|
|
|
// Hacking styles...
|
|
|
|
hackStyles(true)
|
2024-04-03 15:36:01 +00:00
|
|
|
|
|
|
|
// Loading converseJS...
|
|
|
|
await displayConverseJS(registerOptions, container, roomkey, 'peertube-video', false)
|
2024-03-28 14:06:15 +00:00
|
|
|
} catch (err) {
|
2024-04-04 08:58:16 +00:00
|
|
|
// Displaying an error page.
|
|
|
|
if (container) {
|
|
|
|
const message = document.createElement('div')
|
|
|
|
message.classList.add('peertube-plugin-livechat-error-message')
|
|
|
|
message.innerText = await peertubeHelpers.translate(LOC_CHATROOM_NOT_ACCESSIBLE)
|
|
|
|
container.append(message)
|
2024-04-04 15:02:21 +00:00
|
|
|
|
|
|
|
container.querySelectorAll(
|
|
|
|
'.livechat-spinner, converse-root'
|
|
|
|
).forEach(dom => dom.remove())
|
2024-04-04 08:58:16 +00:00
|
|
|
}
|
2024-03-28 14:06:15 +00:00
|
|
|
|
2024-04-04 08:58:16 +00:00
|
|
|
hackStyles(false)
|
2024-03-28 14:06:15 +00:00
|
|
|
}
|
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) {
|
2024-04-02 15:40:43 +00:00
|
|
|
logger.error('Can\'t close livechat, container not found.')
|
2021-03-01 17:38:39 +00:00
|
|
|
return
|
|
|
|
}
|
2024-03-28 14:06:15 +00:00
|
|
|
|
|
|
|
// Disconnecting ConverseJS
|
|
|
|
if (window.converse?.livechatDisconnect) { window.converse.livechatDisconnect() }
|
|
|
|
|
|
|
|
// Removing from the DOM
|
2024-04-04 08:58:16 +00:00
|
|
|
container.querySelectorAll(
|
|
|
|
'converse-root, .livechat-spinner, .peertube-plugin-livechat-error-message'
|
|
|
|
).forEach(dom => dom.remove())
|
2021-02-20 17:31:21 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
async function initChat (video: Video): Promise<void> {
|
2021-05-07 16:52:01 +00:00
|
|
|
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
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
try {
|
|
|
|
settings = await peertubeHelpers.getSettings()
|
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...')
|
2022-04-11 16:12:12 +00:00
|
|
|
if (settings['chat-no-anonymous'] === true && isAnonymousUser(registerOptions)) {
|
|
|
|
logger.log('No chat for anonymous users')
|
|
|
|
return
|
|
|
|
}
|
2024-03-28 14:06:15 +00:00
|
|
|
if (!videoHasWebchat(settings, video) && !videoHasRemoteWebchat(settings, video)) {
|
2021-05-01 17:06:05 +00:00
|
|
|
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
|
2023-04-21 14:56:48 +00:00
|
|
|
if (video.isLocal) { // No need for shareButton on remote chats.
|
|
|
|
const chatShareUrl = settings['chat-share-url'] ?? ''
|
|
|
|
if (chatShareUrl === 'everyone') {
|
|
|
|
showShareUrlButton = true
|
|
|
|
} else if (chatShareUrl === 'owner') {
|
|
|
|
showShareUrlButton = guessIsMine(registerOptions, video)
|
|
|
|
} else if (chatShareUrl === 'owner+moderators') {
|
|
|
|
showShareUrlButton = guessIsMine(registerOptions, video) || guessIamIModerator(registerOptions)
|
|
|
|
}
|
2021-12-14 14:41:34 +00:00
|
|
|
}
|
2022-10-10 16:08:20 +00:00
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
await insertChatDom(container as HTMLElement, video, !!settings['chat-open-blank'], showShareUrlButton)
|
|
|
|
if (settings['chat-auto-display']) {
|
|
|
|
await openChat(video)
|
|
|
|
} else if (container) {
|
|
|
|
container.setAttribute('peertube-plugin-livechat-state', 'closed')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('initChat has failed')
|
|
|
|
logger.error(err as string)
|
|
|
|
}
|
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
|
|
|
|
}
|
2024-03-28 14:06:15 +00:00
|
|
|
initChat(video).then(() => {}, () => {})
|
2021-05-07 16:52:01 +00:00
|
|
|
}
|
|
|
|
})
|
2021-02-18 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
register
|
|
|
|
}
|