Repository navigation
Expand file tree
/
Copy pathtopic.js
More file actions
463 lines (424 loc) · 13.5 KB
/
Copy pathtopic.js
File metadata and controls
463 lines (424 loc) · 13.5 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*!
* nodeclub - controllers/topic.js
*/
/**
* Module dependencies.
*/
var sanitize = require('validator').sanitize;
var at = require('../services/at');
var User = require('../proxy').User;
var Topic = require('../proxy').Topic;
var Tag = require('../proxy').Tag;
var Relation = require('../proxy').Relation;
var TopicTag = require('../proxy').TopicTag;
var TopicCollect = require('../proxy').TopicCollect;
var EventProxy = require('eventproxy');
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.fail(next);
ep.once('topic', function (topic) {
if (topic.content_is_html) {
return ep.emit('@user');
}
at.linkUsers(topic.content, ep.done(function (content) {
topic.content = content;
ep.emit('@user');
}));
});
Topic.getFullTopic(topic_id, ep.done(function (message, topic, tags, author, replies) {
if (message) {
ep.unbind();
return res.render('notify/notify', { error: message });
}
topic.visit_count += 1;
topic.save(ep.done(function () {
// 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 {
TopicCollect.getTopicCollect(req.session.user._id, topic._id, ep.done(function (doc) {
topic.in_collection = doc;
ep.emit('topic', topic);
}));
}
}));
// get author's relationship
if (req.session.user && req.session.user._id) {
Relation.getRelation(req.session.user._id, topic.author_id, ep.done('get_relation'));
} else {
ep.emit('get_relation', null);
}
// get author other topics
var options = { limit: 5, sort: [ [ 'last_reply_at', 'desc' ] ]};
var query = { author_id: topic.author_id, _id: { '$nin': [ topic._id ] } };
Topic.getTopicsByQuery(query, options, ep.done('other_topics'));
// get no reply topics
var options2 = { limit: 5, sort: [ ['create_at', 'desc'] ] };
Topic.getTopicsByQuery({reply_count: 0}, options2, ep.done('no_reply_topics'));
}));
};
exports.create = function (req, res, next) {
Tag.getAllTags(function (err, tags) {
if (err) {
return next(err);
}
res.render('topic/edit', {tags: tags});
});
};
exports.put = function (req, res, next) {
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.getAllTags(function (err, tags) {
if (err) {
return next(err);
}
for (var i = 0; i < topic_tags.length; i++) {
for (var j = 0; j < tags.length; j++) {
if (topic_tags[i] === tags[j]._id) {
tags[j].is_selected = true;
}
}
}
res.render('topic/edit', {tags: tags, edit_error: '标题不能是空的。', content: content});
});
} else if (title.length < 10 || title.length > 100) {
Tag.getAllTags(function (err, tags) {
if (err) {
return next(err);
}
for (var i = 0; i < topic_tags.length; i++) {
for (var j = 0; j < tags.length; j++) {
if (topic_tags[i] === tags[j]._id) {
tags[j].is_selected = true;
}
}
}
res.render('topic/edit', {tags: tags, edit_error: '标题字数太多或太少', title: title, content: content});
});
} else {
Topic.newAndSave(title, content, req.session.user._id, function (err, topic) {
if (err) {
return next(err);
}
var proxy = new EventProxy();
var render = function () {
res.redirect('/topic/' + topic._id);
};
proxy.assign('tags_saved', 'score_saved', render);
proxy.fail(next);
// 话题可以没有标签
if (topic_tags.length === 0) {
proxy.emit('tags_saved');
}
var tags_saved_done = function () {
proxy.emit('tags_saved');
};
proxy.after('tag_saved', topic_tags.length, tags_saved_done);
//save topic tags
topic_tags.forEach(function (tag) {
TopicTag.newAndSave(topic._id, tag, proxy.done('tag_saved'));
Tag.getTagById(tag, proxy.done(function (tag) {
tag.topic_count += 1;
tag.save();
}));
});
User.getUserById(req.session.user._id, proxy.done(function (user) {
user.score += 5;
user.topic_count += 1;
user.save();
req.session.user.score += 5;
proxy.emit('score_saved');
}));
//发送at消息
at.sendMessageToMentionUsers(content, topic._id, req.session.user._id);
});
}
};
exports.showEdit = function (req, res, next) {
if (!req.session.user) {
res.redirect('home');
return;
}
var topic_id = req.params.tid;
if (topic_id.length !== 24) {
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
return;
}
Topic.getTopicById(topic_id, function (err, topic, tags) {
if (!topic) {
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
return;
}
if (String(topic.author_id) === req.session.user._id || req.session.user.is_admin) {
Tag.getAllTags(function (err, all_tags) {
if (err) {
return next(err);
}
for (var i = 0; i < tags.length; i++) {
for (var j = 0; j < all_tags.length; j++) {
if (tags[i].id === all_tags[j].id) {
all_tags[j].is_selected = true;
}
}
}
res.render('topic/edit', {action: 'edit', topic_id: topic._id, title: topic.title, content: topic.content, tags: all_tags});
});
} else {
res.render('notify/notify', {error: '对不起,你不能编辑此话题。'});
}
});
};
exports.update = function (req, res, next) {
if (!req.session.user) {
res.redirect('home');
return;
}
var topic_id = req.params.tid;
if (topic_id.length !== 24) {
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
return;
}
Topic.getTopicById(topic_id, function (err, topic, tags) {
if (!topic) {
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
return;
}
if (String(topic.author_id) === req.session.user._id || req.session.user.is_admin) {
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.getAllTags(function (err, all_tags) {
if (err) {
return next(err);
}
for (var i = 0; i < topic_tags.length; i++) {
for (var j = 0; j < all_tags.length; j++) {
if (topic_tags[i] === all_tags[j]._id) {
all_tags[j].is_selected = true;
}
}
}
res.render('topic/edit', {action: 'edit', edit_error: '标题不能是空的。', topic_id: topic._id, content: content, tags: all_tags});
});
} else {
//保存话题
//删除topic_tag,标签topic_count减1
//保存新topic_tag
topic.title = title;
topic.content = content;
topic.update_at = new Date();
topic.save(function (err) {
if (err) {
return next(err);
}
var proxy = new EventProxy();
var render = function () {
res.redirect('/topic/' + topic._id);
};
proxy.assign('tags_removed_done', 'tags_saved_done', render);
proxy.fail(next);
// 删除topic_tag
var tags_removed_done = function () {
proxy.emit('tags_removed_done');
};
TopicTag.getTopicTagByTopicId(topic._id, function (err, docs) {
if (docs.length === 0) {
proxy.emit('tags_removed_done');
} else {
proxy.after('tag_removed', docs.length, tags_removed_done);
// delete topic tags
docs.forEach(function (doc) {
doc.remove(proxy.done(function () {
Tag.getTagById(doc.tag_id, proxy.done(function (tag) {
proxy.emit('tag_removed');
tag.topic_count -= 1;
tag.save();
}));
}));
});
}
});
// 保存topic_tag
var tags_saved_done = function () {
proxy.emit('tags_saved_done');
};
//话题可以没有标签
if (topic_tags.length === 0) {
proxy.emit('tags_saved_done');
} else {
proxy.after('tag_saved', topic_tags.length, tags_saved_done);
//save topic tags
topic_tags.forEach(function (tag) {
TopicTag.newAndSave(topic._id, tag, proxy.done('tag_saved'));
Tag.getTagById(tag, proxy.done(function (tag) {
tag.topic_count += 1;
tag.save();
}));
});
}
//发送at消息
at.sendMessageToMentionUsers(content, topic._id, req.session.user._id);
});
}
} else {
res.render('notify/notify', {error: '对不起,你不能编辑此话题。'});
}
});
};
exports.delete = function (req, res, next) {
//删除话题, 话题作者topic_count减1
//删除回复,回复作者reply_count减1
//删除topic_tag,标签topic_count减1
//删除topic_collect,用户collect_topic_count减1
if (!req.session.user || !req.session.user.is_admin) {
return res.send({success: false, message: '无权限'});
}
var topic_id = req.params.tid;
if (topic_id.length !== 24) {
return res.send({ success: false, error: '此话题不存在或已被删除。' });
}
Topic.getTopic(topic_id, function (err, topic) {
if (err) {
return res.send({ success: false, message: err.message });
}
if (!topic) {
return res.send({ success: false, message: '此话题不存在或已被删除。' });
}
topic.remove(function (err) {
if (err) {
return res.send({ success: false, message: err.message });
}
res.send({ success: true, message: '话题已被删除。' });
});
});
};
exports.top = function (req, res, next) {
if (!req.session.user.is_admin) {
res.redirect('home');
return;
}
var topic_id = req.params.tid;
var is_top = req.params.is_top;
if (topic_id.length !== 24) {
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
return;
}
Topic.getTopic(topic_id, function (err, topic) {
if (err) {
return next(err);
}
if (!topic) {
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
return;
}
topic.top = is_top;
topic.save(function (err) {
if (err) {
return next(err);
}
var msg = topic.top ? '此话题已经被置顶。' : '此话题已经被取消置顶。';
res.render('notify/notify', {success: msg});
});
});
};
exports.collect = function (req, res, next) {
var topic_id = req.body.topic_id;
Topic.getTopic(topic_id, function (err, topic) {
if (err) {
return next(err);
}
if (!topic) {
res.json({status: 'failed'});
}
TopicCollect.getTopicCollect(req.session.user._id, topic._id, function (err, doc) {
if (err) {
return next(err);
}
if (doc) {
res.json({status: 'success'});
return;
}
TopicCollect.newAndSave(req.session.user._id, topic._id, function (err) {
if (err) {
return next(err);
}
res.json({status: 'success'});
});
User.getUserById(req.session.user._id, function (err, user) {
if (err) {
return next(err);
}
user.collect_topic_count += 1;
user.save();
});
req.session.user.collect_topic_count += 1;
topic.collect_count += 1;
topic.save();
});
});
};
exports.de_collect = function (req, res, next) {
var topic_id = req.body.topic_id;
Topic.getTopic(topic_id, function (err, topic) {
if (err) {
return next(err);
}
if (!topic) {
res.json({status: 'failed'});
}
TopicCollect.remove(req.session.user._id, topic._id, function (err) {
if (err) {
return next(err);
}
res.json({status: 'success'});
});
User.getUserById(req.session.user._id, function (err, user) {
if (err) {
return next(err);
}
user.collect_topic_count -= 1;
user.save();
});
topic.collect_count -= 1;
topic.save();
req.session.user.collect_topic_count -= 1;
});
};