-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathreply.js
190 lines (171 loc) · 5.15 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var validator = require('validator');
var _ = require('lodash');
var at = require('../common/at');
var message = require('../common/message');
var EventProxy = require('eventproxy');
var User = require('../proxy').User;
var Topic = require('../proxy').Topic;
var Reply = require('../proxy').Reply;
var config = require('../config');
/**
* 添加回复
*/
exports.add = function (req, res, next) {
var content = req.body.r_content;
var topic_id = req.params.topic_id;
var reply_id = req.body.reply_id;
var str = validator.trim(String(content));
if (str === '') {
return res.renderError('回复内容不能为空!', 422);
}
var ep = EventProxy.create();
ep.fail(next);
Topic.getTopic(topic_id, ep.doneLater(function (topic) {
if (!topic) {
ep.unbind();
// just 404 page
return next();
}
if (topic.lock) {
return res.status(403).send('此主题已锁定。');
}
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.session.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.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 = user;
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.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.status(422);
res.json({status: 'no reply ' + reply_id + ' exists'});
return;
}
if (reply.author_id.toString() === req.session.user._id.toString() || req.session.user.is_admin) {
reply.deleted = true;
reply.save();
res.json({status: 'success'});
reply.author.score -= 5;
reply.author.reply_count -= 1;
reply.author.save();
} else {
res.json({status: 'failed'});
return;
}
Topic.reduceCount(reply.topic_id, _.noop);
});
};
/*
打开回复编辑器
*/
exports.showEdit = function (req, res, next) {
var reply_id = req.params.reply_id;
Reply.getReplyById(reply_id, function (err, reply) {
if (!reply) {
return res.render404('此回复不存在或已被删除。');
}
if (req.session.user._id.equals(reply.author_id) || req.session.user.is_admin) {
res.render('reply/edit', {
reply_id: reply._id,
content: reply.content
});
} else {
return res.renderError('对不起,你不能编辑此回复。', 403);
}
});
};
/*
提交编辑回复
*/
exports.update = function (req, res, next) {
var reply_id = req.params.reply_id;
var content = req.body.t_content;
Reply.getReplyById(reply_id, function (err, reply) {
if (!reply) {
return res.render404('此回复不存在或已被删除。');
}
if (String(reply.author_id) === req.session.user._id.toString() || req.session.user.is_admin) {
if (content.trim().length > 0) {
reply.content = content;
reply.update_at = new Date();
reply.save(function (err) {
if (err) {
return next(err);
}
res.redirect('/topic/' + reply.topic_id + '#' + reply._id);
});
} else {
return res.renderError('回复的字数太少。', 400);
}
} else {
return res.renderError('对不起,你不能编辑此回复。', 403);
}
});
};
exports.up = function (req, res, next) {
var replyId = req.params.reply_id;
var userId = req.session.user._id;
Reply.getReplyById(replyId, function (err, reply) {
if (err) {
return next(err);
}
if (reply.author_id.equals(userId) && !config.debug) {
// 不能帮自己点赞
res.send({
success: false,
message: '呵呵,不能帮自己点赞。',
});
} 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
});
});
}
});
};