feat(#379): Register MatrixService in BridgeModule with conditional loading
Some checks failed
ci/woodpecker/push/api Pipeline failed
Some checks failed
ci/woodpecker/push/api Pipeline failed
- Add CHAT_PROVIDERS injection token for bridge-agnostic access - Conditional loading based on env vars (DISCORD_BOT_TOKEN, MATRIX_ACCESS_TOKEN) - Both bridges can run simultaneously - No crash if neither bridge is configured - Tests verify all configuration combinations Refs #379
This commit is contained in:
@@ -1,16 +1,55 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { Logger, Module } from "@nestjs/common";
|
||||
import { DiscordService } from "./discord/discord.service";
|
||||
import { MatrixService } from "./matrix/matrix.service";
|
||||
import { StitcherModule } from "../stitcher/stitcher.module";
|
||||
import { CHAT_PROVIDERS } from "./bridge.constants";
|
||||
import type { IChatProvider } from "./interfaces";
|
||||
|
||||
const logger = new Logger("BridgeModule");
|
||||
|
||||
/**
|
||||
* Bridge Module - Chat platform integrations
|
||||
*
|
||||
* Provides integration with chat platforms (Discord, Slack, Matrix, etc.)
|
||||
* Provides integration with chat platforms (Discord, Matrix, etc.)
|
||||
* for controlling Mosaic Stack via chat commands.
|
||||
*
|
||||
* Both services are always registered as providers, but the CHAT_PROVIDERS
|
||||
* injection token only includes bridges whose environment variables are set:
|
||||
* - Discord: included when DISCORD_BOT_TOKEN is set
|
||||
* - Matrix: included when MATRIX_ACCESS_TOKEN is set
|
||||
*
|
||||
* Both bridges can run simultaneously, and no error occurs if neither is configured.
|
||||
* Consumers should inject CHAT_PROVIDERS for bridge-agnostic access to all active providers.
|
||||
*/
|
||||
@Module({
|
||||
imports: [StitcherModule],
|
||||
providers: [DiscordService],
|
||||
exports: [DiscordService],
|
||||
providers: [
|
||||
DiscordService,
|
||||
MatrixService,
|
||||
{
|
||||
provide: CHAT_PROVIDERS,
|
||||
useFactory: (discord: DiscordService, matrix: MatrixService): IChatProvider[] => {
|
||||
const providers: IChatProvider[] = [];
|
||||
|
||||
if (process.env.DISCORD_BOT_TOKEN) {
|
||||
providers.push(discord);
|
||||
logger.log("Discord bridge enabled (DISCORD_BOT_TOKEN detected)");
|
||||
}
|
||||
|
||||
if (process.env.MATRIX_ACCESS_TOKEN) {
|
||||
providers.push(matrix);
|
||||
logger.log("Matrix bridge enabled (MATRIX_ACCESS_TOKEN detected)");
|
||||
}
|
||||
|
||||
if (providers.length === 0) {
|
||||
logger.warn("No chat bridges configured. Set DISCORD_BOT_TOKEN or MATRIX_ACCESS_TOKEN.");
|
||||
}
|
||||
|
||||
return providers;
|
||||
},
|
||||
inject: [DiscordService, MatrixService],
|
||||
},
|
||||
],
|
||||
exports: [DiscordService, MatrixService, CHAT_PROVIDERS],
|
||||
})
|
||||
export class BridgeModule {}
|
||||
|
||||
Reference in New Issue
Block a user