/*! * nodeclub - controllers/topic.js */ /** * Module dependencies. */ 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'); var user_ctrl = require('./user'); var reply_ctrl = require('./reply'); var EventProxy = require('eventproxy').EventProxy; var Showdown = require('../public/libs/showdown'); var Util = require('../libs/util'); /** * Topic page * * @param {HttpRequest} req * @param {HttpResponse} res * @param {Function} next */ exports.index = function (req, res, next) { var topic_id = req.params.tid; if (topic_id.length !== 24) { return res.render('notify/notify', { error: '此话题不存在或已被删除。' }); } var events = [ 'topic', 'other_topics', 'no_reply_topics', 'get_relation', '@user']; var ep = EventProxy.create(events, function (topic, other_topics, no_reply_topics, relation) { res.render('topic/index', { topic: topic, author_other_topics: other_topics, no_reply_topics: no_reply_topics, relation : relation }); }); ep.on('error', function (err) { ep.unbind(); next(err); }); 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); } 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); } if (message) { ep.unbind(); return res.render('notify/notify', { error: message }); } topic.visit_count += 1; topic.save(function (err) { // format date topic.friendly_create_at = Util.format_date(topic.create_at, true); topic.friendly_update_at = Util.format_date(topic.update_at, true); topic.tags = tags; topic.author = author; topic.replies = replies; if (!req.session.user) { 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); } 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); }); } // 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 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); }); }); }; exports.create = function (req, res, next) { if (!req.session.user) { res.render('notify/notify', {error: '未登入用户不能发布话题。'}); return; } 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(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 != ''){ 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