import { pluginShortName } from '../helpers' ; import type { ChatType } from '../../../shared/lib/types' ; async function _migrateChatTypeSetting ( options ) { const peertubeHelpers = options . peertubeHelpers ; const logger = peertubeHelpers . logger ; logger . info ( 'Checking if we need to migrate chat-type' ) ; if ( ! /^[-a-z]+$/ . test ( pluginShortName ) ) { throw new Error ( `Wrong pluginShortName '${ pluginShortName }'` ) ; } const [ results ] = await peertubeHelpers . database . query ( 'SELECT "settings" FROM "plugin"' + ' WHERE "plugin"."name" = :pluginShortName' , { replacements : { pluginShortName } } ) ; if ( ! Array . isArray ( results ) ) { throw new Error ( '_migrateChatTypeSetting: query result is not an array.' ) ; } if ( results . length === 0 ) { logger . error ( 'Plugin not found in database' ) ; return ; } if ( results . length > 1 ) { logger . error ( 'Multiple lines for plugin in database, dont know which one to migrate... Aborting.' ) ; return ; } const settings = results [ 0 ] . settings ; if ( ! settings ) { logger . info ( 'Plugin settings are empty in database, no migration needed.' ) ; return ; } if ( typeof settings !== 'object' ) { logger . error ( 'Plugin settings in database seems to be invalid json' ) ; return ; } if ( 'chat-type' in settings ) { logger . info ( 'The setting chat-type is already here, no need to migrate.' ) ; return ; } logger . info ( 'The setting chat-type is not here, checking if we have to migrate from previous settings...' ) ; let chatType ; if ( settings [ 'chat-use-prosody' ] === true ) { chatType = 'builtin-prosody' ; } else if ( settings [ 'chat-use-builtin' ] === true ) { chatType = 'builtin-converse' ; } else if ( ( ( settings [ 'chat-uri' ] || '' ) as string ) !== '' ) { chatType = 'external-uri' ; } else { logger . info ( 'It seems there was no previous active chat configuration.' ) ; return ; } logger . info ( `We have to set chat-type to value '${ chatType }'.` ) ; await peertubeHelpers . database . query ( 'UPDATE "plugin" ' + ' SET "settings" = "settings" || :value ' + ' WHERE "name" = :pluginShortName' , { replacements : { pluginShortName , value : JSON . stringify ( { 'chat-type' : chatType } ) } } ) ; } async function migrateSettings ( options ) { const logger = options . peertubeHelpers . logger ; logger . info ( 'Checking if there is a migration script to launch...' ) ; await _migrateChatTypeSetting ( options ) ; } export { migrateSettings } ;