diff --git a/lib/helperLib.js b/lib/helperLib.js index c36263c..0d0134b 100644 --- a/lib/helperLib.js +++ b/lib/helperLib.js @@ -27,11 +27,6 @@ console.log("\n\n"); module.exports = { - // createUser: function(username, email, password, callback) { - // var user = {username: username, email: email - // , password: encryptPassword(password)}; - // db.insertOne('users', user, callback); - // }, createUser: function(username, password, callback){ User.addUser(username, password, callback); }, @@ -44,137 +39,130 @@ module.exports = { io = require('socket.io').listen(server); io.sockets.on('connection', function(socket){ - socket.on("COMMIT", function(){ - makeCommit(); - socket.emit("commitReceived"); + socket.AUTHOR = {}; //anonymous. + + socket.on("logIn", function(data){ + socket.AUTHOR = data; + attachLogInListeners(socket); + }); + + socket.on('nodeRequest', function(data){ + var nodes = Node.findAndSocketSend(socket); //finds, then sends through socket. }); socket.on("revHistoryRequest", function(subRootId){ getAndSendRevHistory(subRootId, socket); }); - //socket.emit('news', {hello: "world"}); - socket.on('nodeRequest', function(data){ - var nodes = Node.findAndSendSocket(socket); //finds, then sends through socket. - //var nodes = {'keep': 'calm'}; - //socket.emit('nodeData', nodes); (emit is in findNodes); - }) - - socket.on("edit", function(data){ - var id = data[0]; - var newText = data[1]; - console.log("\n\n\n\n edit received:" + newText); - Node.updateText(id, newText); - socket.broadcast.emit("edit", [id, newText]); - - }); - - //This works for all ids except negative ids. - socket.on("editing", function(id){ - socket.broadcast.emit("editing", id); - }); - - - socket.on("blurred", function(data){ - // console.log('\nBLURRED\n') - socket.broadcast.emit("blurred", data); - var id = data[0]; - var text = data[1]; - Node.updateText(id, text); - }); - - //I'm pretty sure i don't use this anywhere. - socket.on('nodeInsert', function(data){ - Node.addNode("testText", [], [], function(){}); - }); - - socket.on("transclude", function(data){ - var parId = data[0]; - var transcludeId = data[2]; - var newIndex = data[1]; - var now = Date.now(); - Node.updateParent(parId, transcludeId ,newIndex, now ); - }) - - - - socket.on("newNode", function(data){ - //data[0] = [parID, newindex] . data[1] = - var modelJson = data[1]; //(includes the negative ID to find later); - var parId = data[0][0]; - var newIndex = data[0][1]; - - var now = Date.now(); - var callback = function(err, instance, now){ - socket.emit("updateReceived", [modelJson._id ,instance, data[0]]); - socket.broadcast.emit("newNode", [ data[0] ,instance]); - //io.sockets.emit("newNode", [data[0], instance]) - //I'm going to have to update the parent Model on this as well... - //(and therefore, broadcast the array...) - Node.updateParent(parId, instance._id ,newIndex, now ); - } - Node.addNode(modelJson.text, modelJson.children, modelJson.parents, callback); - - }); - - socket.on("removeNode", function(data){ - // console.log("removeNode!!!"); - var thisId = data[0]; - var thisIndex = data[1]; - var parId = data[2]; - socket.broadcast.emit("removeNode", data); - Node.removeNode(thisId, thisIndex, parId); - - }); - - socket.on("movedNode", function(data){ - // var ids = [thisModel.get("_id"), oldParModel.get("_id"), newParModel.get("_id")]; - // var arrays = [thisModel.get("parents"), oldParModel.get("children"), newParModel.get("children")]; - // var data = [ids, arrays]; - Node.moveNode(data[0], data[1]); - socket.broadcast.emit("movedNode", [data[0], data[2]]); - - - - }); - - socket.on("getTimeHash", function(){ - var timeHash = getTimeHash(); - - socket.emit("timeHash", timeHash); - }) - - }); - // io.configure(function (){ - // io.set('authorization', function (handshakeData, callback) { - // if (handshakeData.headers.cookie) { - // handshakeData.cookie = cookie.parse(decodeURIComponent(handshakeData.headers.cookie)); - // handshakeData.sessionID = handshakeData.cookie['connect.sid']; - // sessionStore.get(handshakeData.sessionID, function (err, session) { - // if (err || !session) { - // return callback(null, false); - // } else { - // handshakeData.session = session; - // console.log('session data', session); - // return callback(null, true); - // } - // }); - // } - // else { - // return callback(null, false); - // } - // }); - // }); - }, + }, setUpDB: function(){ Node.setUpDB(); //getTimeHash(); } - - - - +} + + + + +function attachLogInListeners(socket){ + socket.on("COMMIT", function(){ + makeCommit(); + socket.emit("commitReceived"); + }); + + //Used when a node is split... + socket.on("edit", function(data){ + var id = data[0]; + var newText = data[1]; + console.log("\n\n\n\n edit received:" + newText); + Node.updateText(id, newText); + socket.broadcast.emit("edit", [id, newText]); + + }); + + //This works for all ids except negative ids. + socket.on("editing", function(data){ //data=[id, Author.name] + socket.broadcast.emit("editing", data); + }); + + + socket.on("blurred", function(data){ + socket.broadcast.emit("blurred", data); + + var id = data[0]; + var text = data[1]; + var author = data[2]; + + Node.updateText(id, text, author._id); + }); + + + + + // ADD AUTHOR INFORMATION + // socket.on("transclude", function(data){ + // var parId = data[0]; + // var transcludeId = data[2]; + // var newIndex = data[1]; + // var now = Date.now(); + // Node.updateParent(parId, transcludeId ,newIndex, now ); + + // }); + + + + socket.on("newNode", function(data){ + //data[0] = [parID, newindex] . data[1] = modelJSON + var modelJson = data[1]; //(includes the negative ID to find later); + var parId = data[0][0]; + var newIndex = data[0][1]; + var Author = data[2]; + + var now = Date.now(); + + var callback = function(err, instance, now){ + socket.emit("updateReceived", [modelJson._id ,instance, data[0]]); + socket.broadcast.emit("newNode", [ [parId,newIndex] , instance ]); + + //io.sockets.emit("newNode", [data[0], instance]) + //I'm going to have to update the parent Model on this as well... + //(and therefore, broadcast the array...) + Node.updateParent(parId, instance._id ,newIndex, now ); + } + + Node.addNode(modelJson.text, modelJson.children, modelJson.parents, modelJson.author._id ,callback); + + }); + + socket.on("removeNode", function(data){ + // console.log("removeNode!!!"); + var thisId = data[0]; + var thisIndex = data[1]; + var parId = data[2]; + var author = data[3]; data[3] = null; + socket.broadcast.emit("removeNode", data); + Node.removeNode(thisId, thisIndex, parId, author._id); + + }); + + socket.on("movedNode", function(data){ + // var ids = [thisModel.get("_id"), oldParModel.get("_id"), newParModel.get("_id")]; + // var arrays = [thisModel.get("parents"), oldParModel.get("children"), newParModel.get("children")]; + // var indices = [dragIndex, dropIndex]; + // var data = [ids, arrays, indices, CurrentUser]; + + Node.moveNode(data[0], data[1],data[3]._id); + socket.broadcast.emit("movedNode", [data[0], data[2]]); + + + + }); + socket.on("getTimeHash", function(){ + var timeHash = getTimeHash(); + + socket.emit("timeHash", timeHash); + }) ; } diff --git a/lib/revAlgorithm.js b/lib/revAlgorithm.js index e0617b7..caaf6e4 100644 --- a/lib/revAlgorithm.js +++ b/lib/revAlgorithm.js @@ -30,7 +30,8 @@ function updateGlobals(rootSnaps){ //This should only be called once per ID. function FetchSelfAndChildrenBack(rootId, callback){ - MySnap.find({cur_id: rootId}).sort({timestamp: 1}).exec(function(err, rootSnaps){ + MySnap.find({cur_id: rootId}).populate('authorId','_id google.name' ).sort({timestamp: 1}). + exec(function(err, rootSnaps){ if(err){console.log("ERROR HERE")} updateGlobals(rootSnaps); diff --git a/lib/routes.js b/lib/routes.js index 34ea3f9..1164ee2 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,6 +1,9 @@ module.exports = function(app, passport) { - app.get('/', function(req, res) { res.render('index'); } ); + app.get('/', function(req, res) { + var user = {bleh: "blah"} + res.render('index', {data: user}); + }); // route for logging out app.get('/logout', function(req, res) { @@ -12,13 +15,33 @@ module.exports = function(app, passport) { app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] })); + //scope?? //(callBack = None). - // the callback after google has authenticated the user - app.get('/auth/google/callback', - passport.authenticate('google', { - successRedirect : '/', - failureRedirect : '/' - })); + + app.get('/auth/google/callback', + passport.authenticate('google', { + successRedirect : '/', + failureRedirect : '/' + })); + + // // the callback after google has authenticated the user + // app.get('/auth/google/callback', function(req, res, next){ + // passport.authenticate('google', function(err, user, info) { + // if(err){return next(err);} + // if(!user){return res.render('index.ejs', { data: {failed: "true"} } ) } + // req.logIn(user, function(err){ + // if(err) {return next(err); } + // return res.redirect("/"); + // // return res.render('index.ejs', { data: user } ); + // }); + // })(req, res, next); //have to actually call the passport method. + // }); + + app.post("/ajaxlogin", function(req, res){ + console.log("AJAXLOGIN"); + console.log("req.isAuthenticated() = " + req.isAuthenticated()); + res.send(req.user); + }) }; // route middleware to make sure a user is logged in diff --git a/models/Node.js b/models/Node.js index 1497895..9551046 100644 --- a/models/Node.js +++ b/models/Node.js @@ -5,7 +5,8 @@ var NodeSchema = new mongoose.Schema({ children: {type: Array}, parents: {type: Array}, markdown: {type: Boolean}, - timestamp: {type: Number} + timestamp: {type: Number}, + author: {type: String, ref: 'User'} //authorId is populated with limited AuthorObject }); var MyNode = mongoose.model('nodes', NodeSchema); @@ -19,7 +20,7 @@ var addSnap = snapModule.addSnap; // Exports module.exports.MyNode = MyNode; -module.exports.findAndSendSocket = findAndSendSocket +module.exports.findAndSocketSend = findAndSocketSend module.exports.setUpDB = setUpDB; module.exports.moveNode = moveNode; @@ -38,10 +39,11 @@ module.exports.addNode = addNode; // return rootID; // } -function findAndSendSocket(socket){ +function findAndSocketSend(socket){ var nodes = {'keeping': 'calm'} - MyNode.find(function(err, nodes){ + MyNode.find().populate('authorId','_id google.name' ).exec(function(err, nodes){ if(!err){ + // require("underscore").each(nodes, function(node){console.log(node)}); socket.emit('nodeData', nodes) return {'hell': 'yes'} }else{ @@ -57,12 +59,10 @@ function findAndSendSocket(socket){ function setUpDB(){ MyNode.remove({}, function(err) { console.log('collection removed') }); MySnap.remove({}, function(err) { console.log('collection removed') }); - - // addNode("0root", [], ["123456"]); - // addNode("Welcolme!", [], [rootNode._id]); - - addNode("0root", [], ["123456"], function(err, rootNode){ - addNode("Welcolme!", [], [rootNode._id], function(err, firstBullet){ + var curtisId = "53e4079cd7dbc73d16c87c53"; + + addNode("0root", [], ["123456"], curtisId ,function(err, rootNode){ + addNode("Welcolme!", [], [rootNode._id], curtisId ,function(err, firstBullet){ rootNode.children = [firstBullet._id] var now = rootNode.timestamp = firstBullet.timestamp addSnap(rootNode, now); @@ -79,20 +79,24 @@ function setUpDB(){ -function moveNode(ids, arrays){ - var thisId = ids[0]; //draggedId +function moveNode(ids, arrays, authorId){ var now = Date.now(); + + var thisId = ids[0]; //draggedId MyNode.findById(thisId, null, function(err, node){ + node.author = authorId; // node.timestamp = now; node.parents = arrays[0]; node.save(); }); + var oldParId = ids[1]; MyNode.findById(oldParId, null, function(err, node){ node.timestamp = now; node.children = arrays[1]; node.save(); }); + var newParId = ids[2]; MyNode.findById(newParId, null, function(err, node){ node.timestamp = now; @@ -101,8 +105,9 @@ function moveNode(ids, arrays){ }); } -function removeNode(thisId, thisIndex, parId){ +function removeNode(thisId, thisIndex, parId, authorId){ var now = Date.now(); + MyNode.findById(parId, null, function(err, parNode){ if(err || parNode == null){ return; @@ -114,14 +119,16 @@ function removeNode(thisId, thisIndex, parId){ parNode.save(); }); + MyNode.findById(thisId, null, function(err, delNode){ if(err || delNode == null){ return; } + delNode.author = authorId; delNode.timestamp = now; if(delNode.parents.length ==1 ){//(this if statement is technically redundant) // delNode.remove(); - delNode.parnets = []; //(this is how deletion is represented). + delNode.parents = []; //(this is how deletion is represented). delNode.save(); } else{ //this is the condition that we'll have to take care of if there are dups. @@ -131,18 +138,19 @@ function removeNode(thisId, thisIndex, parId){ }) } -function updateText(id, newText){ +function updateText(id, newText, authorId){ MyNode.findById(id, null, function(err, node){ if(err || node == null){ return; } node.timestamp = Date.now(); node.text = newText; + node.authorId = authorId; node.save(); }); } -function updateParent(parId, newId ,newIndex, now){ +function updateParent(parId, newId ,newIndex,now){ MyNode.findById(parId, null, function(err, parNode){ if(err || parNode == null){ return; @@ -153,13 +161,14 @@ function updateParent(parId, newId ,newIndex, now){ }) } //add Node to the DB. -function addNode(text, children, parents, callback){ +function addNode(text, children, parents, authorId, callback){ var instance = new MyNode(); instance.text = text; instance.children = children; instance.parents = parents; instance.markdown = 0; instance.timestamp = Date.now(); + instance.authorId = authorId; instance.save(function (err) { if (err) { diff --git a/models/Snap.js b/models/Snap.js index f1a5158..bed7786 100644 --- a/models/Snap.js +++ b/models/Snap.js @@ -7,7 +7,8 @@ var SnapSchema = new mongoose.Schema({ markdown: {type: Boolean}, timestamp: {type: Number}, - cur_id: {type: String} + cur_id: {type: String}, + authorId: {type: String, ref: 'User'} }); var MySnap = mongoose.model('snaps', SnapSchema); @@ -29,6 +30,7 @@ function addSnap(node, now, remove, callback){ instance.markdown = node.markdown; instance.cur_id = node._id; instance.timestamp = now; + instance.authorId = node.authorId; if(remove){ diff --git a/public/js/app.js b/public/js/app.js index 8d76653..a89500a 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,6 +1,5 @@ $(function(){ //alert("jquery works"); - var myRouter; $("#COMMIT").click(function(){ @@ -13,11 +12,21 @@ $(function(){ $(e.target).attr("data-id", rootId); }); + $("body").on("click", ".snapLI", function(e){ + $(e.target).addClass("selectedSnap"); + }); + $(".toggleSidebar").click(function(){ $("#mainPanel").toggleClass("floatRight centering"); $("#metaSidebar").toggleClass("hideBar"); }); + + + + + + $("#markdownButton").click(function(){ vo.thisView.createMarkDownBullet(); }); @@ -37,6 +46,9 @@ $(function(){ + + + $("body").on("mouseover", "a.expandCollapse", function(event){ // console.log("bleh"); // debugger; @@ -65,18 +77,17 @@ $(function(){ var that = this; voInitializer(that, event); var id = $(event.target).closest("li").attr("data-id"); - console.log("ABOUT TO EMIT 'EDITING' "); - console.log(id); - socket.emit("editing", id); + + socket.emit("editing", [id, CurrentUser._id]); }); $("body").on("blur", "textarea", function(event){ var thisLI = $(event.target).closest("li"); var id = thisLI.attr("data-id"); var text = thisLI.children().children("textarea").val(); - //console.log(id); - //alert("blurred ID" + id + text); - socket.emit("blurred", [id, text]); - $("textarea").textareaAutoExpand(); + $("textarea").textareaAutoExpand(); + + socket.emit("blurred", [id, text, CurrentUser]); + }); $("body").on("click", ".transclude", function(event){ //alert(vo.thisId); diff --git a/public/js/libs/myLib/addNode.js b/public/js/libs/myLib/addNode.js index f52d059..b3d4438 100644 --- a/public/js/libs/myLib/addNode.js +++ b/public/js/libs/myLib/addNode.js @@ -8,6 +8,7 @@ addNode = function(botStr, topStr){ , text: botStr , parents: [vo.parentId] , children: [] + , author: CurrentUser }; @@ -27,7 +28,7 @@ addNode = function(botStr, topStr){ vo.parentModel.get("children").insert(vo.thisIndex + 1, randomId); - socket.emit("newNode", [[vo.parentId, vo.thisIndex+1], modelJSON ]); + socket.emit("newNode", [ [vo.parentId, vo.thisIndex+1] , modelJSON ]); var parentViews = vo.parentModel.get("views"); diff --git a/public/js/libs/myLib/moveNode.js b/public/js/libs/myLib/moveNode.js index 32f8e4b..fe3771b 100644 --- a/public/js/libs/myLib/moveNode.js +++ b/public/js/libs/myLib/moveNode.js @@ -26,10 +26,9 @@ var moveNode = function(thisModel, dragIndex, oldParModel, newParModel, dropInde var ids = [thisModel.get("_id"), oldParModel.get("_id"), newParModel.get("_id")]; var arrays = [thisModel.get("parents"), oldParModel.get("children"), newParModel.get("children")]; var indices = [dragIndex, dropIndex]; - var data = [ids, arrays, indices]; - console.log("about to emit data!!"); - console.log(data); - if(chr){ + var data = [ids, arrays, indices, CurrentUser]; + + if(chr){ socket.emit("movedNode", data); } diff --git a/public/js/libs/myLib/removeNode.js b/public/js/libs/myLib/removeNode.js index 034895d..6dc7de6 100644 --- a/public/js/libs/myLib/removeNode.js +++ b/public/js/libs/myLib/removeNode.js @@ -9,8 +9,8 @@ return parId != vo.parentId; }); vo.thisModel.set("parents", newParents); - if(!broadcast){ - socket.emit("removeNode", [vo.thisId, vo.thisIndex, vo.parentId]); + if(!broadcast){ //broadCast=true means we received this update from somebody else. + socket.emit("removeNode", [vo.thisId, vo.thisIndex, vo.parentId, CurrentUser]); } } @@ -22,7 +22,8 @@ var upDateParentModelViews = function(vo, broadcast){ vo.parentModel.set("children", children); - if(!broadcast){ + + if(!broadcast){ //broadCast=true means we received this update from somebody else. //UI STUFF console.log("what"); if( !vo.thisLI.is(":first-child") ){ @@ -36,6 +37,7 @@ var upDateParentModelViews = function(vo, broadcast){ } } + _.each(vo.parentModel.get("views"), function(parentView){ parentView.removeNode(vo.thisIndex); }); diff --git a/public/js/router.js b/public/js/router.js index 4045786..e2106da 100644 --- a/public/js/router.js +++ b/public/js/router.js @@ -5,191 +5,56 @@ socketEvents: _.extend({}, Backbone.Events), routes: { - '': 'index', - ':id': "index" + '': 'initialize', + ':id': "viewRoot" }, initialize: function(){ - //alert('initializing router'); + + socket = io.connect(); + this.setUpSocket(); + + + $.ajax({ + url: "/ajaxlogin", + type: "POST", + + complete: function(){ + }, + success: function(data){ + console.log("AjaxLoginDATA"); + if(typeof data == "string"){ + $("textarea").attr("disabled", true); + } //User is not logged in. + if(typeof data == "object"){//we've got the userObject. + $("#googleButton").parent().html("Logout "); + socket.emit("logIn", data); //logs the user into the socket. + + + data.google.id = null; + data.google.token = null; + data.google.email = null; + CurrentUser = data; + + console.log("CURRENT USER"); + console.log(CurrentUser); + } + }, + + + error: function(data){ + console.log("ERROR- AjaxLoginDATA"); + } + }); + }, - index: function(otherID){ - var that = this; - //alert('index'); - //this.viewRoot(); - if(socket){ //Most of the time. - that.viewRoot(otherID); - return; - } + // index: function(otherID){ + // var that = this; + + // otherID = otherID || - //(initialization) - socket = io.connect(); - socket.emit("nodeRequest"); - - socket.on('nodeData', function(data){ - //alert("data"); - console.log(data); - nodesCollection = new NodesCollection(data); - var id = nodesCollection.findWhere({text: "0root"}).get("_id"); - if(otherID){ - id = otherID - } - - - that.viewRoot(id); - }); - - - socket.on("commitReceived", function(){ - alert("commitReceived"); - }); - - socket.on("revHistory", function(data){ - alert("revHistory!!"); - console.log("revHistory!!"); - snapHash = data[0]; - timeHash = data[1]; - console.log("snapHash"); - console.log(snapHash); - console.log("timeHash"); - console.log(timeHash); - - var list = ""; - _.each(Object.keys(timeHash), function(timestamp){ - list += "