peertube-plugin-livechat/server/main.ts

56 lines
1.8 KiB
TypeScript
Raw Normal View History

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'
2021-04-13 15:13:41 +00:00
import { ensureProsodyRunning, ensureProsodyNotRunning } from './lib/prosody/ctl'
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
// 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.')
}
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)
2021-04-13 15:13:41 +00:00
try {
await ensureProsodyRunning(options)
} 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))
}
}
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
}