Skip to content

Commit

Permalink
Coding style refine.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Feb 19, 2013
1 parent 1e4247f commit c71d612
Show file tree
Hide file tree
Showing 21 changed files with 467 additions and 602 deletions.
10 changes: 5 additions & 5 deletions controllers/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mailEvent.on("getMail", function () {
* @return {[type]}
*/
function trigger() {
mailEvent.trigger("getMail");
mailEvent.emit("getMail");
}

/**
Expand All @@ -93,7 +93,7 @@ function send_mail(data) {

function send_active_mail(who, token, name, email, cb) {
var sender = config.mail_sender;
var to = who;
var to = who;
var subject = config.name + '社区帐号激活';
var html = '<p>您好:<p/>' +
'<p>我们收到您在' + config.name + '社区的注册信息,请点击下面的链接来激活帐户:</p>' +
Expand All @@ -111,7 +111,7 @@ function send_active_mail(who, token, name, email, cb) {
}
function send_reset_pass_mail(who, token, name, cb) {
var sender = config.mail_sender;
var to = who;
var to = who;
var subject = config.name + '社区密码重置';
var html = '<p>您好:<p/>' +
'<p>我们收到您在' + config.name + '社区重置密码的请求,请在24小时内单击下面的链接来重置密码:</p>' +
Expand All @@ -132,7 +132,7 @@ function send_reset_pass_mail(who, token, name, cb) {

function send_reply_mail(who, msg) {
var sender = config.mail_sender;
var to = who;
var to = who;
var subject = config.name + ' 新消息';
var html = '<p>您好:<p/>' +
'<p>' +
Expand All @@ -155,7 +155,7 @@ function send_reply_mail(who, msg) {

function send_at_mail(who, msg) {
var sender = config.mail_sender;
var to = who;
var to = who;
var subject = config.name + ' 新消息';
var html = '<p>您好:<p/>' +
'<p>' +
Expand Down
26 changes: 12 additions & 14 deletions controllers/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var models = require('../models'),

var user_ctrl = require('./user');
var mail_ctrl = require('./mail');
var topic_ctrl = require('./topic');
var topic_ctrl = require('./topic');

var EventProxy = require('eventproxy');

Expand Down Expand Up @@ -52,7 +52,7 @@ exports.index = function (req, res, next) {
return next(err);
}
messages[i] = message;
proxy.trigger('message_ready');
proxy.emit('message_ready');
});
});
});
Expand Down Expand Up @@ -84,7 +84,7 @@ exports.mark_read = function (req, res, next) {
}
res.json({status: 'success'});
});
});
});
};

exports.mark_all_read = function (req, res, next) {
Expand All @@ -95,22 +95,20 @@ exports.mark_all_read = function (req, res, next) {
// TODO: 直接做update,无需查找然后再逐个修改。
Message.find({master_id: req.session.user._id, has_read: false}, function (err, messages) {
if (messages.length === 0) {
res.json({'status': 'success'});
res.json({'status': 'success'});
return;
}
var proxy = new EventProxy();
var done = function () {
res.json({'status': 'success'});
};
proxy.after('marked', messages.length, done);
proxy.after('marked', messages.length, function () {
res.json({'status': 'success'});
});
proxy.fail(next);
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
var message = messages[i];
message.has_read = true;
message.save(function (err) {
proxy.trigger('marked');
});
message.save(proxy.done('marked'));
}
});
});
};

function send_reply_message(master_id, author_id, topic_id) {
Expand Down Expand Up @@ -192,7 +190,7 @@ function get_message_by_id(id, cb) {
if (!author || !topic) {
message.is_invalid = true;
}
return cb(null, message);
return cb(null, message);
};
proxy.assign('author_found', 'topic_found', done);
proxy.fail(cb);
Expand Down
32 changes: 16 additions & 16 deletions controllers/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.signup = function (req, res, next) {
email = sanitize(email).xss();
var re_pass = sanitize(req.body.re_pass).trim();
re_pass = sanitize(re_pass).xss();

if (name === '' || pass === '' || re_pass === '' || email === '') {
res.render('sign/signup', {error: '信息不完整。', name: name, email: email});
return;
Expand All @@ -50,7 +50,7 @@ exports.signup = function (req, res, next) {
res.render('sign/signup', {error: '两次密码输入不一致。', name: name, email: email});
return;
}

try {
check(email, '不正确的电子邮箱。').isEmail();
} catch (e) {
Expand Down Expand Up @@ -97,7 +97,7 @@ exports.signup = function (req, res, next) {

/**
* Show user login page.
*
*
* @param {HttpRequest} req
* @param {HttpResponse} res
*/
Expand All @@ -119,15 +119,15 @@ var notJump = [

/**
* Handle user login.
*
*
* @param {HttpRequest} req
* @param {HttpResponse} res
* @param {Function} next
*/
exports.login = function (req, res, next) {
var loginname = sanitize(req.body.name).trim().toLowerCase();
var pass = sanitize(req.body.pass).trim();

if (!loginname || !pass) {
return res.render('sign/signin', { error: '信息不完整。' });
}
Expand All @@ -148,7 +148,7 @@ exports.login = function (req, res, next) {
}
// store session cookie
gen_session(user, res);
//check at some page just jump to home page
//check at some page just jump to home page
var refer = req.session._loginReferer || 'home';
for (var i = 0, len = notJump.length; i !== len; ++i) {
if (refer.indexOf(notJump[i]) >= 0) {
Expand Down Expand Up @@ -225,16 +225,16 @@ exports.search_pass = function (req, res, next) {
});
});
});
}
}
};

