Skip to content
Merged
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
19 changes: 12 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
.DS_Store
# SuperScript
lib/*
node_modules/*
logs/*
npm-debug.log
test/fixtures/cache/*
dump.rdb
dump/*
coverage
*.sw*
coverage/*

# Node
node_modules/*
npm-debug.log

# Node profiler logs
isolate-*-v8.log

# OS Files
.DS_Store
*.sw*
dump.rdb
dump/*

# IDEA/WebStorm Project Files
.idea
*.iml
20 changes: 14 additions & 6 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
# SuperScript
src/*
test/*
example/*
logs/*
coverage/*
.github/*

.babelrc
.eslintrc
.travis.yml
changes.md
contribute.md
LICENSE.md
readme.md
logs/*
test/fixtures/cache/*
dump.rdb
dump/*
coverage
*.sw*

# Node
node_modules/*
npm-debug.log

# Node profiler logs
isolate-*-v8.log

# OS Files
.DS_Store
*.sw*
dump.rdb
dump/*

# IDEA/WebStorm Project Files
.idea
*.iml
2 changes: 1 addition & 1 deletion clients/hangout.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ const options = {
importFile: './data.json',
};

SuperScript(options, (err, bot) => {
SuperScript.setup(options, (err, bot) => {
botHandle(null, bot);
});
2 changes: 1 addition & 1 deletion clients/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ const options = {
importFile: './data.json',
};

SuperScript(options, (err, bot) => {
SuperScript.setup(options, (err, bot) => {
botHandle(null, bot);
});
2 changes: 1 addition & 1 deletion clients/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const options = {
importFile: './data.json',
};

SuperScript(options, (err, bot) => {
SuperScript.setup(options, (err, bot) => {
if (err) {
console.error(err);
}
Expand Down
2 changes: 1 addition & 1 deletion clients/telnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ const options = {
importFile: './data.json',
};

SuperScript(options, (err, bot) => {
SuperScript.setup(options, (err, bot) => {
botHandle(null, bot);
});
2 changes: 1 addition & 1 deletion clients/twilio.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const options = {
importFile: './data.json',
};

SuperScript(options, (err, bot) => {
SuperScript.setup(options, (err, bot) => {
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"lodash": "^4.16.5",
"mkdirp": "^0.5.1",
"moment": "^2.13.0",
"mongo-tenant": "^1.0.1",
"mongoose": "^4.5.10",
"mongoose-findorcreate": "^0.1.2",
"natural": "^0.4.0",
Expand Down
35 changes: 23 additions & 12 deletions src/bot/chatSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@ import createReplyModel from './db/models/reply';
import createTopicModel from './db/models/topic';
import createUserModel from './db/models/user';

const createChatSystem = function createChatSystem(db, factSystem, logPath) {
const Gambit = createGambitModel(db, factSystem);
const Reply = createReplyModel(db);
const Topic = createTopicModel(db);
const User = createUserModel(db, factSystem, logPath);

return {
Gambit,
Reply,
Topic,
User,
const setupChatSystem = function setupChatSystem(db, coreFactSystem, logger) {
const GambitCore = createGambitModel(db, coreFactSystem);
const ReplyCore = createReplyModel(db);
const TopicCore = createTopicModel(db);
const UserCore = createUserModel(db, coreFactSystem, logger);

const getChatSystem = function getChatSystem(tenantId = 'master') {
const Gambit = GambitCore.byTenant(tenantId);
const Reply = ReplyCore.byTenant(tenantId);
const Topic = TopicCore.byTenant(tenantId);
const User = UserCore.byTenant(tenantId);

return {
Gambit,
Reply,
Topic,
User,
};
};

return { getChatSystem };
};

export default createChatSystem;
export default {
setupChatSystem,
};
29 changes: 15 additions & 14 deletions src/bot/db/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import _ from 'lodash';
import debuglog from 'debug-levels';
import safeEval from 'safe-eval';

import modelNames from './modelNames';
import Utils from '../utils';
import postParse from '../postParse';

const debug = debuglog('SS:Common');

const _walkReplyParent = function _walkReplyParent(db, replyId, replyIds, cb) {
db.model('Reply').findById(replyId)
const _walkReplyParent = function _walkReplyParent(db, tenantId, replyId, replyIds, cb) {
db.model(modelNames.reply).byTenant(tenantId).findById(replyId)
.populate('parent')
.exec((err, reply) => {
if (err) {
Expand All @@ -23,7 +24,7 @@ const _walkReplyParent = function _walkReplyParent(db, replyId, replyIds, cb) {
if (reply) {
replyIds.push(reply._id);
if (reply.parent && reply.parent.parent) {
_walkReplyParent(db, reply.parent.parent, replyIds, cb);
_walkReplyParent(db, tenantId, reply.parent.parent, replyIds, cb);
} else {
cb(null, replyIds);
}
Expand All @@ -33,8 +34,8 @@ const _walkReplyParent = function _walkReplyParent(db, replyId, replyIds, cb) {
});
};

const _walkGambitParent = function _walkGambitParent(db, gambitId, gambitIds, cb) {
db.model('Gambit').findOne({ _id: gambitId })
const _walkGambitParent = function _walkGambitParent(db, tenantId, gambitId, gambitIds, cb) {
db.model(modelNames.gambit).byTenant(tenantId).findOne({ _id: gambitId })
.populate('parent')
.exec((err, gambit) => {
if (err) {
Expand All @@ -44,7 +45,7 @@ const _walkGambitParent = function _walkGambitParent(db, gambitId, gambitIds, cb
if (gambit) {
gambitIds.push(gambit._id);
if (gambit.parent && gambit.parent.parent) {
_walkGambitParent(db, gambit.parent.parent, gambitIds, cb);
_walkGambitParent(db, tenantId, gambit.parent.parent, gambitIds, cb);
} else {
cb(null, gambitIds);
}
Expand All @@ -56,7 +57,7 @@ const _walkGambitParent = function _walkGambitParent(db, gambitId, gambitIds, cb

// This will find all the gambits to process by parent (topic or conversation)
// and return ones that match the message
const findMatchingGambitsForMessage = function findMatchingGambitsForMessage(db, type, id, message, options, callback) {
const findMatchingGambitsForMessage = function findMatchingGambitsForMessage(db, tenantId, type, id, message, options, callback) {
// Let's query for Gambits
const execHandle = function execHandle(err, gambitsParent) {
if (err) {
Expand All @@ -65,7 +66,7 @@ const findMatchingGambitsForMessage = function findMatchingGambitsForMessage(db,

const populateGambits = function populateGambits(gambit, next) {
debug.verbose('Populating gambit');
db.model('Reply').populate(gambit, { path: 'replies' }, next);
db.model(modelNames.reply).byTenant(tenantId).populate(gambit, { path: 'replies' }, next);
};

async.each(gambitsParent.gambits, populateGambits, (err) => {
Expand All @@ -83,13 +84,13 @@ const findMatchingGambitsForMessage = function findMatchingGambitsForMessage(db,

if (type === 'topic') {
debug.verbose('Looking back Topic', id);
db.model('Topic').findOne({ _id: id }, 'gambits')
db.model(modelNames.topic).byTenant(tenantId).findOne({ _id: id }, 'gambits')
.populate({ path: 'gambits' })
.exec(execHandle);
} else if (type === 'reply') {
options.topic = 'reply';
debug.verbose('Looking back at Conversation', id);
db.model('Reply').findOne({ _id: id }, 'gambits')
db.model(modelNames.reply).byTenant(tenantId).findOne({ _id: id }, 'gambits')
.populate({ path: 'gambits' })
.exec(execHandle);
} else {
Expand Down Expand Up @@ -325,12 +326,12 @@ const _eachGambitHandle = function (message, options) {
};
}; // end EachGambit

const walkReplyParent = (db, replyId, cb) => {
_walkReplyParent(db, replyId, [], cb);
const walkReplyParent = (db, tenantId, replyId, cb) => {
_walkReplyParent(db, tenantId, replyId, [], cb);
};

const walkGambitParent = (db, gambitId, cb) => {
_walkGambitParent(db, gambitId, [], cb);
const walkGambitParent = (db, tenantId, gambitId, cb) => {
_walkGambitParent(db, tenantId, gambitId, [], cb);
};

export default {
Expand Down
8 changes: 8 additions & 0 deletions src/bot/db/modelNames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const names = {
gambit: 'ss_gambit',
reply: 'ss_reply',
topic: 'ss_topic',
user: 'ss_user',
};

export default names;
27 changes: 13 additions & 14 deletions src/bot/db/models/gambit.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
/**

A Gambit is a Trigger + Reply or Reply Set
- We define a Reply as a subDocument in Mongo.

**/

