Skip to content

Commit

Permalink
Fix #757 Add event type name validation & channel_type filter middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Mar 24, 2021
1 parent afcd7bd commit 62a14e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ describe('App', () => {
assert(error.code === ErrorCode.MultipleListenerError);
assert.sameMembers(error.originals, errorsToThrow);
});

it('should detect invalid event names', async () => {
app.event('app_mention', async () => {});
app.event('message', async () => {});
assert.throws(() => app.event('message.channels', async () => {}), 'Although the document mentions');
assert.throws(() => app.event(/message\..+/, async () => {}), 'Although the document mentions');
});
});

describe('WorkflowStep middleware', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,21 @@ export default class App {
eventNameOrPattern: EventType,
...listeners: Middleware<SlackEventMiddlewareArgs<string>>[]
): void {
let invalidEventName = false;
if (typeof eventNameOrPattern === 'string') {
const name = eventNameOrPattern as string;
invalidEventName = name.startsWith('message.');
} else if (eventNameOrPattern instanceof RegExp) {
const name = (eventNameOrPattern as RegExp).source;
invalidEventName = name.startsWith('message\\.');
}
if (invalidEventName) {
throw new AppInitializationError(
`Although the document mentions "${eventNameOrPattern}",` +
'it is not a valid event type. Use "message" instead. ' +
'If you want to filter message events, you can use event.channel_type for it.',
);
}
this.listeners.push([
onlyEvents,
matchEventType(eventNameOrPattern),
Expand Down

0 comments on commit 62a14e5

Please sign in to comment.