From 85a546d09a0579c9e2714245eeb781d42c32ab13 Mon Sep 17 00:00:00 2001 From: David Teller Date: Wed, 11 Jan 2023 14:22:36 +0100 Subject: [PATCH] Fix: Make sure that config.bot.displayName is always set --- src/appservice/AppService.ts | 8 ++++---- src/appservice/MjolnirManager.ts | 5 ++--- src/appservice/cli.ts | 5 +++-- src/appservice/config/config.ts | 8 ++++++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/appservice/AppService.ts b/src/appservice/AppService.ts index 23d56b4..90f559c 100644 --- a/src/appservice/AppService.ts +++ b/src/appservice/AppService.ts @@ -18,7 +18,7 @@ import { AppServiceRegistration, Bridge, Request, WeakEvent, BridgeContext, Matr import { MjolnirManager } from ".//MjolnirManager"; import { DataStore, PgDataStore } from ".//datastore"; import { Api } from "./Api"; -import { IConfig } from "./config/config"; +import { IConfig as IAppserviceConfig } from "./config/config"; import { AccessControl } from "./AccessControl"; import { OpenMetrics } from "../webapis/OpenMetrics"; @@ -37,7 +37,7 @@ export class MjolnirAppService { * use `makeMjolnirAppService`. */ private constructor( - public readonly config: IConfig, + public readonly config: IAppserviceConfig, public readonly bridge: Bridge, private readonly mjolnirManager: MjolnirManager, private readonly accessControl: AccessControl, @@ -54,7 +54,7 @@ export class MjolnirAppService { * @param registrationFilePath A file path to the registration file to read the namespace and tokens from. * @returns A new `MjolnirAppService`. */ - public static async makeMjolnirAppService(config: IConfig, dataStore: DataStore, registrationFilePath: string) { + public static async makeMjolnirAppService(config: IAppserviceConfig, dataStore: DataStore, registrationFilePath: string) { const bridge = new Bridge({ homeserverUrl: config.homeserver.url, domain: config.homeserver.domain, @@ -92,7 +92,7 @@ export class MjolnirAppService { * @param config The parsed configuration file. * @param registrationFilePath A path to their homeserver registration file. */ - public static async run(port: number, config: IConfig, registrationFilePath: string): Promise { + public static async run(port: number, config: IAppserviceConfig, registrationFilePath: string): Promise { Logger.configure(config.logging ?? { console: "debug" }); const dataStore = new PgDataStore(config.db.connectionString); await dataStore.init(); diff --git a/src/appservice/MjolnirManager.ts b/src/appservice/MjolnirManager.ts index c373660..9de9826 100644 --- a/src/appservice/MjolnirManager.ts +++ b/src/appservice/MjolnirManager.ts @@ -65,9 +65,8 @@ export class MjolnirManager { intentListener, ); await managedMjolnir.start(); - if (this.config.bot.displayName) { - await client.setDisplayName(this.config.bot.displayName); - } + const displayName = this.config?.bot?.displayName || "Moderation Bot"; + await client.setDisplayName(displayName); this.perMjolnirId.set(mjolnirUserId, managedMjolnir); this.perOwnerId.set(requestingUserId, managedMjolnir); return managedMjolnir; diff --git a/src/appservice/cli.ts b/src/appservice/cli.ts index 762b808..b9bf25e 100644 --- a/src/appservice/cli.ts +++ b/src/appservice/cli.ts @@ -1,6 +1,6 @@ import { Cli } from "matrix-appservice-bridge"; import { MjolnirAppService } from "./AppService"; -import { IConfig } from "./config/config"; +import { IConfig as IAppserviceConfig, addDefaults } from "./config/config"; import * as utils from "../utils"; /** @@ -17,10 +17,11 @@ const cli = new Cli({ }, generateRegistration: MjolnirAppService.generateRegistration, run: async function(port: number) { - const config: IConfig | null = cli.getConfig() as any; + const config: IAppserviceConfig | null = cli.getConfig() as any; if (config === null) { throw new Error("Couldn't load config"); } + addDefaults(config); utils.initializeSentry(config); utils.initializeGlobalPerformanceMetrics(config); await MjolnirAppService.run(port, config, cli.getRegistrationFilePath()); diff --git a/src/appservice/config/config.ts b/src/appservice/config/config.ts index 9c0ddf0..80b8099 100644 --- a/src/appservice/config/config.ts +++ b/src/appservice/config/config.ts @@ -85,11 +85,15 @@ export function read(configPath: string): IConfig { const content = fs.readFileSync(configPath, "utf8"); const parsed = load(content); const config = (parsed as object) as IConfig; + addDefaults(config); + return config; +} + +export function addDefaults(config: IConfig) { if (!config.bot) { config.bot = {}; } if (!config.bot.displayName) { config.bot.displayName = "Moderation Bot"; } - return config; -} +} \ No newline at end of file