peertube-plugin-livechat/server/main.ts

91 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-07-05 16:33:30 +00:00
import type { RegisterServerOptions } from '@peertube/peertube-types'
import { migrateSettings } from './lib/migration/settings'
import { initSettings } from './lib/settings'
import { initCustomFields } from './lib/custom-fields'
2021-04-09 17:29:44 +00:00
import { initRouters } from './lib/routers/index'
import { initFederation } from './lib/federation/init'
import { initChannelConfiguration } from './lib/configuration/channel/init'
2023-07-05 16:33:30 +00:00
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 { RoomChannel } from './lib/room-channel'
2023-09-15 15:55:07 +00:00
import { BotConfiguration } from './lib/configuration/bot'
2021-04-09 09:22:46 +00:00
import decache from 'decache'
2021-06-02 10:48:24 +00:00
// FIXME: Peertube unregister don't have any parameter.
// Using this global variable to fix this, so we can use helpers to unregister.
2021-04-13 15:13:41 +00:00
let OPTIONS: RegisterServerOptions | undefined
2021-04-07 14:52:38 +00:00
2021-04-09 17:39:40 +00:00
async function register (options: RegisterServerOptions): Promise<any> {
2021-04-13 15:13:41 +00:00
OPTIONS = options
const logger = options.peertubeHelpers.logger
// This is a trick to check that peertube is at least in version 3.2.0
if (!options.peertubeHelpers.plugin) {
throw new Error('Your peertube version is not correct. This plugin is not compatible with Peertube < 3.2.0.')
}
// First: load languages files, so we can localize strings.
await loadLoc()
2023-09-15 15:55:07 +00:00
// Then load the BotConfiguration singleton
await BotConfiguration.initSingleton(options)
// Then load the RoomChannel singleton
const roomChannelSingleton = await RoomChannel.initSingleton(options)
// roomChannelNeedsDataInit: if true, means that the data file does not exist (or is invalid), so we must initiate it
const roomChannelNeedsDataInit = !await roomChannelSingleton.readData()
await migrateSettings(options)
2021-04-09 17:39:40 +00:00
await initSettings(options)
await initCustomFields(options)
2021-04-09 17:39:40 +00:00
await initRouters(options)
await initFederation(options)
await initChannelConfiguration(options)
2023-07-05 16:33:30 +00:00
await initRSS(options)
try {
await prepareProsody(options)
await ensureProsodyRunning(options)
if (roomChannelNeedsDataInit) {
logger.info('The RoomChannel singleton has not found any data, we must rebuild')
// no need to wait here, can be done without await.
roomChannelSingleton.rebuildData().then(
() => { logger.info('RoomChannel singleton rebuild done') },
(reason) => { logger.error('RoomChannel singleton rebuild failed: ' + (reason as string)) }
)
}
} catch (error) {
options.peertubeHelpers.logger.error('Error when launching Prosody: ' + (error as string))
}
2019-07-16 14:39:36 +00:00
}
async function unregister (): Promise<any> {
2021-04-13 15:13:41 +00:00
if (OPTIONS) {
try {
await ensureProsodyNotRunning(OPTIONS)
} catch (error) {
OPTIONS.peertubeHelpers.logger.error('Error when trying to unload Prosody: ' + (error as string))
}
}
unloadDebugMode()
await RoomChannel.destroySingleton()
2023-09-18 10:23:35 +00:00
await BotConfiguration.destroySingleton()
const module = __filename
2021-04-13 15:13:41 +00:00
OPTIONS?.peertubeHelpers.logger.info(`Unloading module ${module}...`)
2021-06-02 10:48:24 +00:00
// Peertube calls decache(plugin) on register, not unregister.
// Will do here, to release memory.
decache(module)
2021-04-13 15:13:41 +00:00
OPTIONS?.peertubeHelpers.logger.info(`Successfully unloaded the module ${module}`)
OPTIONS = undefined
2019-07-16 14:39:36 +00:00
}
module.exports = {
register,
unregister
}