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

Rethink micha #73

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
607 changes: 328 additions & 279 deletions api/items.js

Large diffs are not rendered by default.

92 changes: 47 additions & 45 deletions api/lock.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
var globalState = require("../server"),
app = globalState.app,
express = globalState.express,
db = globalState.db;
express = globalState.express;

/**
* @desc various dependencies
*/
var q = require('q');
var r = require('rethinkdb');
var _ = require('underscore');
var __ = require('lodash');

var dbInfo = require("../database/dbInfo");
var dbHelper = require("../database/utils");
__.assign(root, dbHelper);
var dbUtils = require("../database/utils");


/**
Expand All @@ -28,23 +27,24 @@ app.post('/api/requestLock', express.json(), function (req, res){
res.send({});
} else {
var syncLockPromise;
db.itemdb.findOne({uid:req.body.uid}, function (err, item){
if(err||!item){ console.log('(error requesting lock on item) '+err); }
else {
if (item.lock){
//already has a lock
console.log('we are here....')
res.send(item);
} else {
//does not have lock yet
syncLockPromise = changeLock(item,req.body.email, true);
q.when(syncLockPromise).then(function(){
res.send(item);
});

}
}
});
r.table("items").get(req.body.uid)
.run(req.db, function (err, item){
if(err||!item){ console.log('(error requesting lock on item) '+err); }
else {
if (item.lock){
//already has a lock
console.log('we are here....')
res.send(item);
} else {
//does not have lock yet
syncLockPromise = dbUtils.changeLock(req.db, item,req.body.email, true);
q.when(syncLockPromise).then(function(){
res.send(item);
});

}
}
});
}
});

Expand All @@ -60,16 +60,17 @@ app.post('/api/removeLock', express.json(), function (req, res){
} else {
var syncLockPromise;
if (req.body.uid) {
console.log('removing lock for item: '+req.body.uid)
db.itemdb.findOne({uid:req.body.uid}, function (err, item){
if(err||!item){ console.log('(error removing lock on item) '+err); }
else {
syncLockPromise = changeLock(item,req.body.email, false);
q.when(syncLockPromise).then(function(){
res.send(item);
});
}
});
console.log('removing lock for item: '+req.body.uid);
r.table("items").get(req.body.uid)
.run(req.db, function (err, item){
if(err||!item){ console.log('(error removing lock on item) '+err); }
else {
syncLockPromise = dbUtils.changeLock(req.db, item,req.body.email, false);
q.when(syncLockPromise).then(function(){
res.send(item);
});
}
});
}
}
});
Expand All @@ -85,20 +86,21 @@ app.post('/api/pickLock', express.json(), function (req, res){
res.send({});
} else {
var syncLockPromise;
console.log('breaking lock for item: '+req.body.uid)
db.itemdb.findOne({uid:req.body.uid}, function (err, item){
if(err||!item){ console.log('(error removing lock on item) '+err); }
else {
if(_.contains(item.owners, req.body.email)){
syncLockPromise = changeLock(item,req.body.email, false);
q.when(syncLockPromise).then(function(){
res.send(item);
});
} else {
//send the owner an email
}
}
});
console.log('breaking lock for item: '+req.body.uid);
r.table("items").get(req.body.uid)
.run(req.db, function (err, item){
if(err||!item){ console.log('(error removing lock on item) '+err); }
else {
if(_.contains(item.owners, req.body.email)){
syncLockPromise = dbUtils.changeLock(req.db, item,req.body.email, false);
q.when(syncLockPromise).then(function(){
res.send(item);
});
} else {
//send the owner an email
}
}
});
}
});

82 changes: 41 additions & 41 deletions api/proposals.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var globalState = require("../server"),
app = globalState.app,
express = globalState.express,
db = globalState.db,
io = globalState.io;

/**
* @desc various dependencies
*/
var r = require('rethinkdb');
var __ = require('lodash');

var dbInfo = require("../database/dbInfo");
var dbHelper = require("../database/utils");
__.assign(root, dbHelper);

