mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-14 17:56:16 +01:00
dont create term object on each call
This commit is contained in:
63
src/posts.js
63
src/posts.js
@@ -25,6 +25,7 @@ var async = require('async'),
|
|||||||
websockets = require('./socket.io');
|
websockets = require('./socket.io');
|
||||||
|
|
||||||
(function(Posts) {
|
(function(Posts) {
|
||||||
|
require('./posts/recent')(Posts);
|
||||||
require('./posts/delete')(Posts);
|
require('./posts/delete')(Posts);
|
||||||
|
|
||||||
Posts.create = function(data, callback) {
|
Posts.create = function(data, callback) {
|
||||||
@@ -160,68 +161,6 @@ var async = require('async'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.getRecentPosts = function(uid, start, stop, term, callback) {
|
|
||||||
var terms = {
|
|
||||||
day: 86400000,
|
|
||||||
week: 604800000,
|
|
||||||
month: 2592000000
|
|
||||||
};
|
|
||||||
|
|
||||||
var since = terms.day;
|
|
||||||
if (terms[term]) {
|
|
||||||
since = terms[term];
|
|
||||||
}
|
|
||||||
|
|
||||||
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
|
|
||||||
|
|
||||||
db.getSortedSetRevRangeByScore('posts:pid', start, count, Infinity, Date.now() - since, function(err, pids) {
|
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Array.isArray(pids) || !pids.length) {
|
|
||||||
return callback(null, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
privileges.posts.filter('read', pids, uid, function(err, pids) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Posts.getRecentPosterUids = function(start, end, callback) {
|
|
||||||
db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Array.isArray(pids) || !pids.length) {
|
|
||||||
return callback(null, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
pids = pids.map(function(pid) {
|
|
||||||
return 'post:' + pid;
|
|
||||||
});
|
|
||||||
|
|
||||||
db.getObjectsFields(pids, ['uid'], function(err, postData) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
postData = postData.map(function(post) {
|
|
||||||
return post && post.uid;
|
|
||||||
}).filter(function(value, index, array) {
|
|
||||||
return value && array.indexOf(value) === index;
|
|
||||||
});
|
|
||||||
|
|
||||||
callback(null, postData);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Posts.getUserInfoForPosts = function(uids, callback) {
|
Posts.getUserInfoForPosts = function(uids, callback) {
|
||||||
async.parallel({
|
async.parallel({
|
||||||
groups: function(next) {
|
groups: function(next) {
|
||||||
|
|||||||
70
src/posts/recent.js
Normal file
70
src/posts/recent.js
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var db = require('../database'),
|
||||||
|
privileges = require('../privileges');
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = function(Posts) {
|
||||||
|
var terms = {
|
||||||
|
day: 86400000,
|
||||||
|
week: 604800000,
|
||||||
|
month: 2592000000
|
||||||
|
};
|
||||||
|
|
||||||
|
Posts.getRecentPosts = function(uid, start, stop, term, callback) {
|
||||||
|
var since = terms.day;
|
||||||
|
if (terms[term]) {
|
||||||
|
since = terms[term];
|
||||||
|
}
|
||||||
|
|
||||||
|
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
|
||||||
|
|
||||||
|
db.getSortedSetRevRangeByScore('posts:pid', start, count, Infinity, Date.now() - since, function(err, pids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(pids) || !pids.length) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
privileges.posts.filter('read', pids, uid, function(err, pids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Posts.getRecentPosterUids = function(start, end, callback) {
|
||||||
|
db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(pids) || !pids.length) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
pids = pids.map(function(pid) {
|
||||||
|
return 'post:' + pid;
|
||||||
|
});
|
||||||
|
|
||||||
|
db.getObjectsFields(pids, ['uid'], function(err, postData) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
postData = postData.map(function(post) {
|
||||||
|
return post && post.uid;
|
||||||
|
}).filter(function(value, index, array) {
|
||||||
|
return value && array.indexOf(value) === index;
|
||||||
|
});
|
||||||
|
|
||||||
|
callback(null, postData);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
@@ -7,15 +7,15 @@ var async = require('async'),
|
|||||||
|
|
||||||
|
|
||||||
module.exports = function(Topics) {
|
module.exports = function(Topics) {
|
||||||
|
var terms = {
|
||||||
|
daily: 'day',
|
||||||
|
weekly: 'week',
|
||||||
|
monthly: 'month',
|
||||||
|
yearly: 'year'
|
||||||
|
};
|
||||||
|
|
||||||
Topics.getPopular = function(term, uid, count, callback) {
|
Topics.getPopular = function(term, uid, count, callback) {
|
||||||
count = parseInt(count, 10) || 20;
|
count = parseInt(count, 10) || 20;
|
||||||
var terms = {
|
|
||||||
daily: 'day',
|
|
||||||
weekly: 'week',
|
|
||||||
monthly: 'month',
|
|
||||||
yearly: 'year'
|
|
||||||
};
|
|
||||||
|
|
||||||
if (term === 'alltime') {
|
if (term === 'alltime') {
|
||||||
return getAllTimePopular(uid, count, callback);
|
return getAllTimePopular(uid, count, callback);
|
||||||
|
|||||||
@@ -5,6 +5,12 @@
|
|||||||
var db = require('./../database');
|
var db = require('./../database');
|
||||||
|
|
||||||
module.exports = function(Topics) {
|
module.exports = function(Topics) {
|
||||||
|
var terms = {
|
||||||
|
day: 86400000,
|
||||||
|
week: 604800000,
|
||||||
|
month: 2592000000,
|
||||||
|
year: 31104000000
|
||||||
|
};
|
||||||
|
|
||||||
Topics.getLatestTopics = function(uid, start, end, term, callback) {
|
Topics.getLatestTopics = function(uid, start, end, term, callback) {
|
||||||
Topics.getLatestTids(start, end, term, function(err, tids) {
|
Topics.getLatestTids(start, end, term, function(err, tids) {
|
||||||
@@ -17,15 +23,8 @@ module.exports = function(Topics) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Topics.getLatestTids = function(start, end, term, callback) {
|
Topics.getLatestTids = function(start, end, term, callback) {
|
||||||
var terms = {
|
|
||||||
day: 86400000,
|
|
||||||
week: 604800000,
|
|
||||||
month: 2592000000,
|
|
||||||
year: 31104000000
|
|
||||||
};
|
|
||||||
|
|
||||||
var since = terms.day;
|
var since = terms.day;
|
||||||
if(terms[term]) {
|
if (terms[term]) {
|
||||||
since = terms[term];
|
since = terms[term];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user