Fix: Make sure that config.bot.displayName is always set

This commit is contained in:
David Teller 2023-01-11 14:22:36 +01:00
parent 7be00c1c3c
commit 85a546d09a
4 changed files with 15 additions and 11 deletions

View File

@ -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<MjolnirAppService> {
public static async run(port: number, config: IAppserviceConfig, registrationFilePath: string): Promise<MjolnirAppService> {
Logger.configure(config.logging ?? { console: "debug" });
const dataStore = new PgDataStore(config.db.connectionString);
await dataStore.init();

View File

@ -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;

View File

@ -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());

View File

@ -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;
}