mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 21:19:13 +00:00
Add a variant of BaseConnection that allows for state-configurable msgtypes
This commit is contained in:
parent
a62235e925
commit
362a148ba8
@ -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.
|
||||
@ -19,3 +22,34 @@ export abstract class BaseConnection {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
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<unknown>): Promise<void> {
|
||||
if (event.content && typeof event.content === 'object') {
|
||||
this.msgtype = (event.content as any).msgtype;
|
||||
}
|
||||
}
|
||||
|
||||
public async sendMessage(content: any): Promise<string> {
|
||||
return this.client.sendEvent(this.roomId, 'm.room.message', {
|
||||
msgtype: this.msgtype || 'm.notice',
|
||||
...content
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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<void> {
|
||||
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<void> {
|
||||
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}`
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user