mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: async/await flags.js
This commit is contained in:
@@ -113,9 +113,7 @@ Categories.getModeratorUids = async function (cids) {
|
|||||||
const uniqGroups = _.uniq(_.flatten(sets.groupNames));
|
const uniqGroups = _.uniq(_.flatten(sets.groupNames));
|
||||||
const groupUids = await groups.getMembersOfGroups(uniqGroups);
|
const groupUids = await groups.getMembersOfGroups(uniqGroups);
|
||||||
const map = _.zipObject(uniqGroups, groupUids);
|
const map = _.zipObject(uniqGroups, groupUids);
|
||||||
const moderatorUids = cids.map(function (cid, index) {
|
const moderatorUids = cids.map((cid, index) => _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g])))));
|
||||||
return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))));
|
|
||||||
});
|
|
||||||
return moderatorUids;
|
return moderatorUids;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
338
src/flags.js
338
src/flags.js
@@ -1,6 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const async = require('async');
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const winston = require('winston');
|
const winston = require('winston');
|
||||||
const validator = require('validator');
|
const validator = require('validator');
|
||||||
@@ -11,6 +10,7 @@ const groups = require('./groups');
|
|||||||
const meta = require('./meta');
|
const meta = require('./meta');
|
||||||
const notifications = require('./notifications');
|
const notifications = require('./notifications');
|
||||||
const analytics = require('./analytics');
|
const analytics = require('./analytics');
|
||||||
|
const categories = require('./categories');
|
||||||
const topics = require('./topics');
|
const topics = require('./topics');
|
||||||
const posts = require('./posts');
|
const posts = require('./posts');
|
||||||
const privileges = require('./privileges');
|
const privileges = require('./privileges');
|
||||||
@@ -343,265 +343,141 @@ Flags.getTargetCid = async function (type, id) {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
Flags.update = function (flagId, uid, changeset, callback) {
|
Flags.update = async function (flagId, uid, changeset) {
|
||||||
// Retrieve existing flag data to compare for history-saving purposes
|
const now = changeset.datetime || Date.now();
|
||||||
var fields = ['state', 'assignee'];
|
const notifyAssignee = async function (assigneeId) {
|
||||||
var tasks = [];
|
|
||||||
var now = changeset.datetime || Date.now();
|
|
||||||
var notifyAssignee = function (assigneeId, next) {
|
|
||||||
if (assigneeId === '' || parseInt(uid, 10) === parseInt(assigneeId, 10)) {
|
if (assigneeId === '' || parseInt(uid, 10) === parseInt(assigneeId, 10)) {
|
||||||
// Do nothing
|
return;
|
||||||
return next();
|
|
||||||
}
|
}
|
||||||
// Notify assignee of this update
|
const notifObj = await notifications.create({
|
||||||
notifications.create({
|
|
||||||
type: 'my-flags',
|
type: 'my-flags',
|
||||||
bodyShort: '[[notifications:flag_assigned_to_you, ' + flagId + ']]',
|
bodyShort: '[[notifications:flag_assigned_to_you, ' + flagId + ']]',
|
||||||
bodyLong: '',
|
bodyLong: '',
|
||||||
path: '/flags/' + flagId,
|
path: '/flags/' + flagId,
|
||||||
nid: 'flags:assign:' + flagId + ':uid:' + assigneeId,
|
nid: 'flags:assign:' + flagId + ':uid:' + assigneeId,
|
||||||
from: uid,
|
from: uid,
|
||||||
}, function (err, notification) {
|
|
||||||
if (err || !notification) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
notifications.push(notification, [assigneeId], next);
|
|
||||||
});
|
});
|
||||||
|
await notifications.push(notifObj, [assigneeId]);
|
||||||
};
|
};
|
||||||
|
|
||||||
async.waterfall([
|
// Retrieve existing flag data to compare for history-saving purposes
|
||||||
async.apply(db.getObjectFields.bind(db), 'flag:' + flagId, fields),
|
const current = await db.getObjectFields('flag:' + flagId, ['state', 'assignee']);
|
||||||
function (current, next) {
|
const tasks = [];
|
||||||
for (var prop in changeset) {
|
for (var prop in changeset) {
|
||||||
if (changeset.hasOwnProperty(prop)) {
|
if (changeset.hasOwnProperty(prop)) {
|
||||||
if (current[prop] === changeset[prop]) {
|
if (current[prop] === changeset[prop]) {
|
||||||
delete changeset[prop];
|
delete changeset[prop];
|
||||||
} else {
|
} else if (prop === 'state') {
|
||||||
// Add tasks as necessary
|
tasks.push(db.sortedSetAdd('flags:byState:' + changeset[prop], now, flagId));
|
||||||
switch (prop) {
|
tasks.push(db.sortedSetRemove('flags:byState:' + current[prop], flagId));
|
||||||
case 'state':
|
} else if (prop === 'assignee') {
|
||||||
tasks.push(async.apply(db.sortedSetAdd.bind(db), 'flags:byState:' + changeset[prop], now, flagId));
|
tasks.push(db.sortedSetAdd('flags:byAssignee:' + changeset[prop], now, flagId));
|
||||||
tasks.push(async.apply(db.sortedSetRemove.bind(db), 'flags:byState:' + current[prop], flagId));
|
tasks.push(notifyAssignee(changeset[prop]));
|
||||||
break;
|
|
||||||
|
|
||||||
case 'assignee':
|
|
||||||
tasks.push(async.apply(db.sortedSetAdd.bind(db), 'flags:byAssignee:' + changeset[prop], now, flagId));
|
|
||||||
tasks.push(async.apply(notifyAssignee, changeset[prop]));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!Object.keys(changeset).length) {
|
if (!Object.keys(changeset).length) {
|
||||||
// No changes
|
return;
|
||||||
return next();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Save new object to db (upsert)
|
tasks.push(db.setObject('flag:' + flagId, changeset));
|
||||||
tasks.push(async.apply(db.setObject, 'flag:' + flagId, changeset));
|
tasks.push(Flags.appendHistory(flagId, uid, changeset));
|
||||||
|
tasks.push(plugins.fireHook('action:flags.update', { flagId: flagId, changeset: changeset, uid: uid }));
|
||||||
// Append history
|
await Promise.all(tasks);
|
||||||
tasks.push(async.apply(Flags.appendHistory, flagId, uid, changeset));
|
|
||||||
|
|
||||||
// Fire plugin hook
|
|
||||||
tasks.push(async.apply(plugins.fireHook, 'action:flags.update', { flagId: flagId, changeset: changeset, uid: uid }));
|
|
||||||
|
|
||||||
async.parallel(tasks, function (err) {
|
|
||||||
return next(err);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Flags.getHistory = function (flagId, callback) {
|
Flags.getHistory = async function (flagId) {
|
||||||
var history;
|
const uids = [];
|
||||||
var uids = [];
|
let history = await db.getSortedSetRevRangeWithScores('flag:' + flagId + ':history', 0, -1);
|
||||||
async.waterfall([
|
|
||||||
async.apply(db.getSortedSetRevRangeWithScores.bind(db), 'flag:' + flagId + ':history', 0, -1),
|
|
||||||
function (_history, next) {
|
|
||||||
history = _history.map(function (entry) {
|
|
||||||
try {
|
|
||||||
entry.value = JSON.parse(entry.value);
|
|
||||||
} catch (e) {
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
uids.push(entry.value[0]);
|
history = history.map(function (entry) {
|
||||||
|
entry.value = JSON.parse(entry.value);
|
||||||
|
|
||||||
// Deserialise changeset
|
uids.push(entry.value[0]);
|
||||||
var changeset = entry.value[1];
|
|
||||||
if (changeset.hasOwnProperty('state')) {
|
|
||||||
changeset.state = changeset.state === undefined ? '' : '[[flags:state-' + changeset.state + ']]';
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
// Deserialise changeset
|
||||||
uid: entry.value[0],
|
const changeset = entry.value[1];
|
||||||
fields: changeset,
|
if (changeset.hasOwnProperty('state')) {
|
||||||
datetime: entry.score,
|
changeset.state = changeset.state === undefined ? '' : '[[flags:state-' + changeset.state + ']]';
|
||||||
datetimeISO: utils.toISOString(entry.score),
|
}
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
|
return {
|
||||||
},
|
uid: entry.value[0],
|
||||||
function (users, next) {
|
fields: changeset,
|
||||||
// Append user data to each history event
|
datetime: entry.score,
|
||||||
history = history.map(function (event, idx) {
|
datetimeISO: utils.toISOString(entry.score),
|
||||||
event.user = users[idx];
|
};
|
||||||
return event;
|
});
|
||||||
});
|
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
|
||||||
|
history.forEach((event, idx) => { event.user = userData[idx]; });
|
||||||
next(null, history);
|
return history;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Flags.appendHistory = function (flagId, uid, changeset, callback) {
|
Flags.appendHistory = async function (flagId, uid, changeset) {
|
||||||
var payload;
|
const datetime = changeset.datetime || Date.now();
|
||||||
var datetime = changeset.datetime || Date.now();
|
|
||||||
delete changeset.datetime;
|
delete changeset.datetime;
|
||||||
|
const payload = JSON.stringify([uid, changeset, datetime]);
|
||||||
try {
|
await db.sortedSetAdd('flag:' + flagId + ':history', datetime, payload);
|
||||||
payload = JSON.stringify([uid, changeset, datetime]);
|
|
||||||
} catch (e) {
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
db.sortedSetAdd('flag:' + flagId + ':history', datetime, payload, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Flags.appendNote = function (flagId, uid, note, datetime, callback) {
|
Flags.appendNote = async function (flagId, uid, note, datetime) {
|
||||||
if (typeof datetime === 'function' && !callback) {
|
datetime = datetime || Date.now();
|
||||||
callback = datetime;
|
const payload = JSON.stringify([uid, note]);
|
||||||
datetime = Date.now();
|
await db.sortedSetAdd('flag:' + flagId + ':notes', datetime, payload);
|
||||||
}
|
Flags.appendHistory(flagId, uid, {
|
||||||
|
notes: null,
|
||||||
var payload;
|
datetime: datetime,
|
||||||
try {
|
});
|
||||||
payload = JSON.stringify([uid, note]);
|
|
||||||
} catch (e) {
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
async.apply(db.sortedSetAdd, 'flag:' + flagId + ':notes', datetime, payload),
|
|
||||||
async.apply(Flags.appendHistory, flagId, uid, {
|
|
||||||
notes: null,
|
|
||||||
datetime: datetime,
|
|
||||||
}),
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Flags.notify = function (flagObj, uid, callback) {
|
Flags.notify = async function (flagObj, uid) {
|
||||||
// Notify administrators, mods, and other associated people
|
const [admins, globalMods] = await Promise.all([
|
||||||
if (!callback) {
|
groups.getMembers('administrators', 0, -1),
|
||||||
callback = function () {};
|
groups.getMembers('Global Moderators', 0, -1),
|
||||||
|
]);
|
||||||
|
let uids = admins.concat(globalMods);
|
||||||
|
let notifObj = null;
|
||||||
|
if (flagObj.type === 'post') {
|
||||||
|
const [title, cid] = await Promise.all([
|
||||||
|
topics.getTitleByPid(flagObj.targetId),
|
||||||
|
posts.getCidByPid(flagObj.targetId),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const modUids = await categories.getModeratorUids([cid]);
|
||||||
|
const titleEscaped = utils.decodeHTMLEntities(title).replace(/%/g, '%').replace(/,/g, ',');
|
||||||
|
|
||||||
|
notifObj = await notifications.create({
|
||||||
|
type: 'new-post-flag',
|
||||||
|
bodyShort: '[[notifications:user_flagged_post_in, ' + flagObj.reporter.username + ', ' + titleEscaped + ']]',
|
||||||
|
bodyLong: flagObj.description,
|
||||||
|
pid: flagObj.targetId,
|
||||||
|
path: '/flags/' + flagObj.flagId,
|
||||||
|
nid: 'flag:post:' + flagObj.targetId + ':uid:' + uid,
|
||||||
|
from: uid,
|
||||||
|
mergeId: 'notifications:user_flagged_post_in|' + flagObj.targetId,
|
||||||
|
topicTitle: title,
|
||||||
|
});
|
||||||
|
uids = uids.concat(modUids[0]);
|
||||||
|
} else if (flagObj.type === 'user') {
|
||||||
|
notifObj = await notifications.create({
|
||||||
|
type: 'new-user-flag',
|
||||||
|
bodyShort: '[[notifications:user_flagged_user, ' + flagObj.reporter.username + ', ' + flagObj.target.username + ']]',
|
||||||
|
bodyLong: flagObj.description,
|
||||||
|
path: '/flags/' + flagObj.flagId,
|
||||||
|
nid: 'flag:user:' + flagObj.targetId + ':uid:' + uid,
|
||||||
|
from: uid,
|
||||||
|
mergeId: 'notifications:user_flagged_user|' + flagObj.targetId,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (flagObj.type) {
|
plugins.fireHook('action:flags.create', {
|
||||||
case 'post':
|
flag: flagObj,
|
||||||
async.parallel({
|
});
|
||||||
post: function (next) {
|
uids = uids.filter(_uid => parseInt(_uid, 10) !== parseInt(uid, 10));
|
||||||
async.waterfall([
|
await notifications.push(notifObj, uids);
|
||||||
async.apply(posts.getPostData, flagObj.targetId),
|
|
||||||
async.apply(posts.parsePost),
|
|
||||||
], next);
|
|
||||||
},
|
|
||||||
title: async.apply(topics.getTitleByPid, flagObj.targetId),
|
|
||||||
admins: async.apply(groups.getMembers, 'administrators', 0, -1),
|
|
||||||
globalMods: async.apply(groups.getMembers, 'Global Moderators', 0, -1),
|
|
||||||
moderators: function (next) {
|
|
||||||
var cid;
|
|
||||||
async.waterfall([
|
|
||||||
async.apply(posts.getCidByPid, flagObj.targetId),
|
|
||||||
function (_cid, next) {
|
|
||||||
cid = _cid;
|
|
||||||
groups.getMembers('cid:' + cid + ':privileges:groups:moderate', 0, -1, next);
|
|
||||||
},
|
|
||||||
function (moderatorGroups, next) {
|
|
||||||
groups.getMembersOfGroups(moderatorGroups.concat(['cid:' + cid + ':privileges:moderate']), next);
|
|
||||||
},
|
|
||||||
function (members, next) {
|
|
||||||
next(null, _.flatten(members));
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
},
|
|
||||||
}, function (err, results) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var title = utils.decodeHTMLEntities(results.title);
|
|
||||||
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
|
|
||||||
|
|
||||||
notifications.create({
|
|
||||||
type: 'new-post-flag',
|
|
||||||
bodyShort: '[[notifications:user_flagged_post_in, ' + flagObj.reporter.username + ', ' + titleEscaped + ']]',
|
|
||||||
bodyLong: flagObj.description,
|
|
||||||
pid: flagObj.targetId,
|
|
||||||
path: '/flags/' + flagObj.flagId,
|
|
||||||
nid: 'flag:post:' + flagObj.targetId + ':uid:' + uid,
|
|
||||||
from: uid,
|
|
||||||
mergeId: 'notifications:user_flagged_post_in|' + flagObj.targetId,
|
|
||||||
topicTitle: results.title,
|
|
||||||
}, function (err, notification) {
|
|
||||||
if (err || !notification) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
plugins.fireHook('action:flags.create', {
|
|
||||||
flag: flagObj,
|
|
||||||
});
|
|
||||||
|
|
||||||
var uids = results.admins.concat(results.moderators).concat(results.globalMods);
|
|
||||||
uids = uids.filter(function (_uid) {
|
|
||||||
return parseInt(_uid, 10) !== parseInt(uid, 10);
|
|
||||||
});
|
|
||||||
|
|
||||||
notifications.push(notification, uids, callback);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'user':
|
|
||||||
async.parallel({
|
|
||||||
admins: async.apply(groups.getMembers, 'administrators', 0, -1),
|
|
||||||
globalMods: async.apply(groups.getMembers, 'Global Moderators', 0, -1),
|
|
||||||
}, function (err, results) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
notifications.create({
|
|
||||||
type: 'new-user-flag',
|
|
||||||
bodyShort: '[[notifications:user_flagged_user, ' + flagObj.reporter.username + ', ' + flagObj.target.username + ']]',
|
|
||||||
bodyLong: flagObj.description,
|
|
||||||
path: '/flags/' + flagObj.flagId,
|
|
||||||
nid: 'flag:user:' + flagObj.targetId + ':uid:' + uid,
|
|
||||||
from: uid,
|
|
||||||
mergeId: 'notifications:user_flagged_user|' + flagObj.targetId,
|
|
||||||
}, function (err, notification) {
|
|
||||||
if (err || !notification) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
plugins.fireHook('action:flag.create', {
|
|
||||||
flag: flagObj,
|
|
||||||
}); // delete @ NodeBB v1.6.0
|
|
||||||
plugins.fireHook('action:flags.create', {
|
|
||||||
flag: flagObj,
|
|
||||||
});
|
|
||||||
notifications.push(notification, results.admins.concat(results.globalMods), callback);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
callback(new Error('[[error:invalid-data]]'));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
require('./promisify')(Flags);
|
require('./promisify')(Flags);
|
||||||
|
|||||||
Reference in New Issue
Block a user