From 7defbd2e0ffa770d47a94f4c5a41576fc0d0193c Mon Sep 17 00:00:00 2001 From: Ryo Kato Date: Sat, 23 May 2020 23:23:46 +0900 Subject: [PATCH] Make matchMessage() handle app_mention events --- src/middleware/builtin.spec.ts | 70 +++++++++++++++++++++++++++------- src/middleware/builtin.ts | 14 ++++--- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/middleware/builtin.spec.ts b/src/middleware/builtin.spec.ts index e03464c9f..0e9ab3fbf 100644 --- a/src/middleware/builtin.spec.ts +++ b/src/middleware/builtin.spec.ts @@ -32,14 +32,18 @@ describe('matchMessage()', () => { }; } - function matchesPatternTestCase(pattern: string | RegExp, matchingText: string): Mocha.AsyncFunc { + function matchesPatternTestCase( + pattern: string | RegExp, + matchingText: string, + buildFakeEvent: (content: string) => SlackEvent, + ): Mocha.AsyncFunc { return async () => { // Arrange const dummyContext: DummyContext = {}; const fakeNext = sinon.fake(); const fakeArgs = { next: fakeNext, - message: createFakeMessageEvent(matchingText), + event: buildFakeEvent(matchingText), context: dummyContext, } as unknown as MessageMiddlewareArgs; const { matchMessage } = await importBuiltin(); @@ -61,13 +65,17 @@ describe('matchMessage()', () => { }; } - function notMatchesPatternTestCase(pattern: string | RegExp, nonMatchingText: string): Mocha.AsyncFunc { + function notMatchesPatternTestCase( + pattern: string | RegExp, + nonMatchingText: string, + buildFakeEvent: (content: string) => SlackEvent, + ): Mocha.AsyncFunc { return async () => { // Arrange const dummyContext = {}; const fakeNext = sinon.fake(); const fakeArgs = { - message: createFakeMessageEvent(nonMatchingText), + event: buildFakeEvent(nonMatchingText), context: dummyContext, next: fakeNext, } as unknown as MessageMiddlewareArgs; @@ -89,7 +97,7 @@ describe('matchMessage()', () => { const dummyContext = {}; const fakeNext = sinon.fake(); const fakeArgs = { - message: createFakeMessageEvent([{ type: 'divider' }]), + event: createFakeMessageEvent([{ type: 'divider' }]), context: dummyContext, next: fakeNext, } as unknown as MessageMiddlewareArgs; @@ -110,10 +118,22 @@ describe('matchMessage()', () => { const matchingText = 'foobar'; const nonMatchingText = 'bar'; it('should initialize', initializeTestCase(pattern)); - it('should match message events with a pattern that matches', matchesPatternTestCase(pattern, matchingText)); - it('should filter out message events with a pattern that does not match', notMatchesPatternTestCase( - pattern, nonMatchingText, - )); + it( + 'should match message events with a pattern that matches', + matchesPatternTestCase(pattern, matchingText, createFakeMessageEvent), + ); + it( + 'should match app_mention events with a pattern that matches', + matchesPatternTestCase(pattern, matchingText, createFakeAppMentionEvent), + ); + it( + 'should filter out message events with a pattern that does not match', + notMatchesPatternTestCase(pattern, nonMatchingText, createFakeMessageEvent), + ); + it( + 'should filter out app_mention events with a pattern that does not match', + notMatchesPatternTestCase(pattern, nonMatchingText, createFakeAppMentionEvent), + ); it('should filter out message events which do not have text (block kit)', noTextMessageTestCase(pattern)); }); @@ -122,10 +142,22 @@ describe('matchMessage()', () => { const matchingText = 'foobar'; const nonMatchingText = 'bar'; it('should initialize', initializeTestCase(pattern)); - it('should match message events with a pattern that matches', matchesPatternTestCase(pattern, matchingText)); - it('should filter out message events with a pattern that does not match', notMatchesPatternTestCase( - pattern, nonMatchingText, - )); + it( + 'should match message events with a pattern that matches', + matchesPatternTestCase(pattern, matchingText, createFakeMessageEvent), + ); + it( + 'should match app_mention events with a pattern that matches', + matchesPatternTestCase(pattern, matchingText, createFakeAppMentionEvent), + ); + it( + 'should filter out message events with a pattern that does not match', + notMatchesPatternTestCase(pattern, nonMatchingText, createFakeMessageEvent), + ); + it( + 'should filter out app_mention events with a pattern that does not match', + notMatchesPatternTestCase(pattern, nonMatchingText, createFakeAppMentionEvent), + ); it('should filter out message events which do not have text (block kit)', noTextMessageTestCase(pattern)); }); }); @@ -678,6 +710,18 @@ function createFakeMessageEvent(content: string | MessageEvent['blocks'] = ''): return event as MessageEvent; } +function createFakeAppMentionEvent(text: string = ''): AppMentionEvent { + const event: Partial = { + text, + type: 'app_mention', + user: 'USER_ID', + ts: 'MESSAGE_ID', + channel: 'CHANNEL_ID', + event_ts: 'MESSAGE_ID', + }; + return event as AppMentionEvent; +} + const validCommandPayload: SlashCommand = { token: 'token-value', command: '/hi', diff --git a/src/middleware/builtin.ts b/src/middleware/builtin.ts index 655ffa7c3..68ca834d2 100644 --- a/src/middleware/builtin.ts +++ b/src/middleware/builtin.ts @@ -205,21 +205,23 @@ export function matchConstraints( /* * Middleware that filters out messages that don't match pattern */ -export function matchMessage(pattern: string | RegExp): Middleware> { - return async ({ message, context, next }) => { +export function matchMessage( + pattern: string | RegExp, + ): Middleware> { + return async ({ event, context, next }) => { let tempMatches: RegExpMatchArray | null; - if (message.text === undefined) { + if (event.text === undefined) { return; } - // Filter out messages that don't contain the pattern + // Filter out messages or app mentions that don't contain the pattern if (typeof pattern === 'string') { - if (!message.text.includes(pattern)) { + if (!event.text.includes(pattern)) { return; } } else { - tempMatches = message.text.match(pattern); + tempMatches = event.text.match(pattern); if (tempMatches !== null) { context['matches'] = tempMatches;