Skip to content

Commit

Permalink
Fix: Make sure that config.bot.displayName is always set
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoric authored and David Teller committed Jan 11, 2023
1 parent 7be00c1 commit 9693149
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/appservice/AppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/appservice/MjolnirManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/appservice/cli.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -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());
Expand Down
8 changes: 6 additions & 2 deletions src/appservice/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 9693149

Please sign in to comment.