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 { 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.
|
* Base connection class from which all connections should extend from.
|
||||||
@ -19,3 +22,34 @@ export abstract class BaseConnection {
|
|||||||
return -1;
|
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 { FeedEntry, FeedError} from "../feeds/FeedReader";
|
||||||
import LogWrapper from "../LogWrapper";
|
import LogWrapper from "../LogWrapper";
|
||||||
import { IBridgeStorageProvider } from "../Stores/StorageProvider";
|
import { IBridgeStorageProvider } from "../Stores/StorageProvider";
|
||||||
import { BaseConnection } from "./BaseConnection";
|
import { BaseConnection, ChattyConnection, ChattyConnectionState } from "./BaseConnection";
|
||||||
import markdown from "markdown-it";
|
import markdown from "markdown-it";
|
||||||
|
|
||||||
const log = new LogWrapper("FeedConnection");
|
const log = new LogWrapper("FeedConnection");
|
||||||
const md = new markdown();
|
const md = new markdown();
|
||||||
|
|
||||||
export interface FeedConnectionState extends IConnectionState {
|
export interface FeedConnectionState extends ChattyConnectionState {
|
||||||
url: string;
|
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 CanonicalEventType = "uk.half-shot.matrix-hookshot.feed";
|
||||||
static readonly EventTypes = [ FeedConnection.CanonicalEventType ];
|
static readonly EventTypes = [ FeedConnection.CanonicalEventType ];
|
||||||
private hasError = false;
|
private hasError = false;
|
||||||
@ -31,7 +31,7 @@ export class FeedConnection extends BaseConnection implements IConnection {
|
|||||||
private readonly as: Appservice,
|
private readonly as: Appservice,
|
||||||
private readonly storage: IBridgeStorageProvider
|
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)}`);
|
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> {
|
public async handleFeedEntry(entry: FeedEntry): Promise<void> {
|
||||||
this.hasError = false;
|
this.hasError = false;
|
||||||
const message = `New post in ${entry.feed.title}: [${entry.title}](${entry.link})`
|
const message = `New post in ${entry.feed.title}: [${entry.title}](${entry.link})`
|
||||||
await this.as.botIntent.sendEvent(this.roomId, {
|
await this.sendMessage({
|
||||||
msgtype: 'm.notice',
|
|
||||||
format: "org.matrix.custom.html",
|
format: "org.matrix.custom.html",
|
||||||
formatted_body: md.renderInline(message),
|
formatted_body: md.renderInline(message),
|
||||||
body: message,
|
body: message,
|
||||||
@ -52,8 +51,7 @@ export class FeedConnection extends BaseConnection implements IConnection {
|
|||||||
|
|
||||||
public async handleFeedError(error: FeedError): Promise<void> {
|
public async handleFeedError(error: FeedError): Promise<void> {
|
||||||
if (!this.hasError) {
|
if (!this.hasError) {
|
||||||
await this.as.botIntent.sendEvent(this.roomId, {
|
await this.sendMessage({
|
||||||
msgtype: 'm.notice',
|
|
||||||
format: 'm.text',
|
format: 'm.text',
|
||||||
body: `Error fetching ${this.feedUrl}: ${error.cause.message}`
|
body: `Error fetching ${this.feedUrl}: ${error.cause.message}`
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user