2023-08-08 15:15:05 +00:00
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
2023-12-27 11:48:45 +00:00
|
|
|
import type { InitConverseJSParams } from 'shared/lib/types'
|
2023-09-06 13:23:39 +00:00
|
|
|
import { renderConfigurationHome } from './templates/home'
|
|
|
|
import { renderConfigurationChannel } from './templates/channel'
|
2023-12-27 11:48:45 +00:00
|
|
|
import { getBaseRoute } from '../../utils/uri'
|
|
|
|
import { loadConverseJS } from '../../utils/conversejs'
|
2023-08-08 15:15:05 +00:00
|
|
|
|
2023-08-08 16:26:40 +00:00
|
|
|
/**
|
2023-09-06 13:23:39 +00:00
|
|
|
* Registers stuff related to the user's configuration pages.
|
2023-08-08 16:26:40 +00:00
|
|
|
* @param clientOptions Peertube client options
|
|
|
|
*/
|
2023-09-06 13:23:39 +00:00
|
|
|
async function registerConfiguration (clientOptions: RegisterClientOptions): Promise<void> {
|
2023-08-08 15:15:05 +00:00
|
|
|
const { peertubeHelpers, registerClientRoute, registerHook } = clientOptions
|
|
|
|
|
2023-09-06 13:56:55 +00:00
|
|
|
const settings = await peertubeHelpers.getSettings()
|
2023-09-18 16:53:07 +00:00
|
|
|
if (settings['disable-channel-configuration']) { return }
|
2023-09-06 13:56:55 +00:00
|
|
|
|
2023-12-27 11:48:45 +00:00
|
|
|
registerClientRoute({
|
|
|
|
route: 'livechat/room',
|
|
|
|
onMount: async ({ rootEl }) => {
|
|
|
|
try {
|
|
|
|
const urlParams = new URLSearchParams(window.location.search)
|
|
|
|
const roomKey = urlParams.get('room')
|
|
|
|
if (!roomKey) {
|
|
|
|
throw new Error('missing room parameter')
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
getBaseRoute(clientOptions) + '/api/configuration/room/' + encodeURIComponent(roomKey),
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
headers: peertubeHelpers.getAuthHeader()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Can\'t get channel configuration options.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const converseJSParams: InitConverseJSParams = await (response).json()
|
|
|
|
await loadConverseJS(converseJSParams)
|
|
|
|
|
2023-12-27 14:51:43 +00:00
|
|
|
const container = document.createElement('div')
|
|
|
|
container.classList.add('livechat-embed-fullpage')
|
|
|
|
rootEl.append(container)
|
|
|
|
const converseRoot = document.createElement('converse-root')
|
|
|
|
converseRoot.classList.add('theme-peertube')
|
|
|
|
container.append(converseRoot)
|
2023-12-27 11:48:45 +00:00
|
|
|
|
2024-03-26 14:38:22 +00:00
|
|
|
const authHeader = peertubeHelpers.getAuthHeader()
|
|
|
|
|
|
|
|
window.initConverse(converseJSParams, 'peertube-fullpage', authHeader ?? null)
|
2023-12-27 11:48:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error('[peertube-plugin-livechat] ' + (err as string))
|
|
|
|
rootEl.innerText = await peertubeHelpers.translate(LOC_NOT_FOUND)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-08-08 15:15:05 +00:00
|
|
|
registerClientRoute({
|
2023-09-06 13:23:39 +00:00
|
|
|
route: 'livechat/configuration',
|
2023-08-08 15:15:05 +00:00
|
|
|
onMount: async ({ rootEl }) => {
|
2023-09-06 13:23:39 +00:00
|
|
|
rootEl.innerHTML = await renderConfigurationHome(clientOptions)
|
2023-08-08 15:15:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-08-08 16:26:40 +00:00
|
|
|
registerClientRoute({
|
2023-09-06 13:23:39 +00:00
|
|
|
route: 'livechat/configuration/channel',
|
2023-08-08 16:26:40 +00:00
|
|
|
onMount: async ({ rootEl }) => {
|
|
|
|
const urlParams = new URLSearchParams(window.location.search)
|
|
|
|
const channelId = urlParams.get('channelId') ?? ''
|
2023-09-21 15:13:28 +00:00
|
|
|
await renderConfigurationChannel(clientOptions, channelId, rootEl)
|
2023-08-08 16:26:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-08-08 15:15:05 +00:00
|
|
|
registerHook({
|
|
|
|
target: 'filter:left-menu.links.create.result',
|
|
|
|
handler: async (links: any) => {
|
2023-09-06 13:23:39 +00:00
|
|
|
// Adding the links to livechat/configuration for logged users.
|
2023-08-08 15:15:05 +00:00
|
|
|
if (!peertubeHelpers.isLoggedIn()) { return links }
|
|
|
|
|
|
|
|
if (!Array.isArray(links)) { return links }
|
|
|
|
let myLibraryLinks
|
|
|
|
// Searching the 'in-my-library' entry.
|
|
|
|
for (const link of links) {
|
|
|
|
if (typeof link !== 'object') { continue }
|
|
|
|
if (!('key' in link)) { continue }
|
|
|
|
if (link.key !== 'in-my-library') { continue }
|
|
|
|
myLibraryLinks = link
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if (!myLibraryLinks) { return links }
|
|
|
|
if (!Array.isArray(myLibraryLinks.links)) { return links }
|
|
|
|
|
2023-09-06 13:23:39 +00:00
|
|
|
const label = await peertubeHelpers.translate(LOC_MENU_CONFIGURATION_LABEL)
|
2023-08-08 15:15:05 +00:00
|
|
|
myLibraryLinks.links.push({
|
|
|
|
label,
|
|
|
|
shortLabel: label,
|
2023-09-06 13:23:39 +00:00
|
|
|
path: '/p/livechat/configuration',
|
2023-08-08 15:15:05 +00:00
|
|
|
icon: 'live'
|
|
|
|
})
|
|
|
|
return links
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2023-09-06 13:23:39 +00:00
|
|
|
registerConfiguration
|
2023-08-08 15:15:05 +00:00
|
|
|
}
|