Repository navigation
Expand file tree
/
Copy pathtopic.js
More file actions
220 lines (198 loc) · 5.89 KB
/
Copy pathtopic.js
File metadata and controls
220 lines (198 loc) · 5.89 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
var EventProxy = require('eventproxy');
var models = require('../models');
var Topic = models.Topic;
var TopicTag = models.TopicTag;
var User = require('./user');
var Tag = require('./tag');
var Reply = require('./reply');
var Util = require('../libs/util');
/**
* 根据主题ID获取主题
* Callback:
* - err, 数据库错误
* - topic, 主题
* - tags, 标签列表
* - author, 作者
* - lastReply, 最后回复
* @param {String} id 主题ID
* @param {Function} callback 回调函数
*/
exports.getTopicById = function (id, callback) {
var proxy = new EventProxy();
var events = ['topic', 'tags', 'author', 'last_reply'];
proxy.assign(events, function (topic, tags, author, last_reply) {
return callback(null, topic, tags, author, last_reply);
}).fail(callback);
Topic.findOne({_id: id}, proxy.done(function (topic) {
if (!topic) {
proxy.emit('topic', null);
proxy.emit('tags', []);
proxy.emit('author', null);
proxy.emit('last_reply', null);
return;
}
proxy.emit('topic', topic);
// TODO: 可以只查tag_id这个字段的吧?
TopicTag.find({topic_id: topic._id}, proxy.done(function (topic_tags) {
var tags_id = [];
for (var i = 0; i < topic_tags.length; i++) {
tags_id.push(topic_tags[i].tag_id);
}
Tag.getTagsByIds(tags_id, proxy.done('tags'));
}));
User.getUserById(topic.author_id, proxy.done('author'));
if (topic.last_reply) {
Reply.getReplyById(topic.last_reply, proxy.done(function (last_reply) {
proxy.emit('last_reply', last_reply || null);
}));
} else {
proxy.emit('last_reply', null);
}
}));
};
/**
* 获取关键词能搜索到的主题数量
* Callback:
* - err, 数据库错误
* - count, 主题数量
* @param {String} query 搜索关键词
* @param {Function} callback 回调函数
*/
exports.getCountByQuery = function (query, callback) {
Topic.count(query, callback);
};
/**
* 根据关键词,获取主题列表
* Callback:
* - err, 数据库错误
* - count, 主题列表
* @param {String} query 搜索关键词
* @param {Object} opt 搜索选项
* @param {Function} callback 回调函数
*/
exports.getTopicsByQuery = function (query, opt, callback) {
Topic.find(query, ['_id'], opt, function (err, docs) {
if (err) {
return callback(err);
}
if (docs.length === 0) {
return callback(null, []);
}
var topics_id = [];
for (var i = 0; i < docs.length; i++) {
topics_id.push(docs[i]._id);
}
var proxy = new EventProxy();
proxy.after('topic_ready', topics_id.length, function (topics) {
// 过滤掉空值
var filtered = topics.filter(function (item) {
return !!item;
});
return callback(null, filtered);
});
proxy.fail(callback);
topics_id.forEach(function (id, i) {
exports.getTopicById(id, proxy.group('topic_ready', function (topic, tags, author, last_reply) {
// 当id查询出来之后,进一步查询列表时,文章可能已经被删除了
// 所以这里有可能是null
if (topic) {
topic.tags = tags;
topic.author = author;
topic.reply = last_reply;
topic.friendly_create_at = Util.format_date(topic.create_at, true);
}
return topic;
}));
});
});
};
/**
* 获取所有信息的主题
* Callback:
* - err, 数据库异常
* - message, 消息
* - topic, 主题
* - tags, 主题的标签
* - author, 主题作者
* - replies, 主题的回复
* @param {String} id 主题ID
* @param {Function} callback 回调函数
*/
exports.getFullTopic = function (id, callback) {
var proxy = new EventProxy();
var events = ['topic', 'tags', 'author', 'replies'];
proxy.assign(events, function (topic, tags, author, replies) {
callback(null, '', topic, tags, author, replies);
}).fail(callback);
Topic.findOne({_id: id}, proxy.done(function (topic) {
if (!topic) {
proxy.unbind();
return callback(null, '此话题不存在或已被删除。');
}
proxy.emit('topic', topic);
TopicTag.find({topic_id: topic._id}, proxy.done(function (topic_tags) {
var tags_ids = [];
for (var i = 0; i < topic_tags.length; i++) {
tags_ids.push(topic_tags[i].tag_id);
}
Tag.getTagsByIds(tags_ids, proxy.done('tags'));
}));
User.getUserById(topic.author_id, proxy.done(function (author) {
if (!author) {
proxy.unbind();
return callback(null, '话题的作者丢了。');
}
proxy.emit('author', author);
}));
Reply.getRepliesByTopicId(topic._id, proxy.done('replies'));
}));
};
/**
* 更新主题的最后回复信息
* @param {String} topicId 主题ID
* @param {String} replyId 回复ID
* @param {Function} callback 回调函数
*/
exports.updateLastReply = function (topicId, replyId, callback) {
Topic.findOne({_id: topicId}, function (err, topic) {
if (err || !topic) {
return callback(err);
}
topic.last_reply = replyId;
topic.last_reply_at = new Date();
topic.reply_count += 1;
topic.save(callback);
});
};
/**
* 根据主题ID,查找一条主题
* @param {String} id 主题ID
* @param {Function} callback 回调函数
*/
exports.getTopic = function (id, callback) {
Topic.findOne({_id: id}, callback);
};
/**
* 将当前主题的回复计数减1,删除回复时用到
* @param {String} id 主题ID
* @param {Function} callback 回调函数
*/
exports.reduceCount = function (id, callback) {
Topic.findOne({_id: id}, function (err, topic) {
if (err) {
return callback(err);
}
if (!topic) {
return callback(new Error('该主题不存在'));
}
topic.reply_count -= 1;
topic.save(callback);
});
};
exports.newAndSave = function (title, content, authorId, callback) {
var topic = new Topic();
topic.title = title;
topic.content = content;
topic.author_id = authorId;
topic.save(callback);
};