2023-07-05 16:33:30 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2021-06-02 17:16:27 +00:00
|
|
|
import { migrateSettings } from './lib/migration/settings'
|
2021-04-08 00:43:13 +00:00
|
|
|
import { initSettings } from './lib/settings'
|
2021-06-08 16:08:58 +00:00
|
|
|
import { initCustomFields } from './lib/custom-fields'
|
2021-04-09 17:29:44 +00:00
|
|
|
import { initRouters } from './lib/routers/index'
|
2023-04-19 17:07:08 +00:00
|
|
|
import { initFederation } from './lib/federation/init'
|
2023-09-08 18:00:14 +00:00
|
|
|
import { initChannelConfiguration } from './lib/configuration/channel/init'
|
2023-07-05 16:33:30 +00:00
|
|
|
import { initRSS } from './lib/rss/init'
|
2022-11-14 15:54:08 +00:00
|
|
|
import { prepareProsody, ensureProsodyRunning, ensureProsodyNotRunning } from './lib/prosody/ctl'
|
2023-05-23 10:39:05 +00:00
|
|
|
import { unloadDebugMode } from './lib/debug'
|
2023-06-12 17:26:28 +00:00
|
|
|
import { loadLoc } from './lib/loc'
|
2023-09-08 18:00:14 +00:00
|
|
|
import { RoomChannel } from './lib/room-channel'
|
2023-09-15 15:55:07 +00:00
|
|
|
import { BotConfiguration } from './lib/configuration/bot'
|
2023-09-18 16:53:07 +00:00
|
|
|
import { BotsCtl } from './lib/bots/ctl'
|
2021-04-09 09:22:46 +00:00
|
|
|
import decache from 'decache'
|
2021-04-08 01:47:55 +00:00
|
|
|
|
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
|
2023-09-08 18:00:14 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
2021-04-08 01:47:55 +00:00
|
|
|
|
2021-06-03 09:53:18 +00:00
|
|
|
// 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.')
|
|
|
|
}
|
|
|
|
|
2023-06-12 17:26:28 +00:00
|
|
|
// 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)
|
2023-09-08 18:00:14 +00:00
|
|
|
// 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()
|
2023-06-12 17:26:28 +00:00
|
|
|
|
2023-09-18 16:53:07 +00:00
|
|
|
// BotsCtl.initSingleton() will force reload the bots conf files, so must be done before generating Prosody Conf.
|
|
|
|
await BotsCtl.initSingleton(options)
|
|
|
|
|
2021-06-02 17:16:27 +00:00
|
|
|
await migrateSettings(options)
|
|
|
|
|
2021-04-09 17:39:40 +00:00
|
|
|
await initSettings(options)
|
2021-06-08 16:08:58 +00:00
|
|
|
await initCustomFields(options)
|
2021-04-09 17:39:40 +00:00
|
|
|
await initRouters(options)
|
2023-04-19 17:07:08 +00:00
|
|
|
await initFederation(options)
|
2023-09-08 18:00:14 +00:00
|
|
|
await initChannelConfiguration(options)
|
2023-07-05 16:33:30 +00:00
|
|
|
await initRSS(options)
|
2023-05-24 04:11:10 +00:00
|
|
|
|
2021-09-14 14:49:11 +00:00
|
|
|
try {
|
2022-11-14 15:54:08 +00:00
|
|
|
await prepareProsody(options)
|
2021-09-14 14:49:11 +00:00
|
|
|
await ensureProsodyRunning(options)
|
2023-09-08 18:00:14 +00:00
|
|
|
|
2023-09-18 16:53:07 +00:00
|
|
|
let preBotPromise: Promise<void>
|
2023-09-08 18:00:14 +00:00
|
|
|
if (roomChannelNeedsDataInit) {
|
2023-09-11 16:13:57 +00:00
|
|
|
logger.info('The RoomChannel singleton has not found any data, we must rebuild')
|
2023-09-08 18:00:14 +00:00
|
|
|
// no need to wait here, can be done without await.
|
2023-09-18 16:53:07 +00:00
|
|
|
preBotPromise = roomChannelSingleton.rebuildData().then(
|
2023-09-08 18:00:14 +00:00
|
|
|
() => { logger.info('RoomChannel singleton rebuild done') },
|
|
|
|
(reason) => { logger.error('RoomChannel singleton rebuild failed: ' + (reason as string)) }
|
|
|
|
)
|
2023-09-18 16:53:07 +00:00
|
|
|
} else {
|
|
|
|
preBotPromise = Promise.resolve()
|
2023-09-08 18:00:14 +00:00
|
|
|
}
|
2023-09-18 16:53:07 +00:00
|
|
|
|
|
|
|
// Don't need to wait for the bot to start.
|
|
|
|
preBotPromise.then(
|
|
|
|
async () => {
|
|
|
|
await BotsCtl.singleton().start()
|
|
|
|
},
|
|
|
|
() => {}
|
|
|
|
)
|
2021-09-14 14:49:11 +00:00
|
|
|
} catch (error) {
|
2021-12-01 13:47:16 +00:00
|
|
|
options.peertubeHelpers.logger.error('Error when launching Prosody: ' + (error as string))
|
2021-09-14 14:49:11 +00:00
|
|
|
}
|
2019-07-16 14:39:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 15:20:28 +00:00
|
|
|
async function unregister (): Promise<any> {
|
2023-09-18 16:53:07 +00:00
|
|
|
try {
|
|
|
|
await BotsCtl.destroySingleton()
|
|
|
|
} catch (_error) {} // BotsCtl will log errors.
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 10:39:05 +00:00
|
|
|
unloadDebugMode()
|
|
|
|
|
2023-09-08 18:00:14 +00:00
|
|
|
await RoomChannel.destroySingleton()
|
2023-09-18 10:23:35 +00:00
|
|
|
await BotConfiguration.destroySingleton()
|
2023-09-08 18:00:14 +00:00
|
|
|
|
2021-04-08 01:47:55 +00:00
|
|
|
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.
|
2021-04-08 01:47:55 +00:00
|
|
|
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
|
|
|
|
}
|