Skip to content

Commit

Permalink
Make matchMessage() handle app_mention events
Browse files Browse the repository at this point in the history
  • Loading branch information
hashedhyphen committed May 23, 2020
1 parent 9315395 commit 7defbd2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
70 changes: 57 additions & 13 deletions src/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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));
});

Expand All @@ -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));
});
});
Expand Down Expand Up @@ -678,6 +710,18 @@ function createFakeMessageEvent(content: string | MessageEvent['blocks'] = ''):
return event as MessageEvent;
}

function createFakeAppMentionEvent(text: string = ''): AppMentionEvent {
const event: Partial<AppMentionEvent> = {
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',
Expand Down
14 changes: 8 additions & 6 deletions src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,23 @@ export function matchConstraints(
/*
* Middleware that filters out messages that don't match pattern
*/
export function matchMessage(pattern: string | RegExp): Middleware<SlackEventMiddlewareArgs<'message'>> {
return async ({ message, context, next }) => {
export function matchMessage(
pattern: string | RegExp,
): Middleware<SlackEventMiddlewareArgs<'message' | 'app_mention'>> {
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;
Expand Down

0 comments on commit 7defbd2

Please sign in to comment.