From c024e0df92e876570279dc98d62641a4b987df5d Mon Sep 17 00:00:00 2001 From: JP S Date: Sat, 24 Mar 2018 13:50:46 -0400 Subject: [PATCH 1/3] Ensure commander instance is still in scope in .then processing --- lib/commander.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/commander.js b/lib/commander.js index 1e3a2cd..3ec1cba 100644 --- a/lib/commander.js +++ b/lib/commander.js @@ -13,11 +13,12 @@ class Commander { return when(false); } + let self = this; return this.flint.trigger.createFromMessage(msg) - .then(trigger => this.flint.authorization.auth(trigger)) - .then((trigger) => { + .then(trigger => self.flint.authorization.auth(trigger)) + .then(trigger => { // find match - const foundMatches = this.flint.hears.match(trigger.message.normalized); + const foundMatches = self.flint.hears.match(trigger.message.normalized); if (foundMatches && foundMatches.length > 0) { // sort matches by preference @@ -32,7 +33,7 @@ class Commander { sortedMatch => (sortedMatch.preference === prefLow)); } - return this.flint.query({ roomId: trigger.room.id }) + return self.flint.query({ roomId: trigger.room.id }) .then(bot => when.map(sortedMatches, sortedMatch => when.try(sortedMatch.action, bot, trigger))); } @@ -40,7 +41,7 @@ class Commander { return when(true); }) .catch((err) => { - this.flint.logger.error(err); + self.flint.logger.error(err); return when(true); }); } From 368a58f813b80257b97dfbd02506c7b6fb67ca62 Mon Sep 17 00:00:00 2001 From: JP S Date: Sat, 24 Mar 2018 13:52:08 -0400 Subject: [PATCH 2/3] Keep trigger instance in scope. Access email by first element in memebership.email[] --- lib/factory/trigger.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/factory/trigger.js b/lib/factory/trigger.js index 7b4389e..f595259 100644 --- a/lib/factory/trigger.js +++ b/lib/factory/trigger.js @@ -52,6 +52,8 @@ class Trigger { created: moment().toDate(), }; + // Keep a copy of ourselves + let self = this; // populate person return this.flint.spark.personGet(trigger.person.id) .then(person => when(_.merge(trigger.person, person))) @@ -64,19 +66,31 @@ class Trigger { // enhance message .then(() => { let normalText = text(trigger.message.text).normalize(); + //TODO: Validate that it is intentional that this method + // should strip of forward slashes and lowere case eveything + // ie: change this: "Bot /echo Repeat this back to me." + // to: "bot echo repeat this back to me " // remove bot from mentioned people if (trigger.message.mentionedPeople.length > 0 - && _.includes(trigger.message.mentionedPeople, this.flint.person.id) + && _.includes(trigger.message.mentionedPeople, self.flint.person.id) ) { + //TODO This method of trying to strip the mention name out of + // the message depends on the mentioned name matching the displayName + // or email. WIth the APIs this is not necearily the case as + // it can be set arbitrariy via the API + // Better would be to ferret out the mention name stored in the + // markup field in the message object, ie: + // markdown: "The Bot I'm Testing + // JP didn't implement this because he's not building that yet in the spark-emulator const botIdentities = []; // add bot email if email to name resolution has not yet occcurred - botIdentities.push(this.flint.person.email); + botIdentities.push(self.flint.person.emails[0]); // never understood why this is an array.... // add bot display name - botIdentities.push(_.toLower(this.flint.person.displayName)); + botIdentities.push(_.toLower(self.flint.person.displayName)); // add bot first name when bot name is two or more words - if (this.flint.person.displayName.split(' ').length > 1) { - botIdentities.push(_.toLower(this.flint.person.displayName).split(' ')[0]); + if (self.flint.person.displayName.split(' ').length > 1) { + botIdentities.push(_.toLower(self.flint.person.displayName).split(' ')[0]); } // if bot is mentioned in first word(s) _.forEach(botIdentities, (botIdentity) => { @@ -85,6 +99,7 @@ class Trigger { normalText = normalText.replace(botIdRe, ''); } }); + // TODO its also not clear this logic works if there are multiple mentions in the same message } trigger.message.normalized = normalText.trim(); @@ -102,7 +117,10 @@ class Trigger { }) // return populated trigger - .then(() => when(trigger)); + .then(() => { + console.log('This is it!'); + return when(trigger); + }) } return when.reject(new Error('trigger can not parse an invalid message')); } From 549518b37d618690b09148d1a743255f4c5657dc Mon Sep 17 00:00:00 2001 From: JP S Date: Sat, 24 Mar 2018 14:03:19 -0400 Subject: [PATCH 3/3] Removed debugging output --- lib/factory/trigger.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/factory/trigger.js b/lib/factory/trigger.js index f595259..b13e902 100644 --- a/lib/factory/trigger.js +++ b/lib/factory/trigger.js @@ -118,7 +118,6 @@ class Trigger { // return populated trigger .then(() => { - console.log('This is it!'); return when(trigger); }) }