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

fix: auto acknowledge self events before ignoring #2329

Open
wants to merge 2 commits into
base: autoack
Choose a base branch
from
Open
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
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 } {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. Args here extends AnyMiddlewareArgs which is a union of different kinds of middleware args. Some of these middleware args contain ack and others do not, so in theory Args should be carrying the correct type, including the conditional presence of ack.

You may have to narrow the return type from this method appropriately to get the right kind of Args depending on the situation, which should in theory sometimes carry ack.

I assume the issue was asserting on ack in the test file you changed in this PR?

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