// SPDX-FileCopyrightText: 2024 John Livingston
//
// SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterServerOptions, Video, MVideoThumbnail } from '@peertube/peertube-types'
import { getVideoLiveChatInfos } from './federation/storage'
import { anonymousConnectionInfos, compatibleRemoteAuthenticatedConnectionEnabled } from './federation/connection-infos'
async function initCustomFields (options: RegisterServerOptions): Promise {
const registerHook = options.registerHook
const storageManager = options.storageManager
const logger = options.peertubeHelpers.logger
registerHook({
target: 'action:api.live-video.created',
handler: async ({ video, req }: { video: Video | undefined, req: any }) => {
if (!video?.id) { return }
// When creating a new live, if 'chat-per-live-video' is true,
// we must read req.body.pluginData['livechat-active'] (as for action:api.video.updated).
// This is done for the Peertube live Android app, which does not update the video after creation.
// See: https://github.com/JohnXLivingston/peertube-plugin-livechat/issues/400
// Note: following code is a little bit verbose, to be sure it can't fail, even with old Peertube versions.
if (!req || (typeof req !== 'object') || !('body' in req)) { return }
if (!req.body || (typeof req.body !== 'object') || !('pluginData' in req.body)) { return }
const pluginData = req.body?.pluginData
if (!pluginData || (typeof pluginData !== 'object') || !('livechat-active' in pluginData)) { return }
if (pluginData['livechat-active'] !== true) { return }
const setting = await options.settingsManager.getSetting('chat-per-live-video')
if (setting !== true) { return }
logger.info(
'New live created, livechat-active parameter given, ' +
`enabling chat by default by setting livechat-active=true for video ${video.id.toString()}.`
)
await storageManager.storeData(`livechat-active-${video.id.toString()}`, true)
}
})
registerHook({
target: 'action:api.video.updated',
handler: async (params: any) => {
logger.debug('Saving a video, checking for custom fields')
const body: any = params.body
const video: Video | undefined = params.video
if (!video || !video.id) {
return
}
if (!body.pluginData) return
const value = body.pluginData['livechat-active']
// NB: on Peertube 3.2.1, value is "true", "false", or "null", as strings.
if (value === true || value === 'true') {
logger.info(`Saving livechat-active=true for video ${video.id.toString()}`)
await storageManager.storeData(`livechat-active-${video.id.toString()}`, true)
} else if (value === false || value === 'false' || value === 'null') {
logger.info(`Saving livechat-active=false for video ${video.id.toString()}`)
await storageManager.storeData(`livechat-active-${video.id.toString()}`, false)
} else {
logger.error('Unknown value ' + JSON.stringify(value) + ' for livechat-active field.')
}
}
})
registerHook({
target: 'filter:api.video.get.result',
handler: async (video: Video): Promise