2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
|
|
|
import type { Video } from '@peertube/peertube-types'
|
2024-06-14 11:03:14 +00:00
|
|
|
import type { LiveChatSettings } from '../lib/contexts/peertube'
|
2024-06-13 14:36:16 +00:00
|
|
|
import { logger } from '../../utils/logger'
|
2024-06-14 11:03:14 +00:00
|
|
|
import { html, render } from 'lit'
|
2021-12-14 16:46:07 +00:00
|
|
|
|
2024-06-14 11:03:14 +00:00
|
|
|
async function shareChatUrl (
|
|
|
|
registerOptions: RegisterClientOptions,
|
|
|
|
settings: LiveChatSettings,
|
|
|
|
video: Video
|
|
|
|
): Promise<void> {
|
2021-12-14 16:46:07 +00:00
|
|
|
const peertubeHelpers = registerOptions.peertubeHelpers
|
|
|
|
|
2024-06-14 11:03:14 +00:00
|
|
|
const labelShare = await peertubeHelpers.translate(LOC_SHARE_CHAT_LINK)
|
|
|
|
|
|
|
|
// function save (form: ShareForm): void {
|
|
|
|
// if (!window.localStorage) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// const v = {
|
|
|
|
// version: 1, // in case we add incompatible values in a near feature
|
|
|
|
// readonly: !!form.readonly.checked,
|
|
|
|
// withscroll: !!form.withscroll.checked,
|
|
|
|
// transparent: !!form.transparent.checked,
|
|
|
|
// autocolors: !!form.autoColors?.checked,
|
|
|
|
// generateIframe: !!form.generateIframe.checked,
|
|
|
|
// protocol: !form.radioProtocolWeb || form.radioProtocolWeb.checked ? 'web' : 'xmpp'
|
|
|
|
// }
|
|
|
|
// window.localStorage.setItem('peertube-plugin-livechat-shareurl', JSON.stringify(v))
|
|
|
|
// }
|
|
|
|
|
|
|
|
// function restore (form: ShareForm): void {
|
|
|
|
// if (!window.localStorage) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// const s = window.localStorage.getItem('peertube-plugin-livechat-shareurl')
|
|
|
|
// if (!s) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// let v: any
|
|
|
|
// try {
|
|
|
|
// v = JSON.parse(s)
|
|
|
|
// if (!v || (typeof v !== 'object') || v.version !== 1) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// form.readonly.checked = !!v.readonly
|
|
|
|
// form.withscroll.checked = !!v.withscroll
|
|
|
|
// form.transparent.checked = !!v.transparent
|
|
|
|
// if (form.autoColors) {
|
|
|
|
// form.autoColors.checked = !!v.autocolors
|
|
|
|
// }
|
|
|
|
// form.generateIframe.checked = !!v.generateIframe
|
|
|
|
// if (form.radioProtocolXMPP && v.protocol === 'xmpp') {
|
|
|
|
// form.radioProtocolXMPP.checked = true
|
|
|
|
// } else if (form.radioProtocolWeb) {
|
|
|
|
// form.radioProtocolWeb.checked = true
|
|
|
|
// }
|
|
|
|
// } catch (err) {
|
|
|
|
// logger.error(err as string)
|
|
|
|
// }
|
|
|
|
// }
|
2021-12-21 17:39:02 +00:00
|
|
|
|
2021-12-14 16:46:07 +00:00
|
|
|
logger.info('Opening the share modal...')
|
2024-06-14 11:03:14 +00:00
|
|
|
// We can't just put <livechat-share-chat> in modalContent, Peertube sanitize it...
|
2021-12-14 16:46:07 +00:00
|
|
|
const observer = new MutationObserver(mutations => {
|
|
|
|
for (const { addedNodes } of mutations) {
|
|
|
|
addedNodes.forEach(node => {
|
|
|
|
if ((node as HTMLElement).localName === 'ngb-modal-window') {
|
|
|
|
logger.info('Detecting a new modal, checking if this is the good one...')
|
|
|
|
if (!(node as HTMLElement).querySelector) { return }
|
|
|
|
const title = (node as HTMLElement).querySelector('.modal-title')
|
|
|
|
if (!(title?.textContent === labelShare)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.info('Yes, it is the good modal!')
|
|
|
|
observer.disconnect()
|
|
|
|
|
|
|
|
const modalBodyElem: HTMLElement | null = (node as HTMLElement).querySelector('.modal-body')
|
|
|
|
if (!modalBodyElem) {
|
|
|
|
logger.error('Modal has no body... Dont know how to fill it.')
|
|
|
|
return
|
|
|
|
}
|
2024-06-14 11:03:14 +00:00
|
|
|
modalBodyElem.childNodes.forEach(child => modalBodyElem.removeChild(child))
|
|
|
|
|
|
|
|
render(html`
|
|
|
|
<livechat-share-chat
|
|
|
|
._settings=${settings}
|
|
|
|
._video=${video}
|
|
|
|
></livechat-share-chat>
|
|
|
|
`, modalBodyElem)
|
2021-12-14 16:46:07 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
observer.observe(document.body, {
|
|
|
|
childList: true
|
|
|
|
})
|
2021-12-14 14:41:34 +00:00
|
|
|
peertubeHelpers.showModal({
|
2021-12-14 16:46:07 +00:00
|
|
|
title: labelShare,
|
2024-06-14 11:03:14 +00:00
|
|
|
content: '',
|
2021-12-14 14:41:34 +00:00
|
|
|
close: true
|
|
|
|
})
|
2021-12-14 16:46:07 +00:00
|
|
|
// just in case, remove the observer after a timeout, if not already done...
|
|
|
|
setTimeout(() => {
|
|
|
|
observer.disconnect()
|
|
|
|
}, 1000)
|
2021-12-14 14:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
shareChatUrl
|
|
|
|
}
|