mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 21:19:13 +00:00
Merge pull request #87 from Half-Shot/hs/ensure-webhooks-store-hookId-internally
Ensure webhook hookIds are stored internally
This commit is contained in:
commit
d2b1ff64da
@ -8,7 +8,7 @@ import { Appservice, StateEvent } from "matrix-bot-sdk";
|
||||
import { CommentProcessor } from "./CommentProcessor";
|
||||
import { BridgeConfig, GitLabInstance } from "./Config/Config";
|
||||
import { GitHubDiscussionConnection, GitHubDiscussionSpace, GitHubIssueConnection, GitHubProjectConnection, GitHubRepoConnection, GitHubUserSpace, GitLabIssueConnection, GitLabRepoConnection, IConnection } from "./Connections";
|
||||
import { GenericHookConnection } from "./Connections/GenericHook";
|
||||
import { GenericHookAccountData, GenericHookConnection } from "./Connections/GenericHook";
|
||||
import { JiraProjectConnection } from "./Connections/JiraProject";
|
||||
import { GithubInstance } from "./Github/GithubInstance";
|
||||
import { GitLabClient } from "./Gitlab/Client";
|
||||
@ -16,6 +16,7 @@ import { JiraProject } from "./Jira/Types";
|
||||
import LogWrapper from "./LogWrapper";
|
||||
import { MessageSenderClient } from "./MatrixSender";
|
||||
import { UserTokenStore } from "./UserTokenStore";
|
||||
import {v4 as uuid} from "uuid";
|
||||
|
||||
const log = new LogWrapper("ConnectionManager");
|
||||
|
||||
@ -137,9 +138,18 @@ export class ConnectionManager {
|
||||
}
|
||||
|
||||
if (GenericHookConnection.EventTypes.includes(state.type) && this.config.generic?.enabled) {
|
||||
// Generic hooks store the hookId in the account data
|
||||
let acctData = await this.as.botClient.getSafeRoomAccountData<GenericHookAccountData|null>(GenericHookConnection.CanonicalEventType, roomId);
|
||||
if (!acctData) {
|
||||
log.info(`hookId for ${roomId} not set, setting`);
|
||||
acctData = { hookId: uuid() };
|
||||
await this.as.botClient.setRoomAccountData(GenericHookConnection.CanonicalEventType, roomId, acctData);
|
||||
await this.as.botClient.sendStateEvent(roomId, GenericHookConnection.CanonicalEventType, state.stateKey, {...state.content, hookId: acctData.hookId });
|
||||
}
|
||||
return new GenericHookConnection(
|
||||
roomId,
|
||||
state.content,
|
||||
acctData,
|
||||
state.stateKey,
|
||||
this.messageClient,
|
||||
this.config.generic.allowJsTransformationFunctions
|
||||
|
@ -6,10 +6,20 @@ import { Script, createContext } from "vm";
|
||||
import { MatrixEvent } from "../MatrixEvent";
|
||||
|
||||
export interface GenericHookConnectionState {
|
||||
/**
|
||||
* This is ONLY used for display purposes.
|
||||
*/
|
||||
hookId: string;
|
||||
transformationFunction?: string;
|
||||
}
|
||||
|
||||
export interface GenericHookAccountData {
|
||||
/**
|
||||
* This is where the true hook ID is kept.
|
||||
*/
|
||||
hookId: string;
|
||||
}
|
||||
|
||||
const log = new LogWrapper("GenericHookConnection");
|
||||
const md = new markdownit();
|
||||
|
||||
@ -28,13 +38,14 @@ export class GenericHookConnection implements IConnection {
|
||||
];
|
||||
|
||||
public get hookId() {
|
||||
return this.state.hookId;
|
||||
return this.accountData.hookId;
|
||||
}
|
||||
|
||||
private transformationFunction?: Script;
|
||||
|
||||
constructor(public readonly roomId: string,
|
||||
private state: GenericHookConnectionState,
|
||||
state: GenericHookConnectionState,
|
||||
private readonly accountData: GenericHookAccountData,
|
||||
private readonly stateKey: string,
|
||||
private messageClient: MessageSenderClient,
|
||||
private readonly allowJSTransformation: boolean = false) {
|
||||
@ -58,11 +69,28 @@ export class GenericHookConnection implements IConnection {
|
||||
}
|
||||
}
|
||||
|
||||
public transformHookData(data: Record<string, unknown>): string {
|
||||
// Supported parameters https://developers.mattermost.com/integrate/incoming-webhooks/#parameters
|
||||
let msg = "";
|
||||
if (typeof data.username === "string") {
|
||||
// Create a matrix user for this person
|
||||
msg = `**${data.username}**: `
|
||||
}
|
||||
if (typeof data.text === "string") {
|
||||
msg = data.text;
|
||||
} else {
|
||||
msg = `Recieved webhook data:\n\n\`\`\`${JSON.stringify(data, undefined, 2)}\`\`\``;
|
||||
}
|
||||
|
||||
// TODO: Transform Slackdown into markdown.
|
||||
return msg;
|
||||
}
|
||||
|
||||
public async onGenericHook(data: Record<string, unknown>) {
|
||||
log.info(`onGenericHook ${this.roomId} ${this.hookId}`);
|
||||
let content: string;
|
||||
if (!this.transformationFunction) {
|
||||
content = `Recieved webhook data:\n\n\`\`\`${JSON.stringify(data)}\`\`\``;
|
||||
content = this.transformHookData(data);
|
||||
} else {
|
||||
try {
|
||||
const context = createContext({data});
|
||||
|
Loading…
x
Reference in New Issue
Block a user