/**
* reset password
* 'get' to show the page, 'post' to reset password
* after reset password, retrieve_key&time will be destroy
* @param {http.req} req
* @param {http.res} res
* @param {Function} next
* @param {http.req} req
* @param {http.res} res
* @param {Function} next
*/
exports.reset_pass = function (req, res, next) {
var method = req.method.toLowerCase();
Expand All @@ -252,7 +252,7 @@ exports.reset_pass = function (req, res, next) {
return res.render('notify/notify', {error : '该链接已过期,请重新申请。'});
}
return res.render('sign/reset', {name : name, key : key});
});
});
} else {
var psw = req.body.psw || '';
var repsw = req.body.repsw || '';
Expand Down Expand Up @@ -335,16 +335,16 @@ exports.auth_user = function(req, res, next) {
return next();
});
} else {
return next();
return next();
}
});
});
}
};

// private
function gen_session(user, res) {
var auth_token = encrypt(user._id + '\t' + user.name + '\t' + user.pass + '\t' + user.email, config.session_secret);
res.cookie(config.auth_cookie_name, auth_token, {path: '/', maxAge: 1000 * 60 * 60 * 24 * 30}); //cookie 有效期30天
res.cookie(config.auth_cookie_name, auth_token, {path: '/', maxAge: 1000 * 60 * 60 * 24 * 30}); //cookie 有效期30天
}

function encrypt(str, secret) {
Expand All @@ -369,12 +369,12 @@ function md5(str) {
}
function randomString(size) {
size = size || 6;
var code_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var code_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var max_num = code_string.length + 1;
var new_pass = '';
while(size>0){
new_pass += code_string.charAt(Math.floor(Math.random()* max_num));
size--;
size--;
}
return new_pass;
}
8 changes: 4 additions & 4 deletions controllers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ exports.index = function (req, res, next) {
pages: pages,
keyword: keyword
});
};
};

