Skip to content

Node.js based forum software built for the modern web

finish up posts refactor

barisusakli7225c0d45bf8
diff --git a/src/posts/summary.js b/src/posts/summary.js
index 9b1d880176..3728818911 100644
--- a/src/posts/summary.js
+++ b/src/posts/summary.js
@@ -20,10 +20,6 @@ module.exports = function(Posts) {
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
- if (!Array.isArray(pids) || !pids.length) {
- return callback(null, []);
- }
-
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields);
Posts.getPostsFields(pids, fields, function(err, posts) {
@@ -35,13 +31,13 @@ module.exports = function(Posts) {
return !!p && parseInt(p.deleted, 10) !== 1;
});
- var uids = [], tids = [];
+ var uids = [], topicKeys = [];
for(var i=0; i<posts.length; ++i) {
if (uids.indexOf(posts[i].uid) === -1) {
uids.push(posts[i].uid);
}
- if (tids.indexOf('topic:' + posts[i].tid) === -1) {
- tids.push('topic:' + posts[i].tid);
+ if (topicKeys.indexOf('topic:' + posts[i].tid) === -1) {
+ topicKeys.push('topic:' + posts[i].tid);
}
}
@@ -50,45 +46,12 @@ module.exports = function(Posts) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
topicsAndCategories: function(next) {
- db.getObjectsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) {
- if (err) {
- return next(err);
- }
-
- var cids = topics.map(function(topic) {
- if (topic) {
- topic.title = validator.escape(topic.title);
- }
- return topic && topic.cid;
- }).filter(function(value, index, array) {
- return value && array.indexOf(value) === index;
- });
-
- categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug'], function(err, categories) {
- next(err, {topics: topics, categories: categories});
- });
- });
+ getTopicAndCategories(topicKeys, next);
},
indices: function(next) {
Posts.getPostIndices(posts, uid, next);
}
}, function(err, results) {
- function toObject(key, data) {
- var obj = {};
- for(var i=0; i<data.length; ++i) {
- obj[data[i][key]] = data[i];
- }
- return obj;
- }
-
- function stripTags(content) {
- if (options.stripTags && content) {
- var s = S(content);
- return s.stripTags.apply(s, utils.stripTags).s;
- }
- return content;
- }
-
if (err) {
return callback(err);
}
@@ -112,7 +75,9 @@ module.exports = function(Posts) {
post.relativeTime = utils.toISOString(post.timestamp);
if (!post.content || !options.parse) {
- post.content = stripTags(post.content);
+ if (options.stripTags) {
+ post.content = stripTags(post.content);
+ }
return next(null, post);
}
@@ -120,8 +85,9 @@ module.exports = function(Posts) {
if (err) {
return next(err);
}
-
- post.content = stripTags(post.content);
+ if (options.stripTags) {
+ post.content = stripTags(post.content);
+ }
next(null, post);
});
@@ -133,4 +99,41 @@ module.exports = function(Posts) {
});
});
};
+
+ function getTopicAndCategories(topicKeys, callback) {
+ db.getObjectsFields(topicKeys, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) {
+ if (err) {
+ return callback(err);
+ }
+
+ var cids = topics.map(function(topic) {
+ if (topic) {
+ topic.title = validator.escape(topic.title);
+ }
+ return topic && topic.cid;
+ }).filter(function(topic, index, array) {
+ return topic && array.indexOf(topic) === index;
+ });
+
+ categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug'], function(err, categories) {
+ callback(err, {topics: topics, categories: categories});
+ });
+ });
+ }
+
+ function toObject(key, data) {
+ var obj = {};
+ for(var i=0; i<data.length; ++i) {
+ obj[data[i][key]] = data[i];
+ }
+ return obj;
+ }
+
+ function stripTags(content) {
+ if (content) {
+ var s = S(content);
+ return s.stripTags.apply(s, utils.stripTags).s;
+ }
+ return content;
+ }
};
\ No newline at end of file
diff --git a/src/posts/user.js b/src/posts/user.js
index 9bc24c82eb..4fbaa96654 100644
--- a/src/posts/user.js
+++ b/src/posts/user.js
@@ -107,57 +107,34 @@ module.exports = function(Posts) {
};
Posts.getPostsByUid = function(callerUid, uid, start, end, callback) {
- user.getPostIds(uid, start, end, function(err, pids) {
- if (err) {
- return callback(err);
+ async.waterfall([
+ function(next) {
+ user.getPostIds(uid, start, end, next);
+ },
+ function(pids, next) {
+ privileges.posts.filter('read', pids, callerUid, next);
+ },
+ function(pids, next) {
+ Posts.getPostSummaryByPids(pids, callerUid, {stripTags: false}, next);
+ },
+ function(posts, next) {
+ next(null, {posts: posts, nextStart: end + 1});
}
-
- privileges.posts.filter('read', pids, callerUid, function(err, pids) {
- if (err) {
- return callback(err);
- }
- getPosts(pids, callerUid, function(err, posts) {
- if (err) {
- return callback(err);
- }
- callback(null, {posts: posts, nextStart: end + 1});
- });
- });
- });
+ ], callback);
};
Posts.getFavourites = function(uid, start, end, callback) {
- db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) {
- if (err) {
- return callback(err);
- }
-
- getPosts(pids, uid, function(err, posts) {
- if (err) {
- return callback(err);
- }
+ async.waterfall([
+ function(next) {
+ db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, next);
+ },
+ function(pids, next) {
+ Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, next);
+ },
+ function(posts, next) {
callback(null, {posts: posts, nextStart: end + 1});
- });
- });
- };
-
- function getPosts(pids, uid, callback) {
- if (!Array.isArray(pids) || !pids.length) {
- return callback(null, []);
- }
-
- Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, function(err, posts) {
- if (err) {
- return callback(err);
- }
-
- if (!Array.isArray(posts) || !posts.length) {
- return callback(null, []);
}
-
- callback(null, posts);
- });
- }
-
+ ], callback);
+ };
};