Skip to content

Instantly share code, notes, and snippets.

@abcdabcd987
Created October 25, 2013 12:44
Show Gist options
  • Save abcdabcd987/7154105 to your computer and use it in GitHub Desktop.
Save abcdabcd987/7154105 to your computer and use it in GitHub Desktop.
/*
* GET message listing => show()
* POST meaasge posting => send()
*/
var settings = require('../settings');
var group = require('../models/group');
var mesg = require('../models/mesg');
var md = require('markdown').markdown.toHTML;
var moment = require('moment');
var checkSecret = function(name, secret, cb) {
group.findOne({name: name}, function(err, found) {
if (err || !found) return cb('Group not found');
if (found.secret != secret) return cb('Bad secret');
return cb();
});
};
var getAndRenderMessage = function(name, skip, limit) {
console.log(name, skip, limit);
mesg.find({group: name}).exec(function(err, list) {
console.log("=======================");
});
console.log("-----------------------");
mesg.find({group: name}).sort('-create').skip(skip).limit(limit)
.find(function(err, mesglist) {
console.log("ooooooooooooooooooooooooo");
var htmls = [];
var latest = info.timestamp = mesglist[0] ? Number(mesglist[0].create) : 0;
mesglist.forEach(function(item) {
var newitem = {
content: md(item.content),
create: moment(item.create).format("MM/DD HH:mm"),
author: item.author
};
htmls.push(res.render('mesgsingle', newitem));
});
console.log(htmls, latest);
return {htmls: htmls, latest: latest};
});
};
exports.welcome = function(req, res) {
var name = req.params.name;
var secret = req.params.secret;
var info = {title: name, name: name, secret: secret};
var fallback = function(err) {
req.flash('error', err);
return res.redirect('/');
}
checkSecret(name, secret, function(err) {
if (err) return fallback(err);
return res.render('mesgwelcome', info);
});
};
exports.redirect = function(req, res) {
var name = req.params.name;
var secret = req.params.secret;
var user = req.body.user;
if (user == '') {
req.flash('error', 'Fullname needed');
return res.redirect('back');
} else {
req.flash('error', 'Save this page to bookmarks for further writing.');
return res.redirect('/w/' + name + '/' + secret + '/' + user);
}
};
exports.show = function(req, res) {
var name = req.params.name;
var secret = req.params.secret;
var user = req.params.user;
var info = {title: name, name: name, secret: secret, user: user};
var page = info.page = Number(req.params.page || '1');
var fallback = function(err) {
req.flash('error', err);
return res.redirect('/');
}
checkSecret(name, secret, function(err) {
if (err) return fallback(err);
mesg.find({group: name}).count(function(err, count) {
if (err) {
console.log(err);
return fallback('Database error');
}
info.totpage = Math.ceil(count / settings.perpage);
var skip = settings.perpage * (page - 1);
console.log("...");
var newmesgs = getAndRenderMessage(name, skip, settings.perpage);
console.log(newmesgs);
info.timestamp = page == 1 ? newmesgs.latest : Number(Date.now());
return res.render('mesglist', newmesgs.htmls);
});
});
};
exports.send = function (req, res) {
var name = req.params.name;
var secret = req.params.secret;
var user = req.params.user;
var info = {title: name, name: name, secret: secret, user: user};
var fallback = function(err) {
req.flash('error', err);
return res.redirect('back');
}
checkSecret(name, secret, function(err) {
if (err) return fallback(err);
if (!req.body.content) return res.render('mesgtextarea', info);
mesg.findOne().sort('-id').exec(function(err, last) {
var item = new mesg(req.body);
item.id = last ? last.id + 1 : 0;
item.author = user;
item.group = name;
item.save(function(err) {
if (err) {
console.log(err);
fallback('Unknown error');
}
return res.redirect('back');
});
});
});
};
exports.notification = function(req, res) {
var name = req.params.name;
var secret = req.params.secret;
if (!req.query.timestamp) return res.send('0');
checkSecret(name, secret, function(err) {
if (err) return res.send('0');
mesg.findOne({group: name}).sort('-create').exec(function(err, last) {
if (!last) return res.send('0');
if (err || Number(last.create) <= Number(req.query.timestamp)) return res.send('0');
else return res.send('1');
});
});
}
@abcdabcd987
Copy link
Author

[abcdabcd987@clq-desktop write-group]$ node app.js 
write-group server listening on port 3000
...
hello 0 25
-----------------------
undefined

/home/abcdabcd987/github/write-group/node_modules/mongoose/lib/utils.js:419
        throw err;
              ^
TypeError: Cannot read property 'latest' of undefined
    at /home/abcdabcd987/github/write-group/routes/mesg.js:94:50
    at /home/abcdabcd987/github/write-group/node_modules/mongoose/lib/utils.js:414:16
    at /home/abcdabcd987/github/write-group/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection.js:594:7
    at /home/abcdabcd987/github/write-group/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:153:27
    at /home/abcdabcd987/github/write-group/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1670:9
    at Server.Base._callHandler (/home/abcdabcd987/github/write-group/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:382:41)
    at /home/abcdabcd987/github/write-group/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:472:18
    at MongoReply.parseBody (/home/abcdabcd987/github/write-group/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/home/abcdabcd987/github/write-group/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:430:20)
    at EventEmitter.emit (events.js:95:17)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment