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

Bugfix various #20

Open
wants to merge 3 commits into
base: v5-beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,15 +33,15 @@ 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)));
}

return when(true);
})
.catch((err) => {
this.flint.logger.error(err);
self.flint.logger.error(err);
return when(true);
});
}
Expand Down
29 changes: 23 additions & 6 deletions lib/factory/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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: "<spark-mention data-object-type=\"person\" data-object-id=\"Y2l...w\">The Bot I'm Testing</spark-mention>
// 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) => {
Expand All @@ -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();
Expand All @@ -102,7 +117,9 @@ class Trigger {
})

// return populated trigger
.then(() => when(trigger));
.then(() => {
return when(trigger);
})
}
return when.reject(new Error('trigger can not parse an invalid message'));
}
Expand Down