2024-05-23 15:17:28 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 Mehdi Benadel <https://mehdibenadel.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-06-13 14:36:16 +00:00
|
|
|
import type { SettingEntries } from '@peertube/peertube-types'
|
2024-05-23 14:56:11 +00:00
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client/types'
|
2024-06-14 11:03:14 +00:00
|
|
|
import type { ConverseJSTheme } from 'shared/lib/types'
|
2024-06-13 14:36:16 +00:00
|
|
|
import { logger as loggerFunction } from '../../../utils/logger'
|
2024-05-23 14:56:11 +00:00
|
|
|
|
2024-06-13 14:36:16 +00:00
|
|
|
// We precise some of the settings in SettingsEntries, to faciliate some type checking.
|
|
|
|
export type LiveChatSettings = SettingEntries & {
|
|
|
|
'chat-per-live-video': boolean
|
|
|
|
'chat-all-lives': boolean
|
|
|
|
'chat-all-non-lives': boolean
|
|
|
|
'chat-videos-list': string
|
|
|
|
'federation-no-remote-chat': boolean
|
|
|
|
'chat-style': string | undefined
|
2024-06-14 11:03:14 +00:00
|
|
|
'prosody-room-allow-s2s': boolean
|
|
|
|
'converse-theme': ConverseJSTheme
|
|
|
|
'prosody-room-type': string
|
2024-06-17 13:21:22 +00:00
|
|
|
'livechat-token-disabled': boolean
|
2024-06-13 14:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PtContext {
|
|
|
|
public readonly ptOptions: RegisterClientOptions
|
|
|
|
public readonly logger: typeof loggerFunction
|
|
|
|
private _settings: LiveChatSettings | undefined
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
ptOptions: RegisterClientOptions,
|
|
|
|
logger: typeof loggerFunction
|
|
|
|
) {
|
|
|
|
this.ptOptions = ptOptions
|
|
|
|
this.logger = logger
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the livechat settings.
|
|
|
|
* Keep them in cache after first request.
|
|
|
|
*/
|
|
|
|
public async getSettings (): Promise<LiveChatSettings> {
|
|
|
|
if (!this._settings) {
|
|
|
|
this._settings = await this.ptOptions.peertubeHelpers.getSettings() as LiveChatSettings
|
|
|
|
}
|
|
|
|
return this._settings
|
|
|
|
}
|
2024-06-12 14:26:35 +00:00
|
|
|
}
|
2024-05-24 12:25:08 +00:00
|
|
|
|
2024-06-12 14:26:35 +00:00
|
|
|
let context: PtContext
|
|
|
|
|
|
|
|
export function getPtContext (): PtContext {
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('Peertube context not set yet, getPtContext was called too soon.')
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initPtContext (ptOptions: RegisterClientOptions): void {
|
2024-06-13 14:36:16 +00:00
|
|
|
context = new PtContext(
|
|
|
|
ptOptions,
|
|
|
|
loggerFunction
|
|
|
|
)
|
2024-06-12 14:26:35 +00:00
|
|
|
}
|