mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat: add post-queue cache
This commit is contained in:
@@ -13,30 +13,47 @@ const notifications = require('../notifications');
|
|||||||
const privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
const utils = require('../utils');
|
const utils = require('../utils');
|
||||||
|
const cache = require('../cache');
|
||||||
const socketHelpers = require('../socket.io/helpers');
|
const socketHelpers = require('../socket.io/helpers');
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
Posts.getQueuedPosts = async () => {
|
Posts.getQueuedPosts = async (filter = {}, options = {}) => {
|
||||||
const ids = await db.getSortedSetRange('post:queue', 0, -1);
|
options = { metadata: true, ...options }; // defaults
|
||||||
const keys = ids.map(id => `post:queue:${id}`);
|
let postData;
|
||||||
const postData = await db.getObjects(keys);
|
if (cache.has('post-queue')) {
|
||||||
postData.forEach((data) => {
|
postData = cache.get('post-queue');
|
||||||
if (data) {
|
} else {
|
||||||
data.data = JSON.parse(data.data);
|
const ids = await db.getSortedSetRange('post:queue', 0, -1);
|
||||||
data.data.timestampISO = utils.toISOString(data.data.timestamp);
|
const keys = ids.map(id => `post:queue:${id}`);
|
||||||
}
|
postData = await db.getObjects(keys);
|
||||||
});
|
postData.forEach((data) => {
|
||||||
const uids = postData.map(data => data && data.uid);
|
if (data) {
|
||||||
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
|
data.data = JSON.parse(data.data);
|
||||||
postData.forEach((postData, index) => {
|
data.data.timestampISO = utils.toISOString(data.data.timestamp);
|
||||||
if (postData) {
|
}
|
||||||
postData.user = userData[index];
|
});
|
||||||
postData.data.rawContent = validator.escape(String(postData.data.content));
|
const uids = postData.map(data => data && data.uid);
|
||||||
postData.data.title = validator.escape(String(postData.data.title || ''));
|
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
|
||||||
}
|
postData.forEach((postData, index) => {
|
||||||
});
|
if (postData) {
|
||||||
|
postData.user = userData[index];
|
||||||
|
postData.data.rawContent = validator.escape(String(postData.data.content));
|
||||||
|
postData.data.title = validator.escape(String(postData.data.title || ''));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
cache.set('post-queue', postData);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.metadata) {
|
||||||
|
await Promise.all(postData.map(p => addMetaData(p)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter by tid if present
|
||||||
|
if (isFinite(filter.tid)) {
|
||||||
|
const tid = parseInt(filter.tid, 10);
|
||||||
|
postData = postData.filter(item => item.data.tid && parseInt(item.data.tid, 10) === tid);
|
||||||
|
}
|
||||||
|
|
||||||
await Promise.all(postData.map(p => addMetaData(p)));
|
|
||||||
return postData;
|
return postData;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user