Skip to content

Commit

Permalink
Allow configuration for messages webhooks
Browse files Browse the repository at this point in the history
- Add a new `sendMessages` field to WebHook database table
  • Loading branch information
Uxio0 committed Sep 12, 2023
1 parent 1813d87 commit 651dbfb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/datasources/migrations/1694512065902-SendMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class SendMessages1694512065902 implements MigrationInterface {
name = 'SendMessages1694512065902';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "webhook" ADD "sendMessages" boolean NOT NULL DEFAULT true`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "webhook" DROP COLUMN "sendMessages"`);
}
}
15 changes: 15 additions & 0 deletions src/routes/webhook/entities/webhook.entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Webhook entity', () => {
webhook.sendTokenTransfers = true;
webhook.sendModuleTransactions = true;
webhook.sendSafeCreations = true;
webhook.sendMessages = true;
});

it('If chain is set, only those chain messages will be sent', async () => {
Expand Down Expand Up @@ -105,4 +106,18 @@ describe('Webhook entity', () => {
webhook.sendSafeCreations = false;
expect(webhook.isEventRelevant(txServiceEvent)).toBe(false);
});

it('MESSAGE_CREATED should not be relevant if sendMessages is disabled', async () => {
txServiceEvent.type = 'MESSAGE_CREATED' as TxServiceEventType;
expect(webhook.isEventRelevant(txServiceEvent)).toBe(true);
webhook.sendMessages = false;
expect(webhook.isEventRelevant(txServiceEvent)).toBe(false);
});

it('MESSAGE_CONFIRMATION should not be relevant if sendMessages is disabled', async () => {
txServiceEvent.type = 'MESSAGE_CONFIRMATION' as TxServiceEventType;
expect(webhook.isEventRelevant(txServiceEvent)).toBe(true);
webhook.sendMessages = false;
expect(webhook.isEventRelevant(txServiceEvent)).toBe(false);
});
});
10 changes: 9 additions & 1 deletion src/routes/webhook/entities/webhook.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ export class Webhook extends BaseEntity {
@Column({ default: true })
sendSafeCreations: boolean;

@Column({ default: true })
sendMessages: boolean;

/**
* Check if event chainId matches the one of the webhook (everything will match if webhook chains are empty). Check if event
* type matches the flags enabled for the webhook
* @param message
* @returns true if event is relevant.
*/
isEventRelevant(message: TxServiceEvent): boolean {
const chainMatches: boolean =
this.chains.length === 0 || this.chains.includes(message.chainId);
return (
(this.chains.length === 0 || this.chains.includes(message.chainId)) &&
chainMatches &&
((this.sendConfirmations &&
(message.type === 'NEW_CONFIRMATION' ||
message.type === 'CONFIRMATION_REQUEST')) ||
Expand All @@ -62,6 +67,9 @@ export class Webhook extends BaseEntity {
message.type === 'OUTGOING_TOKEN')) ||
(this.sendModuleTransactions &&
message.type === 'MODULE_TRANSACTION') ||
(this.sendMessages &&
(message.type === 'MESSAGE_CREATED' ||
message.type === 'MESSAGE_CONFIRMATION')) ||
(this.sendSafeCreations && message.type === 'SAFE_CREATED'))
);
}
Expand Down

0 comments on commit 651dbfb

Please sign in to comment.