Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration for messages webhooks #79

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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