Files
NodeBB/src/posts/queue.js

235 lines
6.2 KiB
JavaScript
Raw Normal View History

'use strict';
2019-01-04 13:27:31 -05:00
const _ = require('lodash');
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 socketHelpers = require('../socket.io/helpers');
module.exports = function (Posts) {
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) {
await notifications.rescind('post-queue-' + id);
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();
const id = type + '-' + now;
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);
await db.setObject('post:queue:' + id, payload);
2019-07-17 19:05:55 -04:00
await user.setUserField(data.uid, 'lastqueuetime', now);
const cid = await getCid(type, data);
const uids = await getNotificationUids(cid);
const notifObj = await notifications.create({
type: 'post-queue',
nid: 'post-queue-' + id,
mergeId: 'post-queue',
bodyShort: '[[notifications:post_awaiting_review]]',
bodyLong: data.content,
path: '/post-queue',
});
await notifications.push(notifObj, uids);
return {
id: id,
type: type,
queued: true,
message: '[[success:post-queued]]',
};
};
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);
}
}
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) {
await removeQueueNotification(id);
await db.sortedSetRemove('post:queue', id);
await db.delete('post:queue:' + id);
};
2019-07-17 19:05:55 -04:00
Posts.submitFromQueue = async function (id) {
const data = await getParsedObject(id);
if (!data) {
return;
}
if (data.type === 'topic') {
await createTopic(data.data);
} else if (data.type === 'reply') {
await createReply(data.data);
}
await Posts.removeFromQueue(id);
};
2019-07-17 19:05:55 -04:00
async function getParsedObject(id) {
const data = await db.getObject('post:queue:' + id);
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 });
}
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);
}
2017-10-31 10:53:28 -04:00
Posts.editQueuedContent = async function (uid, editData) {
const canEditQueue = await Posts.canEditQueue(uid, editData);
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;
}
await db.setObjectField('post:queue:' + editData.id, 'data', JSON.stringify(data.data));
2017-10-31 10:53:28 -04:00
};
2017-10-31 16:04:25 -04:00
Posts.canEditQueue = async function (uid, editData) {
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;
}
if (isAdminOrGlobalMod) {
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
};
};