Some refactoring.

This commit is contained in:
John Livingston
2023-07-05 18:33:30 +02:00
committed by John Livingston
parent 2e98d930d3
commit 995dfa4dff
7 changed files with 141 additions and 79 deletions

87
server/lib/rss/init.ts Normal file
View File

@ -0,0 +1,87 @@
import type { RegisterServerOptions, Video } from '@peertube/peertube-types'
import type { CustomTag } from '@peertube/feed/lib/typings'
import { videoHasWebchat } from '../../../shared/lib/video'
import { fillVideoCustomFields } from '../custom-fields'
import { getProsodyDomain } from '../prosody/config/domain'
import { getPublicChatUri } from '../uri/webchat'
async function initRSS (options: RegisterServerOptions): Promise<void> {
const logger = options.peertubeHelpers.logger
const registerHook = options.registerHook
logger.info('Registring RSS hooks...')
registerHook({
target: 'filter:feed.podcast.video.create-custom-tags.result',
handler: async (
result: CustomTag[], { video, liveItem }: { video: Video, liveItem: boolean }
): Promise<CustomTag[]> => {
if (!liveItem) {
// Note: the Podcast RSS feed specification does not handle chats for non-live.
// So we just return here.
return result
}
// FIXME: calling getSettings for each RSS entry is not optimal.
// Settings should be cached somewhere on the plugin level.
// (i already have some plans to do something for this)
const settings = await options.settingsManager.getSettings([
'chat-per-live-video',
'chat-all-lives',
'chat-all-non-lives',
'chat-videos-list',
'prosody-room-type',
'federation-dont-publish-remotely',
'prosody-room-allow-s2s',
'prosody-s2s-port'
])
if (settings['federation-dont-publish-remotely']) {
// Chat must not be published to the outer world.
return result
}
await fillVideoCustomFields(options, video)
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) {
logger.debug(`Video uuid=${video.uuid} has not livechat, no need to add podcast:chat tag.`)
return result
}
const prosodyDomain = await getProsodyDomain(options)
const podcastChat: any = {
name: 'podcast:chat',
attributes: {
server: prosodyDomain,
protocol: 'xmpp',
// space: will be added only if external XMPP connections are available
embedUrl: getPublicChatUri(options, video)
}
}
// In order to connect to the chat using standard xmpp, it requires these settings:
// - prosody-room-allow-s2s
// - prosody-s2s-port
if (settings['prosody-room-allow-s2s'] && settings['prosody-s2s-port']) {
let roomJID: string
if (settings['prosody-room-type'] === 'channel') {
roomJID = `channel.${video.channel.id}@room.${prosodyDomain}`
} else {
roomJID = `${video.uuid}@room.${prosodyDomain}`
}
podcastChat.attributes.space = roomJID
}
return result.concat([podcastChat])
}
})
}
export {
initRSS
}

View File

@ -1,5 +1,4 @@
import type { RegisterServerOptions, VideoObject } from '@peertube/peertube-types'
import type { RegisterServerOptions, VideoObject, Video } from '@peertube/peertube-types'
import { getBaseRouterRoute, getBaseWebSocketRoute } from '../helpers'
import { canonicalizePluginUri } from './canonicalize'
@ -19,7 +18,7 @@ export function getWSS2SUri (options: RegisterServerOptions): string | undefined
return base + 'xmpp-websocket-s2s'
}
export function getPublicChatUri (options: RegisterServerOptions, video: VideoObject): string {
export function getPublicChatUri (options: RegisterServerOptions, video: VideoObject | Video): string {
const url = getBaseRouterRoute(options) + 'webchat/room/' + encodeURIComponent(video.uuid)
return canonicalizePluginUri(options, url, {
removePluginVersion: true

View File

@ -1,15 +1,14 @@
import type { RegisterServerOptions, Video } from '@peertube/peertube-types'
import type { RegisterServerOptions } from '@peertube/peertube-types'
import { migrateSettings } from './lib/migration/settings'
import { initSettings } from './lib/settings'
import { initCustomFields } from './lib/custom-fields'
import { initRouters } from './lib/routers/index'
import { initFederation } from './lib/federation/init'
import { initRSS } from './lib/rss/init'
import { prepareProsody, ensureProsodyRunning, ensureProsodyNotRunning } from './lib/prosody/ctl'
import { unloadDebugMode } from './lib/debug'
import { loadLoc } from './lib/loc'
import decache from 'decache'
import { CustomTag } from '@peertube/feed/lib/typings'
import { URL } from 'url'
// FIXME: Peertube unregister don't have any parameter.
// Using this global variable to fix this, so we can use helpers to unregister.
@ -32,33 +31,7 @@ async function register (options: RegisterServerOptions): Promise<any> {
await initCustomFields(options)
await initRouters(options)
await initFederation(options)
options.registerHook({
// @ts-expect-error Type doesn't exist for peertube 5.1 yet
target: 'filter:feed.podcast.video.create-custom-tags.result',
handler: (result: CustomTag[], { video, liveItem }: { video: Video, liveItem: boolean }) => {
if (!liveItem) {
return result
}
const webserverUrl = options.peertubeHelpers.config.getWebserverUrl()
const hostname = (new URL(webserverUrl)).hostname
const embedUrl = `${webserverUrl}/plugins/livechat/router/webchat/room/${encodeURIComponent(video.uuid)}`
const xmppRoom = `room.${hostname}`
return result.concat([
{
name: 'podcast:chat',
attributes: {
server: hostname,
protocol: 'xmpp',
space: `${video.uuid}@${xmppRoom}`,
embedUrl: embedUrl
}
}
])
}
})
await initRSS(options)
try {
await prepareProsody(options)