forked from cnodejs/nodeclub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.js
147 lines (127 loc) · 3.86 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var sanitize = require('validator').sanitize;
var at = require('../services/at');
var message = require('../services/message');
var EventProxy = require('eventproxy');
var User = require('../proxy').User;
var Topic = require('../proxy').Topic;
var Reply = require('../proxy').Reply;
/**
* 添加一级回复
*/
exports.add = function (req, res, next) {
var content = req.body.r_content;
var topic_id = req.params.topic_id;
var str = sanitize(content).trim();
if (str === '') {
res.render('notify/notify', {error: '回复内容不能为空!'});
return;
}
var ep = EventProxy.create();
ep.fail(next);
Topic.getTopic(topic_id, ep.doneLater(function (topic) {
if (!topic) {
ep.unbind();
// just 404 page
return next();
}
ep.emit('topic', topic);
}));
ep.on('topic', function (topic) {
Reply.newAndSave(content, topic_id, req.session.user._id, ep.done(function (reply) {
Topic.updateLastReply(topic_id, reply._id, ep.done(function () {
ep.emit('reply_saved', reply);
//发送at消息
at.sendMessageToMentionUsers(content, topic_id, req.session.user._id, reply._id);
}));
}));
User.getUserById(req.session.user._id, ep.done(function (user) {
user.score += 5;
user.reply_count += 1;
user.save();
req.session.user.score += 5;
ep.emit('score_saved');
}));
});
ep.all('reply_saved', 'topic', function (reply, topic) {
if (topic.author_id.toString() !== req.session.user._id.toString()) {
message.sendReplyMessage(topic.author_id, req.session.user._id, topic._id, reply._id);
}
ep.emit('message_saved');
});
ep.all('reply_saved', 'message_saved', 'score_saved', function (reply) {
res.redirect('/topic/' + topic_id + '#' + reply._id);
});
};
/**
* 添加二级回复
*/
exports.add_reply2 = function (req, res, next) {
var topic_id = req.params.topic_id;
var reply_id = req.body.reply_id;
var content = req.body.r2_content;
var str = sanitize(content).trim();
if (str === '') {
res.send('');
return;
}
var proxy = new EventProxy();
proxy.assign('reply_saved', 'message_saved', function (reply) {
Reply.getReplyById(reply._id, function (err, reply) {
res.redirect('/topic/' + topic_id + '#' + reply._id);
// res.partial('reply/reply2', {object: reply, as: 'reply'});
});
});
// 创建一条回复,并保存
Reply.newAndSave(content, topic_id, req.session.user._id, reply_id, function (err, reply) {
if (err) {
return next(err);
}
// 更新主题的最后回复信息
Topic.updateLastReply(topic_id, reply._id, function (err) {
if (err) {
return next(err);
}
proxy.emit('reply_saved', reply);
//发送at消息
at.sendMessageToMentionUsers(content, topic_id, req.session.user._id, reply._id);
});
});
// 将回复信息发送通知到相关人
Reply.getReply(reply_id, function (err, reply) {
if (err) {
return next(err);
}
if (reply && reply.author_id.toString() !== req.session.user._id.toString()) {
message.sendReply2Message(reply.author_id, req.session.user._id, topic_id, reply._id);
}
proxy.emit('message_saved');
});
};
/**
* 删除回复信息
*/
exports.delete = function (req, res, next) {
var reply_id = req.body.reply_id;
Reply.getReplyById(reply_id, function (err, reply) {
if (err) {
return next(err);
}
if (!reply) {
res.json({status: 'failed'});
return;
}
if (reply.author_id.toString() === req.session.user._id.toString()) {
reply.remove();
res.json({status: 'success'});
if (!reply.reply_id) {
reply.author.score -= 5;
reply.author.reply_count -= 1;
reply.author.save();
}
} else {
res.json({status: 'failed'});
return;
}
Topic.reduceCount(reply.topic_id, function () {});
});
};