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 global matching in matchMessage() #150

Merged
merged 8 commits into from Apr 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/middleware/builtin.ts
Expand Up @@ -79,8 +79,7 @@ export function matchConstraints(
return ({ payload, body, next, context }) => {
// TODO: is putting matches in an array actually helpful? there's no way to know which of the regexps contributed
// which matches (and in which order)
let tempMatches: RegExpExecArray | null;
const matches: any[] = [];
let tempMatches: RegExpMatchArray | null;

if (constraints.block_id !== undefined) {
if (!isBlockPayload(payload)) {
Expand All @@ -92,8 +91,10 @@ export function matchConstraints(
return;
}
} else {
if ((tempMatches = constraints.block_id.exec(payload.block_id)) !== null) {
matches.concat(tempMatches);
tempMatches = payload.block_id.match(constraints.block_id);

if (tempMatches !== null) {
context['blockIdMatches'] = tempMatches;
} else {
return;
}
Expand All @@ -110,8 +111,10 @@ export function matchConstraints(
return;
}
} else {
if ((tempMatches = constraints.action_id.exec(payload.action_id)) !== null) {
matches.concat(tempMatches);
tempMatches = payload.action_id.match(constraints.action_id);

if (tempMatches !== null) {
context['actionIdMatches'] = tempMatches;
} else {
return;
}
Expand All @@ -127,17 +130,16 @@ export function matchConstraints(
return;
}
} else {
if ((tempMatches = constraints.callback_id.exec(body.callback_id)) !== null) {
matches.concat(tempMatches);
tempMatches = body.callback_id.match(constraints.callback_id);

if (tempMatches !== null) {
context['callbackIdMatches'] = tempMatches;
} else {
return;
}
}
}

// Add matches to context
context['matches'] = matches;

next();
};
}
Expand All @@ -147,15 +149,17 @@ export function matchConstraints(
*/
export function matchMessage(pattern: string | RegExp): Middleware<SlackEventMiddlewareArgs<'message'>> {
return ({ message, context, next }) => {
let tempMatches: RegExpExecArray | null;
let tempMatches: RegExpMatchArray | null;

// Filter out messages that don't contain the pattern
if (typeof pattern === 'string') {
if (message.text !== undefined && !message.text.includes(pattern)) {
return;
}
} else {
if ((tempMatches = pattern.exec(message.text)) !== null) {
tempMatches = message.text.match(pattern);

if (tempMatches !== null) {
context['matches'] = tempMatches;
} else {
return;
Expand Down