2024-05-23 11:42:14 +02:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-07-05 18:33:30 +02:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2021-06-02 19:16:27 +02:00
|
|
|
import { migrateSettings } from './lib/migration/settings'
|
2021-04-08 02:43:13 +02:00
|
|
|
import { initSettings } from './lib/settings'
|
2021-06-08 18:08:58 +02:00
|
|
|
import { initCustomFields } from './lib/custom-fields'
|
2021-04-09 19:29:44 +02:00
|
|
|
import { initRouters } from './lib/routers/index'
|
2023-04-19 19:07:08 +02:00
|
|
|
import { initFederation } from './lib/federation/init'
|
2023-09-08 20:00:14 +02:00
|
|
|
import { initChannelConfiguration } from './lib/configuration/channel/init'
|
2023-07-05 18:33:30 +02:00
|
|
|
import { initRSS } from './lib/rss/init'
|
2022-11-14 16:54:08 +01:00
|
|
|
import { prepareProsody, ensureProsodyRunning, ensureProsodyNotRunning } from './lib/prosody/ctl'
|
2023-05-23 12:39:05 +02:00
|
|
|
import { unloadDebugMode } from './lib/debug'
|
2023-06-12 19:26:28 +02:00
|
|
|
import { loadLoc } from './lib/loc'
|
2023-09-08 20:00:14 +02:00
|
|
|
import { RoomChannel } from './lib/room-channel'
|
2023-09-15 17:55:07 +02:00
|
|
|
import { BotConfiguration } from './lib/configuration/bot'
|
2023-09-18 18:53:07 +02:00
|
|
|
import { BotsCtl } from './lib/bots/ctl'
|
2024-04-15 18:29:09 +02:00
|
|
|
import { ExternalAuthOIDC } from './lib/external-auth/oidc'
|
2024-05-17 11:47:37 +02:00
|
|
|
import { migrateMUCAffiliations } from './lib/prosody/migration/migrateV10'
|
2024-09-11 10:16:24 +02:00
|
|
|
import { updateProsodyChannelEmojisRegex } from './lib/prosody/migration/migrateV12'
|
2024-06-04 16:39:25 +02:00
|
|
|
import { Emojis } from './lib/emojis'
|
2024-06-16 19:48:02 +02:00
|
|
|
import { LivechatProsodyAuth } from './lib/prosody/auth'
|
2021-04-09 11:22:46 +02:00
|
|
|
import decache from 'decache'
|
2021-04-08 03:47:55 +02:00
|
|
|
|
2021-06-02 12:48:24 +02: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 17:13:41 +02:00
|
|
|
let OPTIONS: RegisterServerOptions | undefined
|
2021-04-07 16:52:38 +02:00
|
|
|
|
2021-04-09 19:39:40 +02:00
|
|
|
async function register (options: RegisterServerOptions): Promise<any> {
|
2021-04-13 17:13:41 +02:00
|
|
|
OPTIONS = options
|
2023-09-08 20:00:14 +02:00
|
|
|
const logger = options.peertubeHelpers.logger
|
2021-04-08 03:47:55 +02:00
|
|
|
|
2021-06-03 11:53:18 +02: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 19:26:28 +02:00
|
|
|
// First: load languages files, so we can localize strings.
|
|
|
|
await loadLoc()
|
2023-09-15 17:55:07 +02:00
|
|
|
// Then load the BotConfiguration singleton
|
|
|
|
await BotConfiguration.initSingleton(options)
|
2023-09-08 20:00:14 +02: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 19:26:28 +02:00
|
|
|
|
2023-09-18 18:53:07 +02: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 19:16:27 +02:00
|
|
|
await migrateSettings(options)
|
|
|
|
|
2021-04-09 19:39:40 +02:00
|
|
|
await initSettings(options)
|
2024-06-04 16:39:25 +02:00
|
|
|
|
|
|
|
await Emojis.initSingleton(options) // after settings, before routes
|
2024-06-17 15:21:22 +02:00
|
|
|
await LivechatProsodyAuth.initSingleton(options) // after settings, before routes
|
2024-06-04 16:39:25 +02:00
|
|
|
|
2021-06-08 18:08:58 +02:00
|
|
|
await initCustomFields(options)
|
2021-04-09 19:39:40 +02:00
|
|
|
await initRouters(options)
|
2023-04-19 19:07:08 +02:00
|
|
|
await initFederation(options)
|
2023-09-08 20:00:14 +02:00
|
|
|
await initChannelConfiguration(options)
|
2023-07-05 18:33:30 +02:00
|
|
|
await initRSS(options)
|
2023-05-23 23:11:10 -05:00
|
|
|
|
2021-09-14 16:49:11 +02:00
|
|
|
try {
|
2022-11-14 16:54:08 +01:00
|
|
|
await prepareProsody(options)
|
2021-09-14 16:49:11 +02:00
|
|
|
await ensureProsodyRunning(options)
|
2023-09-08 20:00:14 +02:00
|
|
|
|
2023-09-18 18:53:07 +02:00
|
|
|
let preBotPromise: Promise<void>
|
2023-09-08 20:00:14 +02:00
|
|
|
if (roomChannelNeedsDataInit) {
|
2023-09-11 18:13:57 +02:00
|
|
|
logger.info('The RoomChannel singleton has not found any data, we must rebuild')
|
2023-09-08 20:00:14 +02:00
|
|
|
// no need to wait here, can be done without await.
|
2023-09-18 18:53:07 +02:00
|
|
|
preBotPromise = roomChannelSingleton.rebuildData().then(
|
2023-09-08 20:00:14 +02:00
|
|
|
() => { logger.info('RoomChannel singleton rebuild done') },
|
|
|
|
(reason) => { logger.error('RoomChannel singleton rebuild failed: ' + (reason as string)) }
|
|
|
|
)
|
2023-09-18 18:53:07 +02:00
|
|
|
} else {
|
|
|
|
preBotPromise = Promise.resolve()
|
2023-09-08 20:00:14 +02:00
|
|
|
}
|
2023-09-18 18:53:07 +02:00
|
|
|
|
|
|
|
// Don't need to wait for the bot to start.
|
|
|
|
preBotPromise.then(
|
|
|
|
async () => {
|
|
|
|
await BotsCtl.singleton().start()
|
|
|
|
},
|
|
|
|
() => {}
|
|
|
|
)
|
2024-05-17 11:47:37 +02:00
|
|
|
|
|
|
|
// livechat v10.0.0: we must migrate MUC affiliations (but we don't have to wait)
|
|
|
|
// we do this after the preBotPromise, just to avoid doing both at the same time.
|
|
|
|
preBotPromise.then(() => {
|
2024-09-06 11:01:48 +02:00
|
|
|
const p = migrateMUCAffiliations(options).then(
|
2024-05-17 11:47:37 +02:00
|
|
|
() => {},
|
|
|
|
(err) => {
|
|
|
|
logger.error(err)
|
2024-09-06 11:01:48 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// livechat v11.1: we must send channel emojis regexp to Prosody rooms
|
|
|
|
p.finally(
|
|
|
|
() => {
|
|
|
|
updateProsodyChannelEmojisRegex(options).then(
|
|
|
|
() => {},
|
|
|
|
(err: any) => {
|
|
|
|
logger.error(err)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2024-09-07 14:49:27 +02:00
|
|
|
).catch(() => {})
|
2024-05-17 11:47:37 +02:00
|
|
|
}, () => {})
|
2021-09-14 16:49:11 +02:00
|
|
|
} catch (error) {
|
2021-12-01 14:47:16 +01:00
|
|
|
options.peertubeHelpers.logger.error('Error when launching Prosody: ' + (error as string))
|
2021-09-14 16:49:11 +02:00
|
|
|
}
|
2019-07-16 16:39:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:20:28 +02:00
|
|
|
async function unregister (): Promise<any> {
|
2023-09-18 18:53:07 +02:00
|
|
|
try {
|
|
|
|
await BotsCtl.destroySingleton()
|
|
|
|
} catch (_error) {} // BotsCtl will log errors.
|
|
|
|
|
2021-04-13 17:13:41 +02: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 12:39:05 +02:00
|
|
|
unloadDebugMode()
|
|
|
|
|
2023-09-08 20:00:14 +02:00
|
|
|
await RoomChannel.destroySingleton()
|
2023-09-18 12:23:35 +02:00
|
|
|
await BotConfiguration.destroySingleton()
|
2024-04-22 13:03:31 +02:00
|
|
|
await ExternalAuthOIDC.destroySingletons()
|
2024-06-04 16:39:25 +02:00
|
|
|
await Emojis.destroySingleton()
|
2024-06-16 19:48:02 +02:00
|
|
|
await LivechatProsodyAuth.destroySingleton()
|
2023-09-08 20:00:14 +02:00
|
|
|
|
2021-04-08 03:47:55 +02:00
|
|
|
const module = __filename
|
2021-04-13 17:13:41 +02:00
|
|
|
OPTIONS?.peertubeHelpers.logger.info(`Unloading module ${module}...`)
|
2021-06-02 12:48:24 +02:00
|
|
|
// Peertube calls decache(plugin) on register, not unregister.
|
|
|
|
// Will do here, to release memory.
|
2021-04-08 03:47:55 +02:00
|
|
|
decache(module)
|
2021-04-13 17:13:41 +02:00
|
|
|
OPTIONS?.peertubeHelpers.logger.info(`Successfully unloaded the module ${module}`)
|
|
|
|
OPTIONS = undefined
|
2019-07-16 16:39:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
register,
|
|
|
|
unregister
|
|
|
|
}
|