diff --git a/src/App/BridgeApp.ts b/src/App/BridgeApp.ts index 909bb151c..703fcde1f 100644 --- a/src/App/BridgeApp.ts +++ b/src/App/BridgeApp.ts @@ -7,6 +7,7 @@ import { UserNotificationWatcher } from "../Notifications/UserNotificationWatche import { ListenerService } from "../ListenerService"; import { Logger } from "matrix-appservice-bridge"; import { LogService } from "matrix-bot-sdk"; +import { getAppservice } from "../appservice"; Logger.configure({console: "info"}); const log = new Logger("App"); @@ -25,14 +26,16 @@ async function start() { }); LogService.setLogger(Logger.botSdkLogger); + const {appservice, storage} = getAppservice(config, registration); + if (config.queue.monolithic) { - const matrixSender = new MatrixSender(config, registration); + const matrixSender = new MatrixSender(config, appservice); matrixSender.listen(); const userNotificationWatcher = new UserNotificationWatcher(config); userNotificationWatcher.start(); } - const bridgeApp = new Bridge(config, registration, listener); + const bridgeApp = new Bridge(config, listener, appservice, storage); process.once("SIGTERM", () => { log.error("Got SIGTERM"); diff --git a/src/App/MatrixSenderApp.ts b/src/App/MatrixSenderApp.ts index 1198c98b2..e0881f98e 100644 --- a/src/App/MatrixSenderApp.ts +++ b/src/App/MatrixSenderApp.ts @@ -4,6 +4,7 @@ import { Logger } from "matrix-appservice-bridge"; import Metrics from "../Metrics"; import { ListenerService } from "../ListenerService"; import { LogService } from "matrix-bot-sdk"; +import { getAppservice } from "../appservice"; const log = new Logger("App"); @@ -21,7 +22,7 @@ async function start() { }); LogService.setLogger(Logger.botSdkLogger); const listener = new ListenerService(config.listeners); - const sender = new MatrixSender(config, registration); + const sender = new MatrixSender(config, getAppservice(config, registration).appservice); if (config.metrics) { if (!config.metrics.port) { log.warn(`Not running metrics for service, no port specified`); diff --git a/src/Bridge.ts b/src/Bridge.ts index 5cb750eb8..4efbaab24 100644 --- a/src/Bridge.ts +++ b/src/Bridge.ts @@ -1,6 +1,6 @@ import { AdminAccountData } from "./AdminRoomCommandHandler"; import { AdminRoom, BRIDGE_ROOM_TYPE, LEGACY_BRIDGE_ROOM_TYPE } from "./AdminRoom"; -import { Appservice, IAppserviceRegistration, RichRepliesPreprocessor, IRichReplyMetadata, StateEvent, MatrixClient, EventKind, PowerLevelsEvent } from "matrix-bot-sdk"; +import { Appservice, RichRepliesPreprocessor, IRichReplyMetadata, StateEvent, EventKind, PowerLevelsEvent } from "matrix-bot-sdk"; import { BridgeConfig, BridgePermissionLevel, GitLabInstance } from "./Config/Config"; import { BridgeWidgetApi } from "./Widgets/BridgeWidgetApi"; import { CommentProcessor } from "./CommentProcessor"; @@ -14,14 +14,12 @@ import { IGitLabWebhookIssueStateEvent, IGitLabWebhookMREvent, IGitLabWebhookNot import { JiraIssueEvent, JiraIssueUpdatedEvent, JiraVersionEvent } from "./Jira/WebhookTypes"; import { JiraOAuthResult } from "./Jira/Types"; import { MatrixEvent, MatrixMemberContent, MatrixMessageContent } from "./MatrixEvent"; -import { MemoryStorageProvider } from "./Stores/MemoryStorageProvider"; import { MessageQueue, createMessageQueue } from "./MessageQueue"; import { MessageSenderClient } from "./MatrixSender"; import { NotifFilter, NotificationFilterStateContent } from "./NotificationFilters"; import { NotificationProcessor } from "./NotificationsProcessor"; import { NotificationsEnableEvent, NotificationsDisableEvent } from "./Webhooks"; import { GitHubOAuthToken, GitHubOAuthTokenResponse, ProjectsGetResponseData } from "./Github/Types"; -import { RedisStorageProvider } from "./Stores/RedisStorageProvider"; import { retry } from "./PromiseUtil"; import { UserNotificationsEvent } from "./Notifications/UserNotificationWatcher"; import { UserTokenStore } from "./UserTokenStore"; @@ -36,7 +34,6 @@ import Metrics from "./Metrics"; import { FigmaEvent, ensureFigmaWebhooks } from "./figma"; import { ListenerService } from "./ListenerService"; import { SetupConnection } from "./Connections/SetupConnection"; -import { getAppservice } from "./appservice"; import { JiraOAuthRequestCloud, JiraOAuthRequestOnPrem, JiraOAuthRequestResult } from "./Jira/OAuth"; import { GenericWebhookEvent, GenericWebhookEventResult } from "./generic/types"; import { SetupWidget } from "./Widgets/SetupWidget"; @@ -44,8 +41,6 @@ import { FeedEntry, FeedError, FeedReader, FeedSuccess } from "./feeds/FeedReade const log = new Logger("Bridge"); export class Bridge { - private readonly as: Appservice; - private readonly storage: IBridgeStorageProvider; private readonly messageClient: MessageSenderClient; private readonly queue: MessageQueue; private readonly commentProcessor: CommentProcessor; @@ -61,16 +56,12 @@ export class Bridge { private ready = false; - constructor(private config: BridgeConfig, private registration: IAppserviceRegistration, private readonly listener: ListenerService) { - if (this.config.queue.host && this.config.queue.port) { - log.info(`Initialising Redis storage (on ${this.config.queue.host}:${this.config.queue.port})`); - this.storage = new RedisStorageProvider(this.config.queue.host, this.config.queue.port); - } else { - log.info('Initialising memory storage'); - this.storage = new MemoryStorageProvider(); - } - this.as = getAppservice(this.config, this.registration, this.storage); - Metrics.registerMatrixSdkMetrics(this.as); + constructor( + private config: BridgeConfig, + private readonly listener: ListenerService, + private readonly as: Appservice, + private readonly storage: IBridgeStorageProvider, + ) { this.queue = createMessageQueue(this.config.queue); this.messageClient = new MessageSenderClient(this.queue); this.commentProcessor = new CommentProcessor(this.as, this.config.bridge.mediaUrl || this.config.bridge.url); diff --git a/src/MatrixSender.ts b/src/MatrixSender.ts index f1f37523e..8b1dbb733 100644 --- a/src/MatrixSender.ts +++ b/src/MatrixSender.ts @@ -1,10 +1,8 @@ import { BridgeConfig } from "./Config/Config"; import { MessageQueue, createMessageQueue } from "./MessageQueue"; -import { Appservice, IAppserviceRegistration, MemoryStorageProvider } from "matrix-bot-sdk"; +import { Appservice, Intent } from "matrix-bot-sdk"; import { Logger } from "matrix-appservice-bridge"; import { v4 as uuid } from "uuid"; -import { getAppservice } from "./appservice"; -import Metrics from "./Metrics"; export interface IMatrixSendMessage { sender: string|null; @@ -26,11 +24,8 @@ const log = new Logger("MatrixSender"); export class MatrixSender { private mq: MessageQueue; - private as: Appservice; - constructor(private config: BridgeConfig, registration: IAppserviceRegistration) { + constructor(private config: BridgeConfig, private readonly as: Appservice) { this.mq = createMessageQueue(this.config.queue); - this.as = getAppservice(config, registration, new MemoryStorageProvider()); - Metrics.registerMatrixSdkMetrics(this.as); } public listen() { diff --git a/src/appservice.ts b/src/appservice.ts index 17e963af0..ff7873928 100644 --- a/src/appservice.ts +++ b/src/appservice.ts @@ -1,10 +1,25 @@ -import { Appservice, IAppserviceRegistration, IAppserviceStorageProvider, RustSdkAppserviceCryptoStorageProvider } from "matrix-bot-sdk"; +import { Logger } from "matrix-appservice-bridge"; +import { Appservice, IAppserviceRegistration, RustSdkAppserviceCryptoStorageProvider } from "matrix-bot-sdk"; import { BridgeConfig } from "./Config/Config"; +import Metrics from "./Metrics"; +import { MemoryStorageProvider } from "./Stores/MemoryStorageProvider"; +import { RedisStorageProvider } from "./Stores/RedisStorageProvider"; +import { IBridgeStorageProvider } from "./Stores/StorageProvider"; +const log = new Logger("Appservice"); + +export function getAppservice(config: BridgeConfig, registration: IAppserviceRegistration) { + let storage: IBridgeStorageProvider; + if (config.queue.host && config.queue.port) { + log.info(`Initialising Redis storage (on ${config.queue.host}:${config.queue.port})`); + storage = new RedisStorageProvider(config.queue.host, config.queue.port); + } else { + log.info('Initialising memory storage'); + storage = new MemoryStorageProvider(); + } -export function getAppservice(config: BridgeConfig, registration: IAppserviceRegistration, storage: IAppserviceStorageProvider) { const cryptoStorage = config.encryption?.storagePath ? new RustSdkAppserviceCryptoStorageProvider(config.encryption.storagePath) : undefined; - return new Appservice({ + const appservice = new Appservice({ homeserverName: config.bridge.domain, homeserverUrl: config.bridge.url, port: config.bridge.port, @@ -27,4 +42,8 @@ export function getAppservice(config: BridgeConfig, registration: IAppserviceReg }, cryptoStorage: cryptoStorage, }); + + Metrics.registerMatrixSdkMetrics(appservice); + + return {appservice, storage}; } \ No newline at end of file