import mongoose from 'mongoose';
import findOrCreate from 'mongoose-findorcreate';
// import norm from 'node-normalizer';
import mongoTenant from 'mongo-tenant';
import debuglog from 'debug-levels';
import async from 'async';
import parser from 'ss-parser';

import modelNames from '../modelNames';
import helpers from '../helpers';
import Utils from '../../utils';

const debug = debuglog('SS:Gambit');

/**

A trigger is the matching rule behind a piece of input. It lives in a topic or several topics.
A trigger also contains one or more replies.

**/

const createGambitModel = function createGambitModel(db, factSystem) {
Expand Down Expand Up @@ -50,10 +47,10 @@ const createGambitModel = function createGambitModel(db, factSystem) {
filter: { type: String, default: '' },

// An array of replies.
replies: [{ type: String, ref: 'Reply' }],
replies: [{ type: String, ref: modelNames.reply }],

// Save a reference to the parent Reply, so we can walk back up the tree
parent: { type: String, ref: 'Reply' },
parent: { type: String, ref: modelNames.reply },

// This will redirect anything that matches elsewhere.
// If you want to have a conditional rediect use reply redirects
Expand All @@ -70,7 +67,8 @@ const createGambitModel = function createGambitModel(db, factSystem) {

// If we created the trigger in an external editor, normalize the trigger before saving it.
if (this.input && !this.trigger) {
return parser.normalizeTrigger(this.input, factSystem, (err, cleanTrigger) => {
const facts = factSystem.getFactSystem(this.getTenantId());
return parser.normalizeTrigger(this.input, facts, (err, cleanTrigger) => {
this.trigger = cleanTrigger;
next();
});
Expand All @@ -83,7 +81,7 @@ const createGambitModel = function createGambitModel(db, factSystem) {
return callback('No data');
}

const Reply = db.model('Reply');
const Reply = db.model(modelNames.reply).byTenant(this.getTenantId());
const reply = new Reply(replyData);
reply.save((err) => {
if (err) {
Expand All @@ -105,7 +103,7 @@ const createGambitModel = function createGambitModel(db, factSystem) {

const clearReply = function (replyId, cb) {
self.replies.pull({ _id: replyId });
db.model('Reply').remove({ _id: replyId }, (err) => {
db.model(modelNames.reply).byTenant(this.getTenantId()).remove({ _id: replyId }, (err) => {
if (err) {
console.log(err);
}
Expand All @@ -125,15 +123,15 @@ const createGambitModel = function createGambitModel(db, factSystem) {

gambitSchema.methods.getRootTopic = function (cb) {
if (!this.parent) {
db.model('Topic')
db.model(modelNames.topic).byTenant(this.getTenantId())
.findOne({ gambits: { $in: [this._id] } })
.exec((err, doc) => {
cb(err, doc.name);
});
} else {
helpers.walkGambitParent(db, this._id, (err, gambits) => {
helpers.walkGambitParent(db, this.getTenantId(), this._id, (err, gambits) => {
if (gambits.length !== 0) {
db.model('Topic')
db.model(modelNames.topic).byTenant(this.getTenantId())
.findOne({ gambits: { $in: [gambits.pop()] } })
.exec((err, topic) => {
cb(null, topic.name);
Expand All @@ -146,8 +144,9 @@ const createGambitModel = function createGambitModel(db, factSystem) {
};

gambitSchema.plugin(findOrCreate);
gambitSchema.plugin(mongoTenant);

return db.model('Gambit', gambitSchema);
return db.model('ss_gambit', gambitSchema);
};

export default createGambitModel;
Loading