Chat Federation: send the information to the frontend.

This commit is contained in:
John Livingston 2023-04-21 12:02:52 +02:00
parent b1f1271389
commit 333590defd
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
3 changed files with 73 additions and 9 deletions

View File

@ -1,4 +1,6 @@
import type { RegisterServerOptions, Video } from '@peertube/peertube-types'
import type { RegisterServerOptions, Video, MVideoThumbnail } from '@peertube/peertube-types'
import { getVideoLiveChatInfos } from './federation/storage'
async function initCustomFields (options: RegisterServerOptions): Promise<void> {
const registerHook = options.registerHook
const storageManager = options.storageManager
@ -32,8 +34,11 @@ async function initCustomFields (options: RegisterServerOptions): Promise<void>
registerHook({
target: 'filter:api.video.get.result',
handler: async (video: Video): Promise<Video> => {
logger.debug('Getting a video, searching for custom fields')
logger.debug('Getting a video, searching for custom fields and data')
await fillVideoCustomFields(options, video)
if (!video.isLocal) {
await fillVideoRemoteLiveChat(options, video)
}
return video
}
})
@ -46,6 +51,7 @@ interface LiveChatCustomFieldsVideo {
isLive: boolean
pluginData?: {
'livechat-active'?: boolean
'livechat-remote'?: boolean
}
}
@ -68,7 +74,22 @@ async function fillVideoCustomFields (options: RegisterServerOptions, video: Liv
}
}
async function fillVideoRemoteLiveChat (
options: RegisterServerOptions,
video: Video | MVideoThumbnail
): Promise<void> {
if (('remote' in video) && !video.remote) { return }
if (('isLocal' in video) && video.isLocal) { return }
const infos = await getVideoLiveChatInfos(options, video)
if (!infos) { return }
const v: LiveChatCustomFieldsVideo = video
if (!v.pluginData) v.pluginData = {}
v.pluginData['livechat-remote'] = true
}
export {
initCustomFields,
fillVideoCustomFields
fillVideoCustomFields,
fillVideoRemoteLiveChat
}

View File

@ -1,5 +1,6 @@
import type { RegisterServerOptions, MVideoFullLight, MVideoAP } from '@peertube/peertube-types'
import type { RegisterServerOptions, MVideoFullLight, MVideoAP, Video, MVideoThumbnail } from '@peertube/peertube-types'
import type { LiveChatJSONLDAttribute } from './types'
import { sanitizePeertubeLiveChatInfos } from './sanitize'
import { URL } from 'url'
import * as fs from 'fs'
import * as path from 'path'
@ -50,10 +51,34 @@ async function storeVideoLiveChatInfos (
return
}
logger.debug(`${remote ? 'Remote' : 'Local'} video ${video.uuid} has caht infos to store`)
logger.debug(`${remote ? 'Remote' : 'Local'} video ${video.uuid} has chat infos to store`)
await _store(options, filePath, liveChatInfos)
}
/**
* Gets the stored livechat information (if any).
* @param options server options
* @param video video object
* @returns livechat stored data
*/
async function getVideoLiveChatInfos (
options: RegisterServerOptions,
video: MVideoFullLight | MVideoAP | Video | MVideoThumbnail
): Promise<LiveChatJSONLDAttribute> {
const logger = options.peertubeHelpers.logger
const remote = ('remote' in video) ? video.remote : !video.isLocal
const filePath = await _getFilePath(options, remote, video.uuid, video.url)
if (!filePath) {
logger.error('Cant compute the file path for storing liveChat infos for video ' + video.uuid)
return false
}
const content = await _get(options, filePath)
if (content === null) { return false }
// We must sanitize here, in case a previous plugin version did not sanitize enougth.
return sanitizePeertubeLiveChatInfos(content)
}
async function _getFilePath (
options: RegisterServerOptions,
remote: boolean,
@ -123,6 +148,23 @@ async function _store (options: RegisterServerOptions, filePath: string, content
}
}
export {
storeVideoLiveChatInfos
async function _get (options: RegisterServerOptions, filePath: string): Promise<any | null> {
const logger = options.peertubeHelpers.logger
try {
if (!fs.existsSync(filePath)) {
return null
}
const content = await fs.promises.readFile(filePath, {
encoding: 'utf-8'
})
return JSON.parse(content)
} catch (err) {
logger.error(err)
return null
}
}
export {
storeVideoLiveChatInfos,
getVideoLiveChatInfos
}

View File

@ -7,7 +7,7 @@ import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered }
import { getUserNickname } from '../helpers'
import { Affiliations, getVideoAffiliations, getChannelAffiliations } from '../prosody/config/affiliations'
import { getProsodyDomain } from '../prosody/config/domain'
import { fillVideoCustomFields } from '../custom-fields'
import { fillVideoCustomFields, fillVideoRemoteLiveChat } from '../custom-fields'
import { getChannelInfosById } from '../database/channel'
// See here for description: https://modules.prosody.im/mod_muc_http_defaults.html
@ -96,8 +96,9 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
return
}
// Adding the custom fields:
// Adding the custom fields and data:
await fillVideoCustomFields(options, video)
if (video.remote) { await fillVideoRemoteLiveChat(options, video) }
// check settings (chat enabled for this video?)
const settings = await options.settingsManager.getSettings([