finish up posts refactor

v1.18.x
barisusakli 10 years ago
parent 54ee8e59f6
commit 7225c0d45b

@ -20,10 +20,6 @@ module.exports = function(Posts) {
options.parse = options.hasOwnProperty('parse') ? options.parse : true; options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : []; 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); var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields);
Posts.getPostsFields(pids, fields, function(err, posts) { Posts.getPostsFields(pids, fields, function(err, posts) {
@ -35,13 +31,13 @@ module.exports = function(Posts) {
return !!p && parseInt(p.deleted, 10) !== 1; return !!p && parseInt(p.deleted, 10) !== 1;
}); });
var uids = [], tids = []; var uids = [], topicKeys = [];
for(var i=0; i<posts.length; ++i) { for(var i=0; i<posts.length; ++i) {
if (uids.indexOf(posts[i].uid) === -1) { if (uids.indexOf(posts[i].uid) === -1) {
uids.push(posts[i].uid); uids.push(posts[i].uid);
} }
if (tids.indexOf('topic:' + posts[i].tid) === -1) { if (topicKeys.indexOf('topic:' + posts[i].tid) === -1) {
tids.push('topic:' + posts[i].tid); topicKeys.push('topic:' + posts[i].tid);
} }
} }
@ -50,45 +46,12 @@ module.exports = function(Posts) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next); user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
}, },
topicsAndCategories: function(next) { topicsAndCategories: function(next) {
db.getObjectsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) { getTopicAndCategories(topicKeys, next);
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});
});
});
}, },
indices: function(next) { indices: function(next) {
Posts.getPostIndices(posts, uid, next); Posts.getPostIndices(posts, uid, next);
} }
}, function(err, results) { }, 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) { if (err) {
return callback(err); return callback(err);
} }
@ -112,7 +75,9 @@ module.exports = function(Posts) {
post.relativeTime = utils.toISOString(post.timestamp); post.relativeTime = utils.toISOString(post.timestamp);
if (!post.content || !options.parse) { if (!post.content || !options.parse) {
post.content = stripTags(post.content); if (options.stripTags) {
post.content = stripTags(post.content);
}
return next(null, post); return next(null, post);
} }
@ -120,8 +85,9 @@ module.exports = function(Posts) {
if (err) { if (err) {
return next(err); return next(err);
} }
if (options.stripTags) {
post.content = stripTags(post.content); post.content = stripTags(post.content);
}
next(null, post); 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;
}
}; };

@ -107,57 +107,34 @@ module.exports = function(Posts) {
}; };
Posts.getPostsByUid = function(callerUid, uid, start, end, callback) { Posts.getPostsByUid = function(callerUid, uid, start, end, callback) {
user.getPostIds(uid, start, end, function(err, pids) { async.waterfall([
if (err) { function(next) {
return callback(err); 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});
} }
], callback);
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});
});
});
});
}; };
Posts.getFavourites = function(uid, start, end, callback) { Posts.getFavourites = function(uid, start, end, callback) {
db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) { async.waterfall([
if (err) { function(next) {
return callback(err); db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, next);
} },
function(pids, next) {
getPosts(pids, uid, function(err, posts) { Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, next);
if (err) { },
return callback(err); function(posts, next) {
}
callback(null, {posts: posts, nextStart: end + 1}); 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);
callback(null, posts); };
});
}
}; };

Loading…
Cancel
Save