From 4e64583a0b6175d2c9a6729ffde1472dd55d389c Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Fri, 15 May 2015 12:58:13 +0800 Subject: Marked as 0.2.8 --- lib/auth.js | 8 +++--- lib/db.js | 25 +++++++++-------- lib/realtime.js | 15 ++++++++--- lib/response.js | 8 +++--- lib/temp.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/user.js | 4 +-- 6 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 lib/temp.js (limited to 'lib') diff --git a/lib/auth.js b/lib/auth.js index e7b0dc7c..456c9dff 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -27,23 +27,23 @@ function callback(accessToken, refreshToken, profile, done) { module.exports = passport.use(new FacebookStrategy({ clientID: config.facebook.clientID, clientSecret: config.facebook.clientSecret, - callbackURL: config.domain + config.facebook.callbackPath + callbackURL: config.getserverurl() + config.facebook.callbackPath }, callback)); //twitter passport.use(new TwitterStrategy({ consumerKey: config.twitter.consumerKey, consumerSecret: config.twitter.consumerSecret, - callbackURL: config.domain + config.twitter.callbackPath + callbackURL: config.getserverurl() + config.twitter.callbackPath }, callback)); //github passport.use(new GithubStrategy({ clientID: config.github.clientID, clientSecret: config.github.clientSecret, - callbackURL: config.domain + config.github.callbackPath + callbackURL: config.getserverurl() + config.github.callbackPath }, callback)); //dropbox passport.use(new DropboxStrategy({ clientID: config.dropbox.clientID, clientSecret: config.dropbox.clientSecret, - callbackURL: config.domain + config.dropbox.callbackPath + callbackURL: config.getserverurl() + config.dropbox.callbackPath }, callback)); \ No newline at end of file diff --git a/lib/db.js b/lib/db.js index d763eb83..d924cdf8 100644 --- a/lib/db.js +++ b/lib/db.js @@ -18,10 +18,7 @@ var db = { }; function getDBClient() { - if (config.debug) - return new pg.Client(config.postgresqlstring); - else - return new pg.Client(process.env.DATABASE_URL); + return new pg.Client(process.env.DATABASE_URL || config.postgresqlstring); } function readFromFile(callback) { @@ -49,12 +46,14 @@ function newToDB(id, owner, body, callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } var newnotequery = util.format(insertquery, id, owner, body); //console.log(newnotequery); client.query(newnotequery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("new note to db failed: " + err); @@ -62,7 +61,6 @@ function newToDB(id, owner, body, callback) { if (config.debug) console.log("new note to db success"); callback(null, result); - client.end(); } }); }); @@ -72,23 +70,25 @@ function readFromDB(id, callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } var readquery = util.format(selectquery, id); //console.log(readquery); client.query(readquery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("read from db failed: " + err); } else { //console.log(result.rows); if (result.rows.length <= 0) { - callback("not found note in db", null); + callback("not found note in db: " + id, null); } else { - console.log("read from db success"); + if(config.debug) + console.log("read from db success"); callback(null, result); - client.end(); } } }); @@ -99,12 +99,14 @@ function saveToDB(id, title, data, callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } var savequery = util.format(updatequery, title, data, id); //console.log(savequery); client.query(savequery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("save to db failed: " + err); @@ -112,7 +114,6 @@ function saveToDB(id, title, data, callback) { if (config.debug) console.log("save to db success"); callback(null, result); - client.end(); } }); }); @@ -122,10 +123,12 @@ function countFromDB(callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } client.query(countquery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("count from db failed: " + err); @@ -134,9 +137,9 @@ function countFromDB(callback) { if (result.rows.length <= 0) { callback("not found note in db", null); } else { - console.log("count from db success"); + if(config.debug) + console.log("count from db success"); callback(null, result); - client.end(); } } }); diff --git a/lib/realtime.js b/lib/realtime.js index 303eb6a6..e3800275 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -81,10 +81,11 @@ function getStatus(callback) { break; } } - if (!found) + if (!found) { distinctaddresses.push(value.address); - if(!found && value.login) - distinctregusers++; + if(value.login) + distinctregusers++; + } }); User.getUserCount(function (err, regcount) { if (err) { @@ -372,11 +373,19 @@ function connection(socket) { switch (op.origin) { case '+input': case '+delete': + case '+transpose': case 'paste': case 'cut': case 'undo': case 'redo': case 'drag': + case '*compose': + case 'case': + case '+insertLine': + case '+swapLine': + case '+joinLines': + case '+duplicateLine': + case '+sortLines': notes[notename].socks.forEach(function (sock) { if (sock != socket) { if (config.debug) diff --git a/lib/response.js b/lib/response.js index 458ed01f..a7dcc023 100644 --- a/lib/response.js +++ b/lib/response.js @@ -17,16 +17,16 @@ var Note = require("./note.js"); //public var response = { errorForbidden: function (res) { - res.status(403).send("Forbidden, oh no.") + res.status(403).send("Forbidden, oh no."); }, errorNotFound: function (res) { - responseError(res, "404", "Not Found", "oops.") + responseError(res, "404", "Not Found", "oops."); }, errorInternalError: function (res) { - responseError(res, "500", "Internal Error", "wtf.") + responseError(res, "500", "Internal Error", "wtf."); }, errorServiceUnavailable: function (res) { - res.status(503).send("I'm busy right now, try again later.") + res.status(503).send("I'm busy right now, try again later."); }, newNote: newNote, showFeatures: showFeatures, diff --git a/lib/temp.js b/lib/temp.js new file mode 100644 index 00000000..90d9343f --- /dev/null +++ b/lib/temp.js @@ -0,0 +1,83 @@ +//temp +//external modules +var mongoose = require('mongoose'); + +//core +var config = require("../config.js"); + +// create a temp model +var model = mongoose.model('temp', { + id: String, + data: String, + created: Date +}); + +//public +var temp = { + model: model, + findTemp: findTemp, + newTemp: newTemp, + removeTemp: removeTemp, + getTempCount: getTempCount +}; + +function getTempCount(callback) { + model.count(function(err, count){ + if(err) callback(err, null); + else callback(null, count); + }); +} + +function findTemp(id, callback) { + model.findOne({ + id: id + }, function (err, temp) { + if (err) { + console.log('find temp failed: ' + err); + callback(err, null); + } + if (!err && temp) { + callback(null, temp); + } else { + console.log('find temp failed: ' + err); + callback(err, null); + }; + }); +} + +function newTemp(id, data, callback) { + var temp = new model({ + id: id, + data: data, + created: Date.now() + }); + temp.save(function (err) { + if (err) { + console.log('new temp failed: ' + err); + callback(err, null); + } else { + console.log("new temp success: " + temp.id); + callback(null, temp); + }; + }); +} + +function removeTemp(id, callback) { + findTemp(id, function(err, temp) { + if(!err && temp) { + temp.remove(function(err) { + if(err) { + console.log('remove temp failed: ' + err); + callback(err, null); + } else { + callback(null, null); + } + }); + } else { + console.log('remove temp failed: ' + err); + callback(err, null); + } + }); +} + +module.exports = temp; \ No newline at end of file diff --git a/lib/user.js b/lib/user.js index 1d7f11d2..11064506 100644 --- a/lib/user.js +++ b/lib/user.js @@ -37,7 +37,7 @@ function findUser(id, callback) { console.log('find user failed: ' + err); callback(err, null); } - if (!err && user != null) { + if (!err && user) { callback(null, user); } else { console.log('find user failed: ' + err); @@ -65,7 +65,7 @@ function newUser(id, profile, callback) { function findOrNewUser(id, profile, callback) { findUser(id, function(err, user) { - if(err || user == null) { + if(err || !user) { newUser(id, profile, function(err, user) { if(err) { console.log('find or new user failed: ' + err); -- cgit v1.2.3