Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Mongodb support #9

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
132 changes: 5 additions & 127 deletions components/routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,117 +20,11 @@ module.exports = function(webserver, api) {
});

webserver.post('/admin/save', function(req, res) {
var update = req.body;

api.getScripts().then(function(scripts) {
var found = false;
for (var s = 0; s < scripts.length; s++) {
if (scripts[s].id === update.id) {
found = s;
}
}

if (update.is_fallback) {
scripts.forEach(function(script) {
script.is_fallback = false;
});
}

if (found === false) {
if (!update.id && update._id) {
update.id = update._id;
} else if (!update.id) {
update.id = uuidv4();
}
update.modified = new Date();
scripts.push(update);
api.writeScriptsToFile(scripts).then(function() {
res.json({
success: true,
data: update,
});
});

} else if (new Date(scripts[found].modified) > new Date(update.modified)) {

// if the version in the database was more recently modified, reject this update!
res.json({
success: false,
message: 'Script was modified more recently, please refresh your browser to load the latest',
});

} else {

var original_name = scripts[found].command;

scripts[found] = update;
scripts[found].modified = new Date();

if (update.command != original_name) {
handleRenamed(scripts, original_name, update.command).then(function() {
api.writeScriptsToFile(scripts).then(function() {
res.json({
success: true,
data: update,
});
});
});
} else {
api.writeScriptsToFile(scripts).then(function() {
res.json({
success: true,
data: update,
});
});
}
}
});

api.saveScripts(req.body).then(function(response) {
res.json(response)
})
});


function handleRenamed(scripts, original_name, new_name) {
return new Promise(function(resolve, reject) {
async.each(scripts, function(command, next) {
updateExecScript(command, original_name, new_name, next);
}, function() {
resolve();
})
});
}

function updateExecScript(command, original_name, new_name, next) {
// need to look at command.script[*].script[*].action
// need to look at command.script[*].script[*].collect.options[*].action
var dirty = false;
for (var t = 0; t < command.script.length; t++) {
for (var m = 0; m < command.script[t].script.length; m++) {
if (command.script[t].script[m].action == 'execute_script' && command.script[t].script[m].execute && command.script[t].script[m].execute.script == original_name) {
command.script[t].script[m].execute.script = new_name;
dirty = true;
}

if (command.script[t].script[m].collect && command.script[t].script[m].collect.options) {
for (var o = 0; o < command.script[t].script[m].collect.options.length; o++) {
if (command.script[t].script[m].collect.options[o].action=='execute_script' && command.script[t].script[m].collect.options[o].execute && command.script[t].script[m].collect.options[o].execute.script == original_name) {
command.script[t].script[m].collect.options[o].execute.script = new_name;
dirty = true;
}
}
}
}
}

if (dirty) {
command.modified = new Date();
next();
} else {
next();
}
}



// receives: command, user
webserver.post('/admin/api/script', function(req, res) {
if (req.body.command) {
Expand Down Expand Up @@ -222,24 +116,8 @@ module.exports = function(webserver, api) {
});

webserver.delete('/admin/api/scripts/:id', function(req, res) {
api.getScripts().then(function(scripts) {

// delete script out of list.
scripts = scripts.filter((script) => { return (script.id !== req.params.id) });

// write scripts back to file.
api.writeScriptsToFile(scripts).then(function() {
res.json({
success: true,
data: scripts,
});
});

}).catch(function(err) {
if (err) {
console.error('Error in getScripts',err);
}
res.json({});
api.deleteScript(req.params.id).then(function(response) {
res.json(response);
})
});

Expand Down
1 change: 1 addition & 0 deletions components/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = function(webserver, api) {
// receives: command, user
webserver.post('/api/v1/commands/name', function(req, res) {
api.getScript(req.body.command).then(function(script) {
script.id = script._id;
res.json(script);
}).catch(function(err) {
if (err) {
Expand Down
54 changes: 34 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
require('dotenv').config()
var api = require(__dirname + '/src/api.js')()
require('dotenv').config();
var MongoClient = require('mongodb').MongoClient;

if (!process.env.USERS) {
throw new Error('Specify at least one username:password combo in the USERS environment variable')
}

var admin_creds = api.parseAdminUsers(process.env.USERS);
async function connect() {
if (!process.env.USERS) {
console.log('Please specify at least one username:password combo in the USERS environment variable')
}

if (process.env.MONGODB_URL) {
client = await MongoClient.connect(process.env.MONGODB_URL)
db = client.db(process.env.MONGODB_DB_NAME);
} else {
db = null;
}

var api = require(__dirname + '/src/api.js')(db);
var admin_creds = api.parseAdminUsers(process.env.USERS);

// load scripts from file
api.loadScriptsFromFile(__dirname + '/.data/scripts.json', __dirname + '/.data/sample_scripts.json').catch(function(err) {
console.log('Could not load scripts from file:', err);
process.exit(1);
});
// Set up an Express-powered webserver to expose oauth and webhook endpoints
var webserver = require(__dirname + '/components/express_webserver.js')(admin_creds);

// Set up an Express-powered webserver to expose oauth and webhook endpoints
var webserver = require(__dirname + '/components/express_webserver.js')(admin_creds);

require(__dirname + '/components/routes/admin.js')(webserver, api);
require(__dirname + '/components/routes/api.js')(webserver, api);
require(__dirname + '/components/routes/admin.js')(webserver, api);
require(__dirname + '/components/routes/api.js')(webserver, api);

// load scripts
if (db === null) {
api.loadScriptsFromFile(__dirname + '/.data/scripts.json', __dirname + '/.data/sample_scripts.json').catch(function(err) {
console.log('Could not load scripts from file:', err);
process.exit(1);
});
}

webserver.get('/', function(req, res) {
res.render('instructions',{layout: null});
});
}

webserver.get('/', function(req, res) {
res.render('instructions',{layout: null});
});
connect();
2 changes: 1 addition & 1 deletion js/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ angular.module('howdyPro').factory('sdk', ['$http', '$q', function($http, $q) {

sdk.removeCommand = function(bot_id, command) {
command.deleted = true;
var uri = '/admin/api/scripts/' + command.id;
var uri = '/admin/api/scripts/' + (command.id);
return sdk.v2(uri, 'delete',{deleted:true}, true);
};

Expand Down
Loading