Skip to content

Commit

Permalink
#31 - Fix middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaida committed Jun 23, 2020
1 parent b36cecf commit 1eebefa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
6 changes: 2 additions & 4 deletions app-mentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const errHandler = require('./utils/error');
APP MENTIONS
------------------*/
const app_mentions = (app, store) => {
app.event('app_mention', async({ event, context }) => {
// Ignore channel topic and message edits
if (utils.ignoreMention(event)) return;
app.event('app_mention', utils.ignoreMention, async({ event, context }) => {
// Event and context data
const ec = {
text: event.text, // raw text from the mention
Expand Down Expand Up @@ -115,7 +113,7 @@ const app_mentions = (app, store) => {
// @rota anything else
else {
try {
console.log('Event: ', event, 'Clean Text: ', utils.cleanText(ec.text));
// console.log('Event: ', event, 'Clean Text: ', utils.cleanText(ec.text));
const result = await app.client.chat.postMessage(
utils.msgConfig(ec.botToken, ec.channelID, msgText.didntUnderstand())
);
Expand Down
15 changes: 10 additions & 5 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,18 @@ const utils = {
}
},
/**
* Ignore certain messages
* @param {object} event message event
* @return {boolean} should this message be ignored?
* Message middleware: ignore some kinds of messages
* @param {object} event event object
* @return {Promise<void>} continue if not ignored message type
*/
ignoreMention(event) {
async ignoreMention({ event, next }) {
const disallowedSubtypes = ['channel_topic', 'message_changed'];
return disallowedSubtypes.indexOf(event.subtype) > -1 || !!event.edited;
const ignoreSubtype = disallowedSubtypes.indexOf(event.subtype) > -1;
const messageChanged = !!event.edited;
// If not ignored subtype and not an edited message event, proceed
if (!ignoreSubtype && !messageChanged) {
await next();
}
}
};

Expand Down

0 comments on commit 1eebefa

Please sign in to comment.