var proxy = EventProxy.create('tags', 'topics', 'hot_topics', 'stars', 'tops', 'no_reply_topics', 'pages', render);
proxy.fail(next);
// 取标签
Expand All @@ -68,11 +68,11 @@ exports.index = function (req, res, next) {
// 取热门主题
topic_ctrl.get_topics_by_query({}, { limit: 5, sort: [ [ 'visit_count', 'desc' ] ] }, proxy.done('hot_topics'));
// 取星标用户
user_ctrl.get_users_by_query({ is_star: true }, { limit: 5 }, proxy.done('stars'));
user_ctrl.get_users_by_query({ is_star: true }, { limit: 5 }, proxy.done('stars'));
// 取排行榜上的用户
user_ctrl.get_users_by_query({}, { limit: 10, sort: [ [ 'score', 'desc' ] ] }, proxy.done('tops'));
// 取0回复的主题
topic_ctrl.get_topics_by_query({ reply_count: 0 }, { limit: 5, sort: [ [ 'create_at', 'desc' ] ] },
topic_ctrl.get_topics_by_query({ reply_count: 0 }, { limit: 5, sort: [ [ 'create_at', 'desc' ] ] },
proxy.done('no_reply_topics'));
// 取分页数据
topic_ctrl.get_count_by_query(query, proxy.done(function (all_topics_count) {
Expand Down
64 changes: 22 additions & 42 deletions controllers/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.list_topic = function (req, res, next) {
}
var done = function (topic_ids, collection, hot_topics, no_reply_topics, pages) {
var query = {'_id': {'$in': topic_ids}};
var opt = {skip: (page - 1) * limit, limit: limit, sort: [['create_at', 'desc']]};
var opt = {skip: (page - 1) * limit, limit: limit, sort: [['create_at', 'desc']]};

topic_ctrl.get_topics_by_query(query, opt, function (err, topics) {
for (var i = 0; i < topics.length; i++) {
Expand Down Expand Up @@ -53,53 +53,33 @@ exports.list_topic = function (req, res, next) {

var proxy = new EventProxy();
proxy.assign('topic_ids', 'collection', 'hot_topics', 'no_reply_topics', 'pages', done);
proxy.fail(next);

TopicTag.find({tag_id: tag._id}, function (err, docs) {
if (err) {
return next(err);
}
TopicTag.find({tag_id: tag._id}, proxy.done(function (docs) {
var topic_ids = [];

for (var i = 0; i < docs.length; i++) {
topic_ids.push(docs[i].topic_id);
}
proxy.trigger('topic_ids', topic_ids);
proxy.emit('topic_ids', topic_ids);

topic_ctrl.get_count_by_query({'_id': {'$in': topic_ids}}, function (err, all_topics_count) {
if (err) {
return next(err);
}
topic_ctrl.get_count_by_query({'_id': {'$in': topic_ids}}, proxy.done(function (all_topics_count) {
var pages = Math.ceil(all_topics_count / limit);
proxy.trigger('pages', pages);
});
});
proxy.emit('pages', pages);
}));
}));

if (!req.session.user) {
proxy.trigger('collection', null);
proxy.emit('collection', null);
} else {
TagCollect.findOne({user_id: req.session.user._id, tag_id: tag._id}, function (err, doc) {
if (err) {
return next(err);
}
proxy.trigger('collection', doc);
});
TagCollect.findOne({user_id: req.session.user._id, tag_id: tag._id}, proxy.done('collection'));
}

var opt = {limit: 5, sort: [['visit_count', 'desc']]};
topic_ctrl.get_topics_by_query({}, opt, function (err, hot_topics) {
if (err) {
return next(err);
}
proxy.trigger('hot_topics', hot_topics);
});
topic_ctrl.get_topics_by_query({}, opt, proxy.done('hot_topics'));

opt = {limit: 5, sort: [['create_at', 'desc']]};
topic_ctrl.get_topics_by_query({reply_count: 0}, opt, function (err, no_reply_topics) {
if (err) {
return next(err);
}
proxy.trigger('no_reply_topics', no_reply_topics);
});
topic_ctrl.get_topics_by_query({reply_count: 0}, opt, proxy.done('no_reply_topics'));
});
};

Expand Down Expand Up @@ -127,13 +107,13 @@ exports.add = function (req, res, next) {
}

var name = sanitize(req.body.name).trim();
name = sanitize(name).xss();
name = sanitize(name).xss();
var description = sanitize(req.body.description).trim();
description = sanitize(description).xss();
description = sanitize(description).xss();
var background = sanitize(req.body.background).trim();
background = sanitize(background).xss();
background = sanitize(background).xss();
var order = req.body.order;

if (name === '') {
res.render('notify/notify', {error: '信息不完整。'});
return;
Expand Down Expand Up @@ -195,12 +175,12 @@ exports.edit = function (req, res, next) {
}
if (method === 'post') {
var name = sanitize(req.body.name).trim();
name = sanitize(name).xss();
name = sanitize(name).xss();
var order = req.body.order;
var background = sanitize(req.body.background).trim();
background = sanitize(background).xss();
background = sanitize(background).xss();
var description = sanitize(req.body.description).trim();
description = sanitize(description).xss();
description = sanitize(description).xss();
if (name === '') {
res.render('notify/notify', {error: '信息不完整。'});
return;
Expand Down Expand Up @@ -266,7 +246,7 @@ exports.collect = function (req, res, next) {
if (!tag) {
res.json({status: 'failed'});
}

TagCollect.findOne({user_id: req.session.user._id, tag_id: tag._id}, function (err, doc) {
if (err) {
return next(err);
Expand Down Expand Up @@ -335,7 +315,7 @@ exports.de_collect = function (req, res, next) {
};

function get_all_tags(callback) {
Tag.find({}, [], {sort: [['order', 'asc']]}, callback);
Tag.find({}, [], {sort: [['order', 'asc']]}, callback);
}
function get_tag_by_name(name, callback) {
Tag.findOne({name: name}, callback);
Expand All @@ -344,7 +324,7 @@ function get_tag_by_id(id, callback) {
Tag.findOne({_id: id}, callback);
}
function get_tags_by_ids(ids, callback) {
Tag.find({_id: {'$in': ids}}, callback);
Tag.find({_id: {'$in': ids}}, callback);
}
function get_tags_by_query(query, opt, callback) {
Tag.find(query, [], opt, callback);
Expand Down
Loading

0 comments on commit c71d612

Please sign in to comment.