mirror of
https://github.com/cnodejs/nodeclub.git
synced 2025-12-08 19:55:55 +00:00
73 lines
2.0 KiB
JavaScript
73 lines
2.0 KiB
JavaScript
|
||
var eventproxy = require('eventproxy');
|
||
var validator = require('validator');
|
||
var Topic = require('../../proxy').Topic;
|
||
var User = require('../../proxy').User;
|
||
var Reply = require('../../proxy').Reply;
|
||
var at = require('../../common/at');
|
||
var message = require('../../common/message');
|
||
|
||
var create = function (req, res, next) {
|
||
var topic_id = req.params.topic_id;
|
||
var content = req.body.content;
|
||
var reply_id = req.body.reply_id;
|
||
|
||
var ep = new eventproxy();
|
||
ep.fail(next);
|
||
|
||
var str = validator.trim(content);
|
||
if (str === '') {
|
||
res.status(422);
|
||
res.send({error_msg: '回复内容不能为空!'});
|
||
return;
|
||
}
|
||
|
||
Topic.getTopic(topic_id, ep.done(function (topic) {
|
||
if (!topic) {
|
||
res.status(404);
|
||
res.send({error_msg: 'topic `' + topic_id + '` not found'});
|
||
return;
|
||
}
|
||
ep.emit('topic', topic);
|
||
}));
|
||
|
||
ep.all('topic', function (topic) {
|
||
User.getUserById(topic.author_id, ep.done('topic_author'));
|
||
});
|
||
|
||
ep.all('topic', 'topic_author', function (topic, topicAuthor) {
|
||
Reply.newAndSave(content, topic_id, req.user.id, reply_id, ep.done(function (reply) {
|
||
Topic.updateLastReply(topic_id, reply._id, ep.done(function () {
|
||
ep.emit('reply_saved', reply);
|
||
//发送at消息,并防止重复 at 作者
|
||
var newContent = content.replace('@' + topicAuthor.loginname + ' ', '');
|
||
at.sendMessageToMentionUsers(newContent, topic_id, req.user.id, reply._id);
|
||
}));
|
||
}));
|
||
|
||
User.getUserById(req.user.id, ep.done(function (user) {
|
||
user.score += 5;
|
||
user.reply_count += 1;
|
||
user.save();
|
||
ep.emit('score_saved');
|
||
}));
|
||
});
|
||
|
||
ep.all('reply_saved', 'topic', function (reply, topic) {
|
||
if (topic.author_id.toString() !== req.user.id.toString()) {
|
||
message.sendReplyMessage(topic.author_id, req.user.id, topic._id, reply._id);
|
||
}
|
||
ep.emit('message_saved');
|
||
});
|
||
|
||
ep.all('reply_saved', 'message_saved', 'score_saved', function (reply) {
|
||
res.send({
|
||
success: true,
|
||
reply_id: reply._id,
|
||
});
|
||
});
|
||
|
||
};
|
||
|
||
exports.create = create;
|