Files
NodeBB/src/posts/queue.js

367 lines
11 KiB
JavaScript
Raw Normal View History

'use strict';
2019-01-04 13:27:31 -05:00
const _ = require('lodash');
const validator = require('validator');
const nconf = require('nconf');
2019-09-15 02:14:51 -04:00
const db = require('../database');
const user = require('../user');
const meta = require('../meta');
const groups = require('../groups');
const topics = require('../topics');
const categories = require('../categories');
const notifications = require('../notifications');
const privileges = require('../privileges');
const plugins = require('../plugins');
const utils = require('../utils');
2021-02-12 15:10:30 -05:00
const cache = require('../cache');
2019-09-15 02:14:51 -04:00
const socketHelpers = require('../socket.io/helpers');
module.exports = function (Posts) {
2021-02-12 15:10:30 -05:00
Posts.getQueuedPosts = async (filter = {}, options = {}) => {
2021-11-18 16:42:18 -05:00
options = { metadata: true, ...options }; // defaults
let postData = _.cloneDeep(cache.get('post-queue'));
if (!postData) {
2021-02-12 15:10:30 -05:00
const ids = await db.getSortedSetRange('post:queue', 0, -1);
const keys = ids.map(id => `post:queue:${id}`);
postData = await db.getObjects(keys);
postData.forEach((data) => {
if (data) {
data.data = JSON.parse(data.data);
data.data.timestampISO = utils.toISOString(data.data.timestamp);
}
});
const uids = postData.map(data => data && data.uid);
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', _.cloneDeep(postData));
2021-02-12 15:10:30 -05:00
}
if (filter.id) {
postData = postData.filter(p => p.id === filter.id);
}
2021-02-12 15:10:30 -05:00
if (options.metadata) {
await Promise.all(postData.map(p => addMetaData(p)));
}
// Filter by tid if present
if (utils.isNumber(filter.tid)) {
2021-02-12 15:10:30 -05:00
const tid = parseInt(filter.tid, 10);
postData = postData.filter(item => item.data.tid && parseInt(item.data.tid, 10) === tid);
} else if (Array.isArray(filter.tid)) {
const tids = filter.tid.map(tid => parseInt(tid, 10));
postData = postData.filter(
item => item.data.tid && tids.includes(parseInt(item.data.tid, 10))
);
2021-02-12 15:10:30 -05:00
}
return postData;
};
async function addMetaData(postData) {
if (!postData) {
return;
}
postData.topic = { cid: 0 };
if (postData.data.cid) {
postData.topic = { cid: parseInt(postData.data.cid, 10) };
} else if (postData.data.tid) {
postData.topic = await topics.getTopicFields(postData.data.tid, ['title', 'cid']);
}
postData.category = await categories.getCategoryData(postData.topic.cid);
const result = await plugins.hooks.fire('filter:parse.post', { postData: postData.data });
postData.data.content = result.postData.content;
}
2019-07-17 19:05:55 -04:00
Posts.shouldQueue = async function (uid, data) {
const [userData, isMemberOfExempt, categoryQueueEnabled] = await Promise.all([
user.getUserFields(uid, ['uid', 'reputation', 'postcount']),
groups.isMemberOfAny(uid, meta.config.groupsExemptFromPostQueue),
isCategoryQueueEnabled(data),
]);
const shouldQueue = meta.config.postQueue && categoryQueueEnabled &&
!isMemberOfExempt &&
(!userData.uid || userData.reputation < meta.config.postQueueReputationThreshold || userData.postcount <= 0);
const result = await plugins.hooks.fire('filter:post.shouldQueue', {
2019-07-17 19:05:55 -04:00
shouldQueue: !!shouldQueue,
uid: uid,
data: data,
});
return result.shouldQueue;
};
async function isCategoryQueueEnabled(data) {
const type = getType(data);
const cid = await getCid(type, data);
if (!cid) {
throw new Error('[[error:invalid-cid]]');
}
return await categories.getCategoryField(cid, 'postQueue');
}
function getType(data) {
2020-12-06 13:53:52 -05:00
if (data.hasOwnProperty('tid')) {
return 'reply';
2020-12-06 13:53:52 -05:00
} else if (data.hasOwnProperty('cid')) {
return 'topic';
}
throw new Error('[[error:invalid-type]]');
}
2019-07-17 19:05:55 -04:00
async function removeQueueNotification(id) {
2021-02-03 23:59:08 -07:00
await notifications.rescind(`post-queue-${id}`);
2019-07-17 19:05:55 -04:00
const data = await getParsedObject(id);
if (!data) {
return;
}
const cid = await getCid(data.type, data);
const uids = await getNotificationUids(cid);
uids.forEach(uid => user.notifications.pushCount(uid));
2019-01-04 13:27:31 -05:00
}
2019-07-17 19:05:55 -04:00
async function getNotificationUids(cid) {
const results = await Promise.all([
groups.getMembersOfGroups(['administrators', 'Global Moderators']),
categories.getModeratorUids([cid]),
]);
return _.uniq(_.flattenDeep(results));
2019-01-04 13:27:31 -05:00
}
2019-07-17 19:05:55 -04:00
Posts.addToQueue = async function (data) {
const type = getType(data);
2019-07-17 19:05:55 -04:00
const now = Date.now();
2021-02-03 23:59:08 -07:00
const id = `${type}-${now}`;
2019-07-17 19:05:55 -04:00
await canPost(type, data);
let payload = {
2019-07-17 19:05:55 -04:00
id: id,
uid: data.uid,
type: type,
data: data,
};
payload = await plugins.hooks.fire('filter:post-queue.save', payload);
payload.data = JSON.stringify(data);
await db.sortedSetAdd('post:queue', now, id);
2021-02-03 23:59:08 -07:00
await db.setObject(`post:queue:${id}`, payload);
2019-07-17 19:05:55 -04:00
await user.setUserField(data.uid, 'lastqueuetime', now);
cache.del('post-queue');
2019-07-17 19:05:55 -04:00
const cid = await getCid(type, data);
const uids = await getNotificationUids(cid);
const bodyLong = await parseBodyLong(cid, type, data);
2019-07-17 19:05:55 -04:00
const notifObj = await notifications.create({
type: 'post-queue',
2021-02-03 23:59:08 -07:00
nid: `post-queue-${id}`,
2019-07-17 19:05:55 -04:00
mergeId: 'post-queue',
bodyShort: '[[notifications:post_awaiting_review]]',
bodyLong: bodyLong,
path: `/post-queue/${id}`,
2019-07-17 19:05:55 -04:00
});
await notifications.push(notifObj, uids);
return {
id: id,
type: type,
queued: true,
message: '[[success:post-queued]]',
};
};
async function parseBodyLong(cid, type, data) {
const url = nconf.get('url');
const [content, category, userData] = await Promise.all([
plugins.hooks.fire('filter:parse.raw', data.content),
categories.getCategoryFields(cid, ['name', 'slug']),
user.getUserFields(data.uid, ['uid', 'username']),
]);
category.url = `${url}/category/${category.slug}`;
if (userData.uid > 0) {
userData.url = `${url}/uid/${userData.uid}`;
}
const topic = { cid: cid, title: data.title, tid: data.tid };
if (type === 'reply') {
topic.title = await topics.getTopicField(data.tid, 'title');
topic.url = `${url}/topic/${data.tid}`;
}
const { app } = require('../webserver');
return await app.renderAsync('emails/partials/post-queue-body', {
content: content,
category: category,
user: userData,
topic: topic,
});
}
2019-07-17 19:05:55 -04:00
async function getCid(type, data) {
if (type === 'topic') {
2019-07-17 19:05:55 -04:00
return data.cid;
} else if (type === 'reply') {
2019-07-17 19:05:55 -04:00
return await topics.getTopicField(data.tid, 'cid');
}
2019-07-17 19:05:55 -04:00
return null;
}
2019-07-17 19:05:55 -04:00
async function canPost(type, data) {
const cid = await getCid(type, data);
const typeToPrivilege = {
topic: 'topics:create',
reply: 'topics:reply',
};
topics.checkContent(data.content);
if (type === 'topic') {
topics.checkTitle(data.title);
2020-07-31 14:54:16 -04:00
if (data.tags) {
await topics.validateTags(data.tags, cid, data.uid);
2020-07-31 14:54:16 -04:00
}
}
2019-07-17 19:05:55 -04:00
const [canPost] = await Promise.all([
privileges.categories.can(typeToPrivilege[type], cid, data.uid),
user.isReadyToQueue(data.uid, cid),
]);
if (!canPost) {
throw new Error('[[error:no-privileges]]');
}
}
2019-07-17 19:05:55 -04:00
Posts.removeFromQueue = async function (id) {
const data = await getParsedObject(id);
if (!data) {
return null;
}
2022-03-11 12:27:07 -05:00
const result = await plugins.hooks.fire('filter:post-queue:removeFromQueue', { data: data });
await removeFromQueue(id);
plugins.hooks.fire('action:post-queue:removeFromQueue', { data: result.data });
return result.data;
};
async function removeFromQueue(id) {
2019-07-17 19:05:55 -04:00
await removeQueueNotification(id);
await db.sortedSetRemove('post:queue', id);
2021-02-03 23:59:08 -07:00
await db.delete(`post:queue:${id}`);
cache.del('post-queue');
2022-03-11 12:27:07 -05:00
}
2019-07-17 19:05:55 -04:00
Posts.submitFromQueue = async function (id) {
2022-03-11 12:27:07 -05:00
let data = await getParsedObject(id);
2019-07-17 19:05:55 -04:00
if (!data) {
return null;
2019-07-17 19:05:55 -04:00
}
2022-03-11 12:27:07 -05:00
const result = await plugins.hooks.fire('filter:post-queue:submitFromQueue', { data: data });
data = result.data;
2019-07-17 19:05:55 -04:00
if (data.type === 'topic') {
const result = await createTopic(data.data);
data.pid = result.postData.pid;
2019-07-17 19:05:55 -04:00
} else if (data.type === 'reply') {
const result = await createReply(data.data);
data.pid = result.pid;
2019-07-17 19:05:55 -04:00
}
2022-03-11 12:27:07 -05:00
await removeFromQueue(id);
plugins.hooks.fire('action:post-queue:submitFromQueue', { data: data });
return data;
};
Posts.getFromQueue = async function (id) {
return await getParsedObject(id);
};
2019-07-17 19:05:55 -04:00
async function getParsedObject(id) {
2021-02-03 23:59:08 -07:00
const data = await db.getObject(`post:queue:${id}`);
2019-07-17 19:05:55 -04:00
if (!data) {
return null;
}
data.data = JSON.parse(data.data);
data.data.fromQueue = true;
2019-07-17 19:05:55 -04:00
return data;
2017-10-31 16:04:25 -04:00
}
2019-07-17 19:05:55 -04:00
async function createTopic(data) {
const result = await topics.post(data);
socketHelpers.notifyNew(data.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
return result;
}
2019-07-17 19:05:55 -04:00
async function createReply(data) {
const postData = await topics.reply(data);
const result = {
posts: [postData],
'reputation:disabled': !!meta.config['reputation:disabled'],
'downvote:disabled': !!meta.config['downvote:disabled'],
};
socketHelpers.notifyNew(data.uid, 'newPost', result);
return postData;
}
2017-10-31 10:53:28 -04:00
Posts.editQueuedContent = async function (uid, editData) {
const canEditQueue = await Posts.canEditQueue(uid, editData, 'edit');
2019-07-17 19:05:55 -04:00
if (!canEditQueue) {
throw new Error('[[error:no-privileges]]');
}
const data = await getParsedObject(editData.id);
2019-07-17 19:05:55 -04:00
if (!data) {
return;
}
if (editData.content !== undefined) {
data.data.content = editData.content;
}
if (editData.title !== undefined) {
data.data.title = editData.title;
}
if (editData.cid !== undefined) {
data.data.cid = editData.cid;
}
2021-02-03 23:59:08 -07:00
await db.setObjectField(`post:queue:${editData.id}`, 'data', JSON.stringify(data.data));
cache.del('post-queue');
2017-10-31 10:53:28 -04:00
};
2017-10-31 16:04:25 -04:00
Posts.canEditQueue = async function (uid, editData, action) {
2019-07-17 19:05:55 -04:00
const [isAdminOrGlobalMod, data] = await Promise.all([
user.isAdminOrGlobalMod(uid),
getParsedObject(editData.id),
2019-07-17 19:05:55 -04:00
]);
if (!data) {
return false;
}
const selfPost = parseInt(uid, 10) === parseInt(data.uid, 10);
if (isAdminOrGlobalMod || ((action === 'reject' || action === 'edit') && selfPost)) {
2019-07-17 19:05:55 -04:00
return true;
}
let cid;
if (data.type === 'topic') {
cid = data.data.cid;
} else if (data.type === 'reply') {
cid = await topics.getTopicField(data.data.tid, 'cid');
}
const isModerator = await user.isModerator(uid, cid);
let isModeratorOfTargetCid = true;
if (editData.cid) {
isModeratorOfTargetCid = await user.isModerator(uid, editData.cid);
}
return isModerator && isModeratorOfTargetCid;
2017-10-31 16:04:25 -04:00
};
Posts.updateQueuedPostsTopic = async function (newTid, tids) {
const postData = await Posts.getQueuedPosts({ tid: tids }, { metadata: false });
if (postData.length) {
postData.forEach((post) => {
post.data.tid = newTid;
});
await db.setObjectBulk(
postData.map(p => [`post:queue:${p.id}`, { data: JSON.stringify(p.data) }]),
);
cache.del('post-queue');
}
};
};