Skip to content
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
1 change: 1 addition & 0 deletions src/bot/db/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const rawToGambitData = function rawToGambitData(gambitId, gambit) {
filter: gambit.trigger.filter,
trigger: gambit.trigger.clean,
input: gambit.trigger.raw,
topic: gambit.topic
};

// Conditional rolled up triggers will not have a flags
Expand Down
3 changes: 3 additions & 0 deletions src/bot/db/models/gambit.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const createGambitModel = function createGambitModel(db, factSystem) {
// TODO, change the type to a ID and reference another gambit directly
// this will save us a lookup down the road (and improve performace.)
redirect: { type: String, default: '' },

// topic field added to fix issue#375
topic: { type: String, default: 'random'}
});

gambitSchema.pre('save', function (next) {
Expand Down
12 changes: 12 additions & 0 deletions src/bot/getReply/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ const findMatches = async function findMatches(pendingTopics, messageObject, opt
debug.info(`Replies: ${match.gambit.replies.map(reply => reply.reply).join('\n')}`);
});

// If there are multiple matches, then select the one from topic. Issue #375
function isTopicMatched(match) {
return match.gambit.topic === options.user.getTopic();
}
var topicIndex = unfilteredMatches.findIndex(isTopicMatched);

if (unfilteredMatches.length > 1 && topicIndex > 0) {
var tmp = unfilteredMatches[0];
unfilteredMatches[0] = unfilteredMatches[topicIndex];
unfilteredMatches[topicIndex] = tmp;
}

for (let j = 0; j < unfilteredMatches.length && !stopSearching; ++j) {
const match = unfilteredMatches[j];
const reply = await matchItorHandle(match, messageObject, options);
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/topicconversation/main.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
+ I love animals
- ^topicRedirect("animals", "__favorite__")

+ I love pets
- ^topicRedirect("pets", "__favorite__")


> topic animals {keep}
+ __favorite__
- I don't like pets, what is your favorite?

+ Mine is cat
- Cats are scary, what is your favorite?
+ whatever
% * what is your favorite *
-Cats are scary

+ i do love animals
- Yes, I do as well
< topic

> topic pets {keep}
+ __favorite__
- Me too, what is your favorite?

+ Mine is cat
- I love it too, what is your favorite ?
+ whatever
% * what is your favorite *
-Cats are fun

+ i do love animals
- No, I like only cats
< topic
62 changes: 62 additions & 0 deletions test/topicconversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* global describe, it, before, after */

import mocha from 'mocha';
import should from 'should/as-function';
import helpers from './helpers';
import { doesMatch, doesMatchTopic } from '../src/bot/getReply/helpers';


// Testing topics that include and mixin other topics.
describe('SuperScript TopicsConversation', () => {
before(helpers.before('topicconversation'));

describe('TopicConversationPets', () => {
it('Conversation begin', (done) => {
helpers.getBot().reply('tc_user', 'i love pets', (err, reply) => {
should('Me too, what is your favorite?').eql(reply.string);
done();
});
});

it('Inside the pets topic', (done) => {
helpers.getBot().reply('tc_user', 'mine is cat', (err, reply) => {
should(reply.string).eql('I love it too, what is your favorite ?');
done();
});
});

it('Should remain in the pets topic', (done) => {
helpers.getBot().reply('tc_user', 'whatever', (err, reply) => {
should(reply.string).eql('Cats are fun');
done();
});
});

});

describe('TopicConversationAnimals', () => {
it('Conversation begin', (done) => {
helpers.getBot().reply('tc_user', 'i love animals', (err, reply) => {
should('I don\'t like pets, what is your favorite?').eql(reply.string);
done();
});
});

it('Inside the pets topic', (done) => {
helpers.getBot().reply('tc_user', 'mine is cat', (err, reply) => {
should(reply.string).eql('Cats are scary, what is your favorite?');
done();
});
});

it('Should remain in the pets topic', (done) => {
helpers.getBot().reply('tc_user', 'whatever', (err, reply) => {
should(reply.string).eql('Cats are scary');
done();
});
});

});

after(helpers.after);
});