diff --git a/controllers/mail.js b/controllers/mail.js index 4b790c36ca..fafbcc53e1 100644 --- a/controllers/mail.js +++ b/controllers/mail.js @@ -69,7 +69,7 @@ mailEvent.on("getMail", function () { * @return {[type]} */ function trigger() { - mailEvent.trigger("getMail"); + mailEvent.emit("getMail"); } /** @@ -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 = '
您好:
' + '我们收到您在' + config.name + '社区的注册信息,请点击下面的链接来激活帐户:
' + @@ -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 = '您好:
' + '我们收到您在' + config.name + '社区重置密码的请求,请在24小时内单击下面的链接来重置密码:
' + @@ -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 = '您好:
' + '' + @@ -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 = '
您好:
' + '' +
diff --git a/controllers/message.js b/controllers/message.js
index acad41986b..286f9ff1ac 100644
--- a/controllers/message.js
+++ b/controllers/message.js
@@ -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');
@@ -52,7 +52,7 @@ exports.index = function (req, res, next) {
return next(err);
}
messages[i] = message;
- proxy.trigger('message_ready');
+ proxy.emit('message_ready');
});
});
});
@@ -84,7 +84,7 @@ exports.mark_read = function (req, res, next) {
}
res.json({status: 'success'});
});
- });
+ });
};
exports.mark_all_read = function (req, res, next) {
@@ -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) {
@@ -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);
diff --git a/controllers/sign.js b/controllers/sign.js
index 612b0a22ae..39e7090314 100644
--- a/controllers/sign.js
+++ b/controllers/sign.js
@@ -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;
@@ -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) {
@@ -97,7 +97,7 @@ exports.signup = function (req, res, next) {
/**
* Show user login page.
- *
+ *
* @param {HttpRequest} req
* @param {HttpResponse} res
*/
@@ -119,7 +119,7 @@ var notJump = [
/**
* Handle user login.
- *
+ *
* @param {HttpRequest} req
* @param {HttpResponse} res
* @param {Function} next
@@ -127,7 +127,7 @@ var notJump = [
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: '信息不完整。' });
}
@@ -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) {
@@ -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();
@@ -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 || '';
@@ -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) {
@@ -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;
}
diff --git a/controllers/site.js b/controllers/site.js
index 5c2b0efe91..d1f838a5cb 100644
--- a/controllers/site.js
+++ b/controllers/site.js
@@ -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);
// 取标签
@@ -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) {
diff --git a/controllers/tag.js b/controllers/tag.js
index 8c1ca8d032..1ffa055974 100644
--- a/controllers/tag.js
+++ b/controllers/tag.js
@@ -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++) {
@@ -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'));
});
};
@@ -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;
@@ -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;
@@ -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);
@@ -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);
@@ -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);
diff --git a/controllers/topic.js b/controllers/topic.js
index b3e49528a7..c992ad9a77 100644
--- a/controllers/topic.js
+++ b/controllers/topic.js
@@ -7,12 +7,10 @@
*/
var models = require('../models');
-var Tag = models.Tag;
var Topic = models.Topic;
var TopicTag = models.TopicTag;
var TopicCollect = models.TopicCollect;
var Relation = models.Relation;
-var check = require('validator').check;
var sanitize = require('validator').sanitize;
var at_ctrl = require('./at');
var tag_ctrl = require('./tag');
@@ -24,7 +22,7 @@ var Util = require('../libs/util');
/**
* Topic page
- *
+ *
* @param {HttpRequest} req
* @param {HttpResponse} res
* @param {Function} next
@@ -46,35 +44,26 @@ exports.index = function (req, res, next) {
});
});
- ep.on('error', function (err) {
- ep.unbind();
- next(err);
- });
+ ep.fail(next);
ep.once('topic', function (topic) {
if (topic.content_is_html) {
return ep.emit('@user');
}
- at_ctrl.link_at_who(topic.content, function (err, content) {
- if (err) {
- return ep.emit(err);
- }
+ at_ctrl.link_at_who(topic.content, ep.done(function (content) {
topic.content = Util.xss(Showdown.parse(content));
ep.emit('@user');
- });
+ }));
});
- get_full_topic(topic_id, function (err, message, topic, tags, author, replies) {
- if (err) {
- return ep.emit('error', err);
- }
+ get_full_topic(topic_id, ep.done(function (message, topic, tags, author, replies) {
if (message) {
ep.unbind();
return res.render('notify/notify', { error: message });
}
topic.visit_count += 1;
- topic.save(function (err) {
+ topic.save(ep.done(function () {
// format date
topic.friendly_create_at = Util.format_date(topic.create_at, true);
topic.friendly_update_at = Util.format_date(topic.update_at, true);
@@ -87,47 +76,29 @@ exports.index = function (req, res, next) {
ep.emit('topic', topic);
} else {
var q = { user_id: req.session.user._id, topic_id: topic._id };
- TopicCollect.findOne(q, function (err, doc) {
- if (err) {
- return ep.emit('error', err);
- }
+ TopicCollect.findOne(q, ep.done(function (doc) {
topic.in_collection = doc;
ep.emit('topic', topic);
- });
- }
- });
+ }));
+ }
+ }));
//get author's relationship
if (!req.session.user || req.session.user._id) {
ep.emit('get_relation', null);
} else {
- Relation.findOne({user_id: req.session.user._id, follow_id: topic.author_id}, function (err, relation) {
- if (err) {
- return ep.emit('error', err);
- }
- ep.emit('get_relation', relation);
- });
+ Relation.findOne({user_id: req.session.user._id, follow_id: topic.author_id}, ep.done('get_relation'));
}
// get author other topics
var options = { limit: 5, sort: [ [ 'last_reply_at', 'desc' ] ]};
var query = { author_id: topic.author_id, _id: { '$nin': [ topic._id ] } };
- get_topics_by_query(query, options, function (err, topics) {
- if (err) {
- return ep.emit('error', err);
- }
- ep.emit('other_topics', topics);
- });
+ get_topics_by_query(query, options, ep.done('other_topics'));
// get no reply topics
var options2 = { limit: 5, sort: [ ['create_at', 'desc'] ] };
- get_topics_by_query({ reply_count: 0 }, options2, function (err, topics) {
- if (err) {
- return ep.emit('error', err);
- }
- ep.emit('no_reply_topics', topics);
- });
- });
+ get_topics_by_query({ reply_count: 0 }, options2, ep.done('no_reply_topics'));
+ }));
};
exports.create = function (req, res, next) {
@@ -135,270 +106,267 @@ exports.create = function (req, res, next) {
res.render('notify/notify', {error: '未登入用户不能发布话题。'});
return;
}
-
+ // TODO: app.get/app.post
var method = req.method.toLowerCase();
if (method === 'get') {
tag_ctrl.get_all_tags(function (err, tags) {
- if(err) return next(err);
- res.render('topic/edit',{tags:tags});
- return;
+ if (err) {
+ return next(err);
+ }
+ res.render('topic/edit', {tags: tags});
});
}
- if(method == 'post'){
+ if (method === 'post') {
var title = sanitize(req.body.title).trim();
title = sanitize(title).xss();
var content = req.body.t_content;
- var topic_tags=[];
- if(req.body.topic_tags != ''){
+ var topic_tags = [];
+ if (req.body.topic_tags !== '') {
topic_tags = req.body.topic_tags.split(',');
- }
-
- if(title == ''){
- tag_ctrl.get_all_tags(function(err,tags){
- if(err) return next(err);
- for(var i=0; i