2023-04-20 14:07:00 +00:00
|
|
|
import type { RegisterServerOptions, VideoObject } from '@peertube/peertube-types'
|
2023-04-20 16:28:08 +00:00
|
|
|
import type { LiveChatVideoObject, VideoBuildResultContext, LiveChatJSONLDLink, LiveChatJSONLDAttribute } from './types'
|
|
|
|
import { storeVideoLiveChatInfos } from './storage'
|
2023-04-20 14:07:00 +00:00
|
|
|
import { videoHasWebchat } from '../../../shared/lib/video'
|
|
|
|
import { getBoshUri, getWSUri } from '../uri/webchat'
|
|
|
|
import { canonicalizePluginUri } from '../uri/canonicalize'
|
|
|
|
import { getProsodyDomain } from '../prosody/config/domain'
|
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
/**
|
|
|
|
* This function adds LiveChat information on video ActivityPub data if relevant.
|
|
|
|
* @param options server options
|
|
|
|
* @param jsonld JSON-LD video data to fill
|
|
|
|
* @param context handler context
|
|
|
|
* @returns void
|
|
|
|
*/
|
2023-04-20 14:07:00 +00:00
|
|
|
async function videoBuildJSONLD (
|
|
|
|
options: RegisterServerOptions,
|
|
|
|
jsonld: VideoObject,
|
|
|
|
context: VideoBuildResultContext
|
|
|
|
): Promise<VideoObject | LiveChatVideoObject> {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
const video = context.video
|
|
|
|
if (video.remote) { return jsonld } // should not happen, but... just in case...
|
|
|
|
|
|
|
|
const settings = await options.settingsManager.getSettings([
|
|
|
|
'chat-per-live-video',
|
|
|
|
'chat-all-lives',
|
|
|
|
'chat-all-non-lives',
|
|
|
|
'chat-videos-list',
|
|
|
|
'disable-websocket',
|
|
|
|
'prosody-room-type',
|
|
|
|
'federation-dont-publish-remotely'
|
|
|
|
])
|
|
|
|
|
|
|
|
if (settings['federation-dont-publish-remotely']) {
|
2023-04-20 16:32:13 +00:00
|
|
|
// Note: we store also outgoing data. Could help for migration/cleanup scripts, for example.
|
|
|
|
await storeVideoLiveChatInfos(options, video, false)
|
2023-04-20 14:07:00 +00:00
|
|
|
return jsonld
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasChat = await videoHasWebchat({
|
|
|
|
'chat-per-live-video': !!settings['chat-per-live-video'],
|
|
|
|
'chat-all-lives': !!settings['chat-all-lives'],
|
|
|
|
'chat-all-non-lives': !!settings['chat-all-non-lives'],
|
|
|
|
'chat-videos-list': settings['chat-videos-list'] as string
|
|
|
|
}, video)
|
|
|
|
|
|
|
|
if (!hasChat) {
|
2023-04-20 16:28:08 +00:00
|
|
|
logger.debug(`Video uuid=${video.uuid} has not livechat, adding peertubeLiveChat=false.`)
|
|
|
|
// Note: we store also outgoing data. Could help for migration/cleanup scripts, for example.
|
|
|
|
await storeVideoLiveChatInfos(options, video, false)
|
|
|
|
Object.assign(jsonld, {
|
|
|
|
peertubeLiveChat: false
|
|
|
|
})
|
2023-04-20 14:07:00 +00:00
|
|
|
return jsonld
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug(`Adding LiveChat data on video uuid=${video.uuid}...`)
|
|
|
|
|
|
|
|
const prosodyDomain = await getProsodyDomain(options)
|
|
|
|
const userJID = 'anon.' + prosodyDomain
|
|
|
|
let roomJID: string
|
|
|
|
if (settings['prosody-room-type'] === 'channel') {
|
|
|
|
roomJID = `channel.${video.channelId}@room.${prosodyDomain}`
|
|
|
|
} else {
|
|
|
|
roomJID = `${video.uuid}@room.${prosodyDomain}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const links: LiveChatJSONLDLink[] = [{
|
|
|
|
type: 'xmpp-bosh-anonymous',
|
|
|
|
url: canonicalizePluginUri(options, getBoshUri(options), { removePluginVersion: true }),
|
|
|
|
jid: userJID
|
|
|
|
}]
|
|
|
|
if (!settings['disable-websocket']) {
|
|
|
|
const wsUri = getWSUri(options)
|
|
|
|
if (wsUri) {
|
|
|
|
links.push({
|
|
|
|
type: 'xmpp-websocket-anonymous',
|
|
|
|
url: canonicalizePluginUri(options, wsUri, {
|
|
|
|
removePluginVersion: true,
|
|
|
|
protocol: 'ws'
|
|
|
|
}),
|
|
|
|
jid: userJID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
const peertubeLiveChat: LiveChatJSONLDAttribute = {
|
2023-04-20 14:07:00 +00:00
|
|
|
type: 'xmpp',
|
|
|
|
jid: roomJID,
|
|
|
|
links
|
|
|
|
}
|
2023-04-20 16:28:08 +00:00
|
|
|
Object.assign(jsonld, {
|
|
|
|
peertubeLiveChat
|
|
|
|
})
|
|
|
|
// Note: we store also outgoing data. Could help for migration/cleanup scripts, for example.
|
|
|
|
await storeVideoLiveChatInfos(options, video, peertubeLiveChat)
|
2023-04-20 14:07:00 +00:00
|
|
|
return jsonld
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoBuildJSONLD
|
|
|
|
}
|