Don't display webchat when viewing a playlist:

* remove old Peertube 3.0.1 hook compatibility
* use new parameters for hook action:video-watch.video.loaded (Peertube
3.2.0)
* backward compatibility with Peertube 3.1.0 hooks
* don't display webchat if there is a current playlist
This commit is contained in:
John Livingston 2021-05-07 18:52:01 +02:00
parent 32724fa7d8
commit bebefb1a5d
2 changed files with 48 additions and 29 deletions

View File

@ -2,6 +2,10 @@
## ??? ## ???
### Features
* Don't display webchat when viewing a playlist.
### Fixes ### Fixes
* Fix: starting with Peertube 3.2.0, there is a header 'X-Frame-Options'. Removing it on the iframe route. * Fix: starting with Peertube 3.2.0, there is a header 'X-Frame-Options'. Removing it on the iframe route.

View File

@ -2,6 +2,12 @@ import { videoHasWebchat } from 'shared/lib/video'
interface VideoCache {[key: string]: Video} interface VideoCache {[key: string]: Video}
interface VideoWatchLoadedHookOptions {
videojs: any
video?: Video // comes with Peertube 3.2.0
playlist?: any // comes with Peertube 3.2.0
}
function register ({ registerHook, peertubeHelpers }: RegisterOptions): void { function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
const logger = { const logger = {
log: (s: string) => console.log('[peertube-plugin-livechat] ' + s), log: (s: string) => console.log('[peertube-plugin-livechat] ' + s),
@ -10,8 +16,10 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
warn: (s: string) => console.warn('[peertube-plugin-livechat] ' + s) warn: (s: string) => console.warn('[peertube-plugin-livechat] ' + s)
} }
// This is for backward compatibility with Peertube < 3.2.0.
const videoCache: VideoCache = {} const videoCache: VideoCache = {}
let lastUUID: string | null = null let lastUUID: string | null = null
let settings: any = {} let settings: any = {}
function getBaseRoute (): string { function getBaseRoute (): string {
@ -100,7 +108,7 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
buttonContainer.classList.add('peertube-plugin-livechat-buttons') buttonContainer.classList.add('peertube-plugin-livechat-buttons')
container.append(buttonContainer) container.append(buttonContainer)
displayButton(buttonContainer, 'open', labelOpen, () => openChat(), 'talking.svg') displayButton(buttonContainer, 'open', labelOpen, () => openChat(uuid), 'talking.svg')
if (showOpenBlank) { if (showOpenBlank) {
displayButton(buttonContainer, 'openblank', labelOpenBlank, () => { displayButton(buttonContainer, 'openblank', labelOpenBlank, () => {
closeChat() closeChat()
@ -115,8 +123,7 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
return p return p
} }
function openChat (): void | boolean { function openChat (uuid: string): void | boolean {
const uuid = lastUUID
if (!uuid) { if (!uuid) {
logger.log('No current uuid.') logger.log('No current uuid.')
return false return false
@ -166,7 +173,11 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
container.setAttribute('peertube-plugin-livechat-state', 'closed') container.setAttribute('peertube-plugin-livechat-state', 'closed')
} }
function initChat (): void { function initChat (video: Video): void {
if (!video) {
logger.error('No video provided')
return
}
const videoWrapper = document.querySelector('#video-wrapper') const videoWrapper = document.querySelector('#video-wrapper')
if (!videoWrapper) { if (!videoWrapper) {
logger.error('The required div is not present in the DOM.') logger.error('The required div is not present in the DOM.')
@ -186,25 +197,14 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
settings = s settings = s
logger.log('Checking if this video should have a chat...') logger.log('Checking if this video should have a chat...')
const uuid = lastUUID
if (!uuid) {
logger.error('There is no lastUUID.')
return
}
const video = videoCache[uuid]
if (!video) {
logger.error('Can\'t find the video ' + uuid + ' in the videoCache')
return
}
if (!videoHasWebchat(s, video)) { if (!videoHasWebchat(s, video)) {
logger.log('This video has no webchat') logger.log('This video has no webchat')
return return
} }
insertChatDom(container as HTMLElement, uuid, !!settings['chat-open-blank']).then(() => { insertChatDom(container as HTMLElement, video.uuid, !!settings['chat-open-blank']).then(() => {
if (settings['chat-auto-display']) { if (settings['chat-auto-display']) {
openChat() openChat(video.uuid)
} else if (container) { } else if (container) {
container.setAttribute('peertube-plugin-livechat-state', 'closed') container.setAttribute('peertube-plugin-livechat-state', 'closed')
} }
@ -219,24 +219,39 @@ function register ({ registerHook, peertubeHelpers }: RegisterOptions): void {
registerHook({ registerHook({
target: 'filter:api.video-watch.video.get.result', target: 'filter:api.video-watch.video.get.result',
handler: (video: Video) => { handler: (video: Video) => {
// For now, hooks for action:video-watch... did not receive the video object // For Peertube < 3.2.0, hooks for action:video-watch... did not receive the video object
// So we store video objects in videoCache // So we store video objects in videoCache
videoCache[video.uuid] = video videoCache[video.uuid] = video
lastUUID = video.uuid lastUUID = video.uuid
// FIXME: this should be made in action:video-watch.video.loaded.
// But with Peertube 3.0.1, this hook is not called for lives
// in WAITING_FOR_LIVE and LIVE_ENDED states.
initChat()
return video return video
} }
}) })
// FIXME: this should be the correct hook for initChat... registerHook({
// registerHook({ target: 'action:video-watch.video.loaded',
// target: 'action:video-watch.video.loaded', handler: ({
// handler: () => { video,
// initChat() 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)
}
})
} }
export { export {