/**
* @api - proposal result
Expand All @@ -30,19 +29,19 @@ app.post('/api/proposalResult', express.json(), function (req, res) {
}
});
if(theOne.key===req.body.key) {
db.itemdb.find({uid:req.body.uid}, function (err, check) {
if (!check.length||check[0].uid!==req.body.uid) {
r.table("items").get(req.body.uid).run(req.db, function (err, check) {
if (check.uid!==req.body.uid) {
return res.send(500);
}
//it is there
var pushStuff = {};
pushStuff.uid=req.body.uid;
pushStuff[req.body.who]=req.body[req.body.who];

var extendedItem = __.cloneDeep(check[0]);
var extendedItem = __.cloneDeep(check);
_.extend(extendedItem,pushStuff);

syncItemPromise=updateItem(extendedItem, check[0]);
syncItemPromise=dbUtils.updateItem(req.db, extendedItem, check);
q.when(syncItemPromise).then(function(){
//check if the project has been approved by everyone
var approved = true;
Expand All @@ -52,13 +51,13 @@ app.post('/api/proposalResult', express.json(), function (req, res) {
console.log("approval is currently: "+approved);
if (approved){
//update the item
db.itemdb.find({uid:extendedItem.forUID}, function (err, check2){
r.table("items").get(extendedItem.forUID).run(req.db,function (err, check2){
if(err){ console.log('(error getting item) '+err); }else{
//clone and add approval
var approvalItem = __.cloneDeep(check2[0]);
var approvalItem = __.cloneDeep(check2);
_.extend(approvalItem,{approval:true});
console.log('approval item: '+approvalItem);
updateItem(approvalItem, check2[0]);
dbUtils.updateItem(req.db, approvalItem, check2);
}
});
}
Expand All @@ -80,50 +79,51 @@ app.post('/api/saveProposal', express.json(), function (req, res) {
if(!req.session.user){
res.send({});
} else {
req.body.uid=generateUID();
req.body.uid = dbUtils.generateUID();
proposalSecrets[req.body.uid]=[];
var keys={};

//put in the members
_(members).each(function(member){
req.body[member.name]={};
req.body[member.name].who = member.name;
keys[member.name] = generateUID();
keys[member.name] = dbUtils.generateUID();
});

db.itemdb.insert(req.body, function (err, doc) {
if(err){
console.log('(error saving proposal) '+err);
}else{
console.log('proposal: ' + doc.uid);
io.emit('newProposal', doc.uid);
r.table("items").insert(req.body)
.run(req.db, function (err, doc) {
if(err){
console.log('(error saving proposal) '+err);
}else{
console.log('proposal: ' + doc.uid);
io.emit('newProposal', doc.uid);

//email all members
_(members).each(function(member){
if (member.email){
var key = keys[member.name];

console.log('trying to send email to ' + member.email);
smtpTrans.sendMail({
from: 'Robot <[email protected]>',
to: member.email,
subject: 'new project proposal for colab',
text: 'text body',
html: "<p>Human,</p><p>A new proposal has been created. Please follow the link below to review everything, and fill in your decision and/or comments. The link was generated specifically for you so don't share. Please reply to me if you have any questions. I am a robot. Thank you.</p><p><a href='http://colablife.info/#/proposal/"+req.body.uid+"/"+key+"'>"+member.name+"'s response</a>"
}, function (err, doc){
if(err){ console.log(err); }else{
//email was sent!
proposalSecrets[req.body.uid].push({name:member.name,key:key});
console.log('Message sent: ' + doc.response);
}
});
//email all members
_(members).each(function(member){
if (member.email){
var key = keys[member.name];
console.log('trying to send email to ' + member.email);
smtpTrans.sendMail({
from: 'Robot <[email protected]>',
to: member.email,
subject: 'new project proposal for colab',
text: 'text body',
html: "<p>Human,</p><p>A new proposal has been created. Please follow the link below to review everything, and fill in your decision and/or comments. The link was generated specifically for you so don't share. Please reply to me if you have any questions. I am a robot. Thank you.</p><p><a href='http://colablife.info/#/proposal/"+req.body.uid+"/"+key+"'>"+member.name+"'s response</a>"
}, function (err, doc){
if(err){ console.log(err); }else{
//email was sent!
proposalSecrets[req.body.uid].push({name:member.name,key:key});
console.log('Message sent: ' + doc.response);
}
});

}
});
}
});

res.send(doc.uid);
}
});
res.send(doc.uid);
}
});
}
});

Expand Down
21 changes: 17 additions & 4 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ cd /vagrant
echo Provisioning system...
echo Installing prereq packages...
export DEBIAN_FRONTEND=noninteractive
# Mongo
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

#rethinkdb
source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -

sudo apt-get update
sudo apt-get install -y python-software-properties gcc make build-essential mongodb-10gen imagemagick
sudo apt-get install -y \
python-software-properties \
gcc \
make \
build-essential \
rethinkdb \
imagemagick

# Forget about apt packages for node, just grab a local copy of node
nodeVersion=0.12.7
Expand Down Expand Up @@ -39,6 +47,11 @@ npm install

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 465 -j REDIRECT --to-port 55465

sudo ln -sf /vagrant/conf/oratory_rethinkdb.conf /etc/rethinkdb/instances.d/oratory.conf
sudo mkdir /data
sudo chown -R rethinkdb:rethinkdb /data
sudo service rethinkdb start

#start the forever server
sudo npm install -g forever
touch users.js # this file must exist, but isn't checked in
Expand Down
Loading