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; i100){ - tag_ctrl.get_all_tags(function(err,tags){ - if(err) return next(err); - for(var i=0; i 100) { + tag_ctrl.get_all_tags(function (err, tags) { + if (err) { + return next(err); + } + for (var i = 0; i < topic_tags.length; i++) { + for (var j = 0; j < tags.length; j++) { + if (topic_tags[i] === tags[j]._id) { tags[j].is_selected = true; } - } + } } - res.render('topic/edit',{tags:tags, edit_error:'标题字数太多或太少', title:title, content:content}); - return; + res.render('topic/edit', {tags: tags, edit_error: '标题字数太多或太少', title: title, content: content}); }); - }else{ + } else { var topic = new Topic(); topic.title = title; topic.content = content; topic.author_id = req.session.user._id; - topic.save(function(err){ - if(err) return next(err); - - var proxy = new EventProxy(); - var render = function(){ - res.redirect('/topic/'+topic._id); + topic.save(function (err) { + if (err) { + return next(err); } - proxy.assign('tags_saved','score_saved',render) - //话题可以没有标签 - if(topic_tags.length == 0){ + var proxy = new EventProxy(); + var render = function () { + res.redirect('/topic/' + topic._id); + }; + + proxy.assign('tags_saved', 'score_saved', render); + proxy.fail(next); + // 话题可以没有标签 + if (topic_tags.length === 0) { proxy.emit('tags_saved'); } - var tags_saved_done = function(){ + var tags_saved_done = function () { proxy.emit('tags_saved'); }; - proxy.after('tag_saved',topic_tags.length,tags_saved_done); - //save topic tags - for(var i=0; i'); + res.send('forbidden!'); return; } var user_id = req.body.user_id; @@ -374,7 +353,7 @@ exports.get_collect_tags = function (req, res, next) { var ids = []; for (var i = 0; i < docs.length; i++) { ids.push(docs[i].tag_id); - } + } tag_ctrl.get_tags_by_ids(ids, function (err, tags) { if (err) { return next(err); @@ -388,7 +367,7 @@ exports.get_collect_topics = function (req, res, next) { if (!req.session.user) { res.redirect('home'); return; - } + } var page = Number(req.query.page) || 1; var limit = config.list_topic_count; @@ -396,49 +375,39 @@ exports.get_collect_topics = function (req, res, next) { var render = function (topics, pages) { res.render('user/collect_topics', { topics: topics, - current_page: page, + current_page: page, pages: pages }); }; var proxy = new EventProxy(); proxy.assign('topics', 'pages', render); + proxy.fail(next); - TopicCollect.find({ user_id: req.session.user._id }, function (err, docs) { - if (err) { - return next(err); - } + TopicCollect.find({ user_id: req.session.user._id }, proxy.done(function (docs) { var ids = []; for (var i = 0; i < docs.length; i++) { ids.push(docs[i].topic_id); } var query = { _id: { '$in': ids } }; var opt = { - skip: (page - 1) * limit, - limit: limit, - sort: [ [ 'create_at', 'desc' ] ] + skip: (page - 1) * limit, + limit: limit, + sort: [ [ 'create_at', 'desc' ] ] }; - topic_ctrl.get_topics_by_query(query, opt, function (err, topics) { - if (err) { - return next(err); - } - proxy.trigger('topics', topics); - }); - topic_ctrl.get_count_by_query(query, function (err, all_topics_count) { - if (err) { - return next(err); - } + topic_ctrl.get_topics_by_query(query, opt, proxy.done('topics')); + topic_ctrl.get_count_by_query(query, proxy.done(function (all_topics_count) { var pages = Math.ceil(all_topics_count / limit); - proxy.trigger('pages', pages); - }); - }); + proxy.emit('pages', pages); + })); + })); }; exports.get_followings = function (req, res, next) { if (!req.session.user) { res.redirect('home'); return; - } + } Relation.find({user_id: req.session.user._id}, function (err, docs) { if (err) { return next(err); @@ -453,29 +422,25 @@ exports.get_followings = function (req, res, next) { } res.render('user/followings', {users: users}); }); - }); + }); }; exports.get_followers = function (req, res, next) { if (!req.session.user) { res.redirect('home'); return; - } - Relation.find({follow_id: req.session.user._id}, function (err, docs) { - if (err) { - return next(err); - } + } + var proxy = new EventProxy(); + proxy.fail(next); + Relation.find({follow_id: req.session.user._id}, proxy.done(function (docs) { var ids = []; for (var i = 0; i < docs.length; i++) { ids.push(docs[i].user_id); } - get_users_by_ids(ids, function (err, users) { - if (err) { - return next(err); - } + get_users_by_ids(ids, proxy.done(function (users) { res.render('user/followers', {users: users}); - }); - }); + })); + })); }; exports.top100 = function (req, res, next) { @@ -498,7 +463,7 @@ exports.list_topics = function (req, res, next) { res.render('notify/notify', {error: '这个用户不存在。'}); return; } - + var render = function (topics, relation, pages) { user.friendly_create_at = Util.format_date(user.create_at, true); res.render('user/topics', { @@ -512,34 +477,22 @@ exports.list_topics = function (req, res, next) { var proxy = new EventProxy(); proxy.assign('topics', 'relation', 'pages', render); + proxy.fail(next); var query = {'author_id': user._id}; var opt = {skip: (page - 1) * limit, limit: limit, sort: [['create_at', 'desc']]}; - topic_ctrl.get_topics_by_query(query, opt, function (err, topics) { - if (err) { - return next(err); - } - proxy.trigger('topics', topics); - }); + topic_ctrl.get_topics_by_query(query, opt, proxy.done('topics')); if (!req.session.user) { - proxy.trigger('relation', null); + proxy.emit('relation', null); } else { - Relation.findOne({user_id: req.session.user._id, follow_id: user._id}, function (err, doc) { - if (err) { - return next(err); - } - proxy.trigger('relation', doc); - }); + Relation.findOne({user_id: req.session.user._id, follow_id: user._id}, proxy.done('relation')); } - topic_ctrl.get_count_by_query(query, function (err, all_topics_count) { - if (err) { - return next(err); - } + topic_ctrl.get_count_by_query(query, proxy.done(function (all_topics_count) { var pages = Math.ceil(all_topics_count / limit); - proxy.trigger('pages', pages); - }); + proxy.emit('pages', pages); + })); }); }; @@ -553,7 +506,7 @@ exports.list_replies = function (req, res, next) { res.render('notify/notify', {error: '这个用户不存在。'}); return; } - + var render = function (topics, relation, pages) { user.friendly_create_at = Util.format_date(user.create_at, true); res.render('user/replies', { @@ -567,11 +520,9 @@ exports.list_replies = function (req, res, next) { var proxy = new EventProxy(); proxy.assign('topics', 'relation', 'pages', render); + proxy.fail(next); - Reply.find({author_id: user._id}, function (err, replies) { - if (err) { - return next(err); - } + Reply.find({author_id: user._id}, proxy.done(function (replies) { var topic_ids = []; for (var i = 0; i < replies.length; i++) { if (topic_ids.indexOf(replies[i].topic_id.toString()) < 0) { @@ -580,31 +531,18 @@ exports.list_replies = function (req, res, next) { } var query = {'_id': {'$in': topic_ids}}; var opt = {skip: (page - 1) * limit, limit: limit, sort: [['create_at', 'desc']]}; - topic_ctrl.get_topics_by_query(query, opt, function (err, topics) { - if (err) { - return next(err); - } - proxy.trigger('topics', topics); - }); + topic_ctrl.get_topics_by_query(query, opt, proxy.done('topics')); - topic_ctrl.get_count_by_query(query, function (err, all_topics_count) { - if (err) { - return next(err); - } + topic_ctrl.get_count_by_query(query, 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('relation', null); + proxy.emit('relation', null); } else { - Relation.findOne({user_id: req.session.user._id, follow_id: user._id}, function (err, doc) { - if (err) { - return next(err); - } - proxy.trigger('relation', doc); - }); + Relation.findOne({user_id: req.session.user._id, follow_id: user._id}, proxy.done('relation')); } }); }; diff --git a/libs/util.js b/libs/util.js index b4aa05b97a..45cee367db 100644 --- a/libs/util.js +++ b/libs/util.js @@ -7,7 +7,7 @@ exports.format_date = function (date, friendly) { var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); - + if (friendly) { var now = new Date(); var mseconds = -(date.getTime() - now.getTime()); @@ -44,17 +44,17 @@ exports.format_date = function (date, friendly) { * @api private */ -exports.escape = function(html){ +exports.escape = function (html) { var codeSpan = /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm; var codeBlock = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g; var spans = []; var blocks = []; var text = String(html).replace(/\r\n/g, '\n') .replace('/\r/g', '\n'); - + text = '\n\n' + text + '\n\n'; - text = text.replace(codeSpan, function(code) { + text = text.replace(codeSpan, function (code) { spans.push(code); return '`span`'; }); @@ -69,13 +69,13 @@ exports.escape = function(html){ .replace(//g, '>') .replace(/"/g, '"') - .replace(/`span`/g, function() { + .replace(/`span`/g, function () { return spans.shift(); }) - .replace(/\n\tblock/g, function() { + .replace(/\n\tblock/g, function () { return blocks.shift(); }) - .replace(/~0$/,'') + .replace(/~0$/, '') .replace(/^\n\n/, '') .replace(/\n\n$/, ''); }; diff --git a/models/index.js b/models/index.js index b692553424..f17e2888e4 100644 --- a/models/index.js +++ b/models/index.js @@ -1,6 +1,6 @@ var mongoose = require('mongoose'); var config = require('../config').config; - + mongoose.connect(config.db, function (err) { if (err) { console.error('connect to %s error: ', config.db, err.message); diff --git a/models/relation.js b/models/relation.js index f7d73331fa..e289017286 100644 --- a/models/relation.js +++ b/models/relation.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; - + var RelationSchema = new Schema({ user_id: { type: ObjectId }, follow_id: { type: ObjectId }, diff --git a/models/reply.js b/models/reply.js index fd135cc3ae..ac7cf5d73f 100644 --- a/models/reply.js +++ b/models/reply.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; - + var ReplySchema = new Schema({ content: { type: String }, topic_id: { type: ObjectId, index: true }, diff --git a/models/tag.js b/models/tag.js index cec9d5ad27..41c0d3da1b 100644 --- a/models/tag.js +++ b/models/tag.js @@ -1,6 +1,6 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; - + var TagSchema = new Schema({ name: { type: String }, order: { type: Number, default: 1 }, diff --git a/models/tag_collect.js b/models/tag_collect.js index 1e9ad6e5a6..7e7fa31965 100644 --- a/models/tag_collect.js +++ b/models/tag_collect.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; - + var TagCollectSchema = new Schema({ user_id: { type: ObjectId, index: true }, tag_id: { type: ObjectId }, diff --git a/models/topic.js b/models/topic.js index 68f6ea95d3..f5dd1ffd75 100644 --- a/models/topic.js +++ b/models/topic.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; - + var TopicSchema = new Schema({ title: { type: String }, content: { type: String }, diff --git a/models/topic_collect.js b/models/topic_collect.js index 11e4f8add7..78cd591284 100644 --- a/models/topic_collect.js +++ b/models/topic_collect.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; - + var TopicCollectSchema = new Schema({ user_id: { type: ObjectId }, topic_id: { type: ObjectId }, diff --git a/models/topic_tag.js b/models/topic_tag.js index bd92fe67b6..d5d947f3fe 100644 --- a/models/topic_tag.js +++ b/models/topic_tag.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; - + var TopicTagSchema = new Schema({ topic_id: { type: ObjectId }, tag_id: { type: ObjectId }, diff --git a/models/user.js b/models/user.js index 202751561d..a0b1331036 100644 --- a/models/user.js +++ b/models/user.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var config = require('../config'); - + var UserSchema = new Schema({ name: { type: String, index: true }, loginname: { type: String, unique: true }, @@ -14,7 +14,7 @@ var UserSchema = new Schema({ profile: { type: String }, weibo: { type: String }, avatar: { type: String }, - + score: { type: Number, default: 0 }, topic_count: { type: Number, default: 0 }, reply_count: { type: Number, default: 0 }, @@ -27,7 +27,7 @@ var UserSchema = new Schema({ is_star: { type: Boolean }, level: { type: String }, active: { type: Boolean, default: true }, - + receive_reply_mail: {type: Boolean, default: false }, receive_at_mail: { type: Boolean, default: false }, from_wp: { type: Boolean }, diff --git a/views/index.html b/views/index.html index 5df4b35eb9..f1618e1b23 100644 --- a/views/index.html +++ b/views/index.html @@ -16,17 +16,17 @@

- + <% if (locals.current_user) { %> <% } %>
<% if (locals.topics && topics.length > 0) { %> - <%- partial('topic/list', { + <%- partial('topic/list', { topics: topics, pages: pages, current_pages: current_page, - base: keyword ? ('/?q=' + keyword) : '/' + base: keyword ? ('/?q=' + keyword) : '/' }) %> <% } else { %>
@@ -34,4 +34,4 @@
<% } %> - \ No newline at end of file + diff --git a/views/layout.html b/views/layout.html index 26b39d6729..8207304ba9 100644 --- a/views/layout.html +++ b/views/layout.html @@ -1,12 +1,12 @@ - - + + <% var headers = config.site_headers || []; - for (var i = 0, l = headers.length; i < l; i++) { + for (var i = 0, l = headers.length; i < l; i++) { %> <%- headers[i] %> <% } %> @@ -47,27 +47,27 @@ '/> - +
<%- body %> @@ -82,13 +82,13 @@
- 关于 -   |   - FAQ + 关于   |   - Github + FAQ   |   - RSS + Github +   |   + RSS
@@ -98,4 +98,4 @@
- + diff --git a/views/message/index.html b/views/message/index.html index 49e9b849db..e726aa4998 100644 --- a/views/message/index.html +++ b/views/message/index.html @@ -64,7 +64,7 @@ $me = $(this); $me.toggleClass('message'); $me.find('.marked_icon').show(); - $me.find('.unread').removeClass('unread'); + $me.find('.unread').removeClass('unread'); $('.messages_count').html(0); }); } diff --git a/views/sidebar.html b/views/sidebar.html index e039e63c79..3dca859e2d 100644 --- a/views/sidebar.html +++ b/views/sidebar.html @@ -19,7 +19,7 @@
-
+
社区消息: @@ -30,11 +30,11 @@

如何加入CNode社区?

- + <% if (locals.hot_tags) { %>
-
+
热门标签
@@ -86,7 +86,7 @@ <% if (config.site_links && config.site_links.length > 0) { %>
-
+
相关站点
@@ -142,7 +142,7 @@ <% if (config.side_ads && config.side_ads.length > 0) { %>
-
+
友情社区 | 赞助商
<% config.side_ads.forEach(function (ad) { %> @@ -154,7 +154,7 @@ <% }); %>
<% } %> - +