diff --git a/src/Connections/BaseConnection.ts b/src/Connections/BaseConnection.ts index b734c869..c683988c 100644 --- a/src/Connections/BaseConnection.ts +++ b/src/Connections/BaseConnection.ts @@ -1,4 +1,7 @@ import { FormatUtil } from "../FormatUtil"; +import { IConnectionState } from "."; +import { MatrixClient } from "matrix-bot-sdk"; +import { MatrixEvent } from "../MatrixEvent"; /** * Base connection class from which all connections should extend from. @@ -18,4 +21,35 @@ export abstract class BaseConnection { public get priority(): number { return -1; } -} \ No newline at end of file +} + +export interface ChattyConnectionState extends IConnectionState { + msgtype?: string; +} + +export abstract class ChattyConnection extends BaseConnection { + private msgtype: string|undefined; + constructor( + roomId: string, + stateKey: string, + canonicalStateType: string, + chattyState: ChattyConnectionState, + private client: MatrixClient, + ) { + super(roomId, stateKey, canonicalStateType); + this.msgtype = chattyState.msgtype; + } + + public async onStateUpdate(event: MatrixEvent): Promise { + if (event.content && typeof event.content === 'object') { + this.msgtype = (event.content as any).msgtype; + } + } + + public async sendMessage(content: any): Promise { + return this.client.sendEvent(this.roomId, 'm.room.message', { + msgtype: this.msgtype || 'm.notice', + ...content + }); + } +} diff --git a/src/Connections/FeedConnection.ts b/src/Connections/FeedConnection.ts index 362501ad..5b37d3ed 100644 --- a/src/Connections/FeedConnection.ts +++ b/src/Connections/FeedConnection.ts @@ -4,17 +4,17 @@ import { BridgeConfigFeeds } from "../Config/Config"; import { FeedEntry, FeedError} from "../feeds/FeedReader"; import LogWrapper from "../LogWrapper"; import { IBridgeStorageProvider } from "../Stores/StorageProvider"; -import { BaseConnection } from "./BaseConnection"; +import { BaseConnection, ChattyConnection, ChattyConnectionState } from "./BaseConnection"; import markdown from "markdown-it"; const log = new LogWrapper("FeedConnection"); const md = new markdown(); -export interface FeedConnectionState extends IConnectionState { +export interface FeedConnectionState extends ChattyConnectionState { url: string; } -export class FeedConnection extends BaseConnection implements IConnection { +export class FeedConnection extends ChattyConnection implements IConnection { static readonly CanonicalEventType = "uk.half-shot.matrix-hookshot.feed"; static readonly EventTypes = [ FeedConnection.CanonicalEventType ]; private hasError = false; @@ -31,7 +31,7 @@ export class FeedConnection extends BaseConnection implements IConnection { private readonly as: Appservice, private readonly storage: IBridgeStorageProvider ) { - super(roomId, stateKey, FeedConnection.CanonicalEventType) + super(roomId, stateKey, FeedConnection.CanonicalEventType, state, as.botClient); log.info(`Connection ${this.connectionId} created for ${roomId}, ${JSON.stringify(state)}`); } @@ -42,8 +42,7 @@ export class FeedConnection extends BaseConnection implements IConnection { public async handleFeedEntry(entry: FeedEntry): Promise { this.hasError = false; const message = `New post in ${entry.feed.title}: [${entry.title}](${entry.link})` - await this.as.botIntent.sendEvent(this.roomId, { - msgtype: 'm.notice', + await this.sendMessage({ format: "org.matrix.custom.html", formatted_body: md.renderInline(message), body: message, @@ -52,8 +51,7 @@ export class FeedConnection extends BaseConnection implements IConnection { public async handleFeedError(error: FeedError): Promise { if (!this.hasError) { - await this.as.botIntent.sendEvent(this.roomId, { - msgtype: 'm.notice', + await this.sendMessage({ format: 'm.text', body: `Error fetching ${this.feedUrl}: ${error.cause.message}` });