|
|
@ -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) {
|
|
|
|
|
|
|
|
// No changes
|
|
|
|
|
|
|
|
return next();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Save new object to db (upsert)
|
|
|
|
if (!Object.keys(changeset).length) {
|
|
|
|
tasks.push(async.apply(db.setObject, 'flag:' + flagId, changeset));
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
// Append history
|
|
|
|
|
|
|
|
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) {
|
|
|
|
tasks.push(db.setObject('flag:' + flagId, changeset));
|
|
|
|
return next(err);
|
|
|
|
tasks.push(Flags.appendHistory(flagId, uid, changeset));
|
|
|
|
});
|
|
|
|
tasks.push(plugins.fireHook('action:flags.update', { flagId: flagId, changeset: changeset, uid: uid }));
|
|
|
|
},
|
|
|
|
await Promise.all(tasks);
|
|
|
|
], 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);
|
|
|
|
switch (flagObj.type) {
|
|
|
|
let notifObj = null;
|
|
|
|
case 'post':
|
|
|
|
if (flagObj.type === 'post') {
|
|
|
|
async.parallel({
|
|
|
|
const [title, cid] = await Promise.all([
|
|
|
|
post: function (next) {
|
|
|
|
topics.getTitleByPid(flagObj.targetId),
|
|
|
|
async.waterfall([
|
|
|
|
posts.getCidByPid(flagObj.targetId),
|
|
|
|
async.apply(posts.getPostData, flagObj.targetId),
|
|
|
|
]);
|
|
|
|
async.apply(posts.parsePost),
|
|
|
|
|
|
|
|
], next);
|
|
|
|
const modUids = await categories.getModeratorUids([cid]);
|
|
|
|
},
|
|
|
|
const titleEscaped = utils.decodeHTMLEntities(title).replace(/%/g, '%').replace(/,/g, ',');
|
|
|
|
title: async.apply(topics.getTitleByPid, flagObj.targetId),
|
|
|
|
|
|
|
|
admins: async.apply(groups.getMembers, 'administrators', 0, -1),
|
|
|
|
notifObj = await notifications.create({
|
|
|
|
globalMods: async.apply(groups.getMembers, 'Global Moderators', 0, -1),
|
|
|
|
type: 'new-post-flag',
|
|
|
|
moderators: function (next) {
|
|
|
|
bodyShort: '[[notifications:user_flagged_post_in, ' + flagObj.reporter.username + ', ' + titleEscaped + ']]',
|
|
|
|
var cid;
|
|
|
|
bodyLong: flagObj.description,
|
|
|
|
async.waterfall([
|
|
|
|
pid: flagObj.targetId,
|
|
|
|
async.apply(posts.getCidByPid, flagObj.targetId),
|
|
|
|
path: '/flags/' + flagObj.flagId,
|
|
|
|
function (_cid, next) {
|
|
|
|
nid: 'flag:post:' + flagObj.targetId + ':uid:' + uid,
|
|
|
|
cid = _cid;
|
|
|
|
from: uid,
|
|
|
|
groups.getMembers('cid:' + cid + ':privileges:groups:moderate', 0, -1, next);
|
|
|
|
mergeId: 'notifications:user_flagged_post_in|' + flagObj.targetId,
|
|
|
|
},
|
|
|
|
topicTitle: title,
|
|
|
|
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;
|
|
|
|
uids = uids.concat(modUids[0]);
|
|
|
|
|
|
|
|
} else if (flagObj.type === 'user') {
|
|
|
|
case 'user':
|
|
|
|
notifObj = await notifications.create({
|
|
|
|
async.parallel({
|
|
|
|
type: 'new-user-flag',
|
|
|
|
admins: async.apply(groups.getMembers, 'administrators', 0, -1),
|
|
|
|
bodyShort: '[[notifications:user_flagged_user, ' + flagObj.reporter.username + ', ' + flagObj.target.username + ']]',
|
|
|
|
globalMods: async.apply(groups.getMembers, 'Global Moderators', 0, -1),
|
|
|
|
bodyLong: flagObj.description,
|
|
|
|
}, function (err, results) {
|
|
|
|
path: '/flags/' + flagObj.flagId,
|
|
|
|
if (err) {
|
|
|
|
nid: 'flag:user:' + flagObj.targetId + ':uid:' + uid,
|
|
|
|
return callback(err);
|
|
|
|
from: uid,
|
|
|
|
}
|
|
|
|
mergeId: 'notifications:user_flagged_user|' + flagObj.targetId,
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
default:
|
|
|
|
|
|
|
|
callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('action:flags.create', {
|
|
|
|
|
|
|
|
flag: flagObj,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
uids = uids.filter(_uid => parseInt(_uid, 10) !== parseInt(uid, 10));
|
|
|
|
|
|
|
|
await notifications.push(notifObj, uids);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
require('./promisify')(Flags);
|
|
|
|
require('./promisify')(Flags);
|
|
|
|