forked from cnodejs/nodeclub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreply.js
115 lines (102 loc) · 3.12 KB
/
reply.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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 config = require('../../config');
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;
}
if (topic.lock) {
res.status(403);
return res.send({error_msg: 'topic is locked'});
}
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;
var ups = function (req, res, next) {
var replyId = req.params.reply_id;
var userId = req.user.id;
Reply.getReplyById(replyId, function (err, reply) {
if (err) {
return next(err);
}
if (!reply) {
res.status(404);
return res.send({error_msg: 'reply `' + replyId + '` not found'});
}
if (reply.author_id.equals(userId) && !config.debug) {
// 不能帮自己点赞
res.send({
error_msg: '呵呵,不能帮自己点赞。',
});
} else {
var action;
reply.ups = reply.ups || [];
var upIndex = reply.ups.indexOf(userId);
if (upIndex === -1) {
reply.ups.push(userId);
action = 'up';
} else {
reply.ups.splice(upIndex, 1);
action = 'down';
}
reply.save(function () {
res.send({
success: true,
action: action
});
});
}
});
};
exports.ups = ups;