-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathreply.js
149 lines (139 loc) · 3.89 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
var models = require('../models');
var Reply = models.Reply;
var EventProxy = require('eventproxy');
var tools = require('../common/tools');
var User = require('./user');
var at = require('../common/at');
/**
* 获取一条回复信息
* @param {String} id 回复ID
* @param {Function} callback 回调函数
*/
exports.getReply = function (id, callback) {
Reply.findOne({_id: id}, callback);
};
/**
* 根据回复ID,获取回复
* Callback:
* - err, 数据库异常
* - reply, 回复内容
* @param {String} id 回复ID
* @param {Function} callback 回调函数
*/
exports.getReplyById = function (id, callback) {
if (!id) {
return callback(null, null);
}
Reply.findOne({_id: id}, function (err, reply) {
if (err) {
return callback(err);
}
if (!reply) {
return callback(err, null);
}
var author_id = reply.author_id;
User.getUserById(author_id, function (err, author) {
if (err) {
return callback(err);
}
reply.author = author;
// TODO: 添加更新方法,有些旧帖子可以转换为markdown格式的内容
if (reply.content_is_html) {
return callback(null, reply);
}
at.linkUsers(reply.content, function (err, str) {
if (err) {
return callback(err);
}
reply.content = str;
return callback(err, reply);
});
});
});
};
/**
* 根据主题ID,获取回复列表
* Callback:
* - err, 数据库异常
* - replies, 回复列表
* @param {String} id 主题ID
* @param {Function} callback 回调函数
*/
exports.getRepliesByTopicId = function (id, cb) {
Reply.find({topic_id: id, deleted: false}, '', {sort: 'create_at'}, function (err, replies) {
if (err) {
return cb(err);
}
if (replies.length === 0) {
return cb(null, []);
}
var proxy = new EventProxy();
proxy.after('reply_find', replies.length, function () {
cb(null, replies);
});
for (var j = 0; j < replies.length; j++) {
(function (i) {
var author_id = replies[i].author_id;
User.getUserById(author_id, function (err, author) {
if (err) {
return cb(err);
}
replies[i].author = author || { _id: '' };
if (replies[i].content_is_html) {
return proxy.emit('reply_find');
}
at.linkUsers(replies[i].content, function (err, str) {
if (err) {
return cb(err);
}
replies[i].content = str;
proxy.emit('reply_find');
});
});
})(j);
}
});
};
/**
* 创建并保存一条回复信息
* @param {String} content 回复内容
* @param {String} topicId 主题ID
* @param {String} authorId 回复作者
* @param {String} [replyId] 回复ID,当二级回复时设定该值
* @param {Function} callback 回调函数
*/
exports.newAndSave = function (content, topicId, authorId, replyId, callback) {
if (typeof replyId === 'function') {
callback = replyId;
replyId = null;
}
var reply = new Reply();
reply.content = content;
reply.topic_id = topicId;
reply.author_id = authorId;
if (replyId) {
reply.reply_id = replyId;
}
reply.save(function (err) {
callback(err, reply);
});
};
/**
* 根据topicId查询到最新的一条未删除回复
* @param topicId 主题ID
* @param callback 回调函数
*/
exports.getLastReplyByTopId = function (topicId, callback) {
Reply.find({topic_id: topicId, deleted: false}, '_id', {sort: {create_at : -1}, limit : 1}, callback);
};
exports.getRepliesByAuthorId = function (authorId, opt, callback) {
if (!callback) {
callback = opt;
opt = null;
}
Reply.find({author_id: authorId}, {}, opt, callback);
};
// 通过 author_id 获取回复总数
exports.getCountByAuthorId = function (authorId, callback) {
Reply.countDocuments({author_id: authorId}, callback);
};