Skip to content

Commit

Permalink
AutoAck selfEvents before ignoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamBergamin committed Nov 11, 2024
1 parent 7048251 commit 43a14fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ export const onlyViewActions: Middleware<AnyMiddlewareArgs> = async (args) => {
* Middleware that auto acknowledges the request received
*/
export const autoAcknowledge: Middleware<AnyMiddlewareArgs> = async (args) => {
await safelyAcknowledge(args);
await args.next();
};

export const safelyAcknowledge: Middleware<AnyMiddlewareArgs> = async (args) => {
if ('ack' in args && args.ack !== undefined) {
await args.ack();
}
await args.next();
};

/**
Expand Down Expand Up @@ -318,6 +322,7 @@ export const ignoreSelf: Middleware<AnyMiddlewareArgs> = async (args) => {
const { message } = args;
// Look for an event that is identified as a bot message from the same bot ID as this app, and return to skip
if (message.subtype === 'bot_message' && message.bot_id === botId) {
await safelyAcknowledge(args);
return;
}
}
Expand All @@ -332,6 +337,7 @@ export const ignoreSelf: Middleware<AnyMiddlewareArgs> = async (args) => {
args.event.user === botUserId &&
!eventsWhichShouldBeKept.includes(args.event.type)
) {
await safelyAcknowledge(args);
return;
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/unit/helpers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ const ack: AckFn<void> = (_r?) => Promise.resolve();
export function wrapMiddleware<Args extends AnyMiddlewareArgs>(
args: Args,
ctx?: Context,
): Args & AllMiddlewareArgs & { next: SinonSpy } {
): Args & AllMiddlewareArgs & { next: SinonSpy; ack: SinonSpy } {
const wrapped = {
...args,
context: ctx || { isEnterpriseInstall: false },
logger: createFakeLogger(),
client: new WebClient(),
next: sinon.fake(),
ack: sinon.fake(),
};
return wrapped;
}
Expand Down
5 changes: 5 additions & 0 deletions test/unit/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ describe('Built-in global middleware', () => {
const args = wrapMiddleware(createDummyCommandMiddlewareArgs(), ctx);
await builtins.ignoreSelf(args);
sinon.assert.called(args.next);
sinon.assert.notCalled(args.ack);
});

it('should ignore message events identified as a bot message from the same bot ID as this app', async () => {
Expand All @@ -252,20 +253,23 @@ describe('Built-in global middleware', () => {
ctx,
);
await builtins.ignoreSelf(args);
sinon.assert.called(args.ack);
sinon.assert.notCalled(args.next);
});

it('should ignore events with only a botUserId', async () => {
const ctx = { ...dummyContext, botUserId: fakeBotUserId };
const args = wrapMiddleware(createDummyReactionAddedEventMiddlewareArgs({ user: fakeBotUserId }), ctx);
await builtins.ignoreSelf(args);
sinon.assert.called(args.ack);
sinon.assert.notCalled(args.next);
});

it('should ignore events that match own app', async () => {
const ctx = { ...dummyContext, botUserId: fakeBotUserId, botId: fakeBotUserId };
const args = wrapMiddleware(createDummyReactionAddedEventMiddlewareArgs({ user: fakeBotUserId }), ctx);
await builtins.ignoreSelf(args);
sinon.assert.called(args.ack);
sinon.assert.notCalled(args.next);
});

Expand All @@ -279,6 +283,7 @@ describe('Built-in global middleware', () => {

await Promise.all(listOfFakeArgs.map(builtins.ignoreSelf));
for (const args of listOfFakeArgs) {
sinon.assert.notCalled(args.ack);
sinon.assert.called(args.next);
}
});
Expand Down

0 comments on commit 43a14fb

Please sign in to comment.