Files
NodeBB/src/flags.js

608 lines
17 KiB
JavaScript
Raw Normal View History

2016-11-23 19:41:35 -05:00
'use strict';
2019-09-24 00:30:59 -04:00
const async = require('async');
const _ = require('lodash');
const winston = require('winston');
const validator = require('validator');
const db = require('./database');
const user = require('./user');
const groups = require('./groups');
const meta = require('./meta');
const notifications = require('./notifications');
const analytics = require('./analytics');
const topics = require('./topics');
const posts = require('./posts');
const privileges = require('./privileges');
const plugins = require('./plugins');
const utils = require('../public/src/utils');
const Flags = module.exports;
Flags.init = async function () {
2017-07-04 10:09:37 -04:00
// Query plugins for custom filter strategies and merge into core filter strategies
2019-09-24 00:30:59 -04:00
function prepareSets(sets, orSets, prefix, value) {
2017-07-04 10:09:37 -04:00
if (!Array.isArray(value)) {
sets.push(prefix + value);
} else if (value.length) {
2019-09-24 00:30:59 -04:00
value.forEach(x => orSets.push(prefix + x));
2017-07-04 10:09:37 -04:00
}
2019-09-24 00:30:59 -04:00
}
2017-07-04 10:09:37 -04:00
2019-09-24 00:30:59 -04:00
const hookData = {
2017-07-04 10:09:37 -04:00
filters: {
type: function (sets, orSets, key) {
prepareSets(sets, orSets, 'flags:byType:', key);
},
state: function (sets, orSets, key) {
prepareSets(sets, orSets, 'flags:byState:', key);
},
reporterId: function (sets, orSets, key) {
prepareSets(sets, orSets, 'flags:byReporter:', key);
},
assignee: function (sets, orSets, key) {
prepareSets(sets, orSets, 'flags:byAssignee:', key);
},
targetUid: function (sets, orSets, key) {
prepareSets(sets, orSets, 'flags:byTargetUid:', key);
},
cid: function (sets, orSets, key) {
prepareSets(sets, orSets, 'flags:byCid:', key);
},
2018-03-09 12:57:52 -05:00
page: function () { /* noop */ },
perPage: function () { /* noop */ },
2017-07-04 10:09:37 -04:00
quick: function (sets, orSets, key, uid) {
switch (key) {
case 'mine':
sets.push('flags:byAssignee:' + uid);
break;
}
},
},
helpers: {
prepareSets: prepareSets,
},
2019-09-24 00:30:59 -04:00
};
2017-07-04 10:09:37 -04:00
2019-09-24 00:30:59 -04:00
try {
const data = await plugins.fireHook('filter:flags.getFilters', hookData);
2017-07-04 10:09:37 -04:00
Flags._filters = data.filters;
2019-09-24 00:30:59 -04:00
} catch (err) {
winston.error('[flags/init] Could not retrieve filters', err);
Flags._filters = {};
}
2017-07-04 10:09:37 -04:00
};
2019-09-24 00:30:59 -04:00
Flags.get = async function (flagId) {
const [base, history, notes] = await Promise.all([
db.getObject('flag:' + flagId),
Flags.getHistory(flagId),
Flags.getNotes(flagId),
]);
if (!base) {
return;
}
const [userObj, targetObj] = await Promise.all([
user.getUserFields(base.uid, ['username', 'userslug', 'picture', 'reputation']),
Flags.getTarget(base.type, base.targetId, 0),
]);
const flagObj = {
state: 'open',
...base,
description: validator.escape(base.description),
datetimeISO: utils.toISOString(base.datetime),
target_readable: base.type.charAt(0).toUpperCase() + base.type.slice(1) + ' ' + base.targetId,
target: targetObj,
history: history,
notes: notes,
reporter: userObj,
};
const data = await plugins.fireHook('filter:flags.get', {
flag: flagObj,
});
return data.flag;
};
2019-09-26 22:51:11 -04:00
Flags.list = async function (filters, uid) {
filters = filters || {};
2016-11-25 12:43:10 -05:00
2019-09-26 22:51:11 -04:00
let sets = [];
const orSets = [];
2018-03-09 12:57:52 -05:00
// Default filter
filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1;
filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20;
for (var type in filters) {
if (filters.hasOwnProperty(type)) {
if (Flags._filters.hasOwnProperty(type)) {
Flags._filters[type](sets, orSets, filters[type], uid);
} else {
winston.warn('[flags/list] No flag filter type found: ' + type);
}
}
}
sets = (sets.length || orSets.length) ? sets : ['flags:datetime']; // No filter default
2019-09-26 22:51:11 -04:00
let flagIds = [];
if (sets.length === 1) {
flagIds = await db.getSortedSetRevRange(sets[0], 0, -1);
} else if (sets.length > 1) {
flagIds = await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' });
}
2016-12-06 16:11:56 -05:00
2019-09-26 22:51:11 -04:00
if (orSets.length) {
const _flagIds = await db.getSortedSetRevUnion({ sets: orSets, start: 0, stop: -1, aggregate: 'MAX' });
if (sets.length) {
// If flag ids are already present, return a subset of flags that are in both sets
flagIds = _.intersection(flagIds, _flagIds);
} else {
// Otherwise, return all flags returned via orSets
flagIds = _.union(flagIds, _flagIds);
2016-12-06 16:11:56 -05:00
}
2019-09-26 22:51:11 -04:00
}
2016-12-06 16:11:56 -05:00
2019-09-26 22:51:11 -04:00
// Create subset for parsing based on page number (n=20)
const flagsPerPage = Math.abs(parseInt(filters.perPage, 10) || 1);
const pageCount = Math.ceil(flagIds.length / flagsPerPage);
flagIds = flagIds.slice((filters.page - 1) * flagsPerPage, filters.page * flagsPerPage);
const flags = await Promise.all(flagIds.map(async (flagId) => {
let flagObj = await db.getObject('flag:' + flagId);
const userObj = await user.getUserFields(flagObj.uid, ['username', 'picture']);
flagObj = {
state: 'open',
...flagObj,
reporter: {
username: userObj.username,
picture: userObj.picture,
'icon:bgColor': userObj['icon:bgColor'],
'icon:text': userObj['icon:text'],
},
};
const stateToLabel = {
open: 'info',
wip: 'warning',
resolved: 'success',
rejected: 'danger',
};
flagObj.labelClass = stateToLabel[flagObj.state];
return Object.assign(flagObj, {
description: validator.escape(String(flagObj.description)),
target_readable: flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1) + ' ' + flagObj.targetId,
datetimeISO: utils.toISOString(flagObj.datetime),
});
}));
const data = await plugins.fireHook('filter:flags.list', {
flags: flags,
page: filters.page,
});
2019-09-26 22:51:11 -04:00
return {
flags: data.flags,
page: data.page,
pageCount: pageCount,
};
};
2017-02-24 12:47:46 -05:00
2019-09-26 22:51:11 -04:00
Flags.validate = async function (payload) {
const [target, reporter] = await Promise.all([
Flags.getTarget(payload.type, payload.id, payload.uid),
user.getUserData(payload.uid),
]);
2019-09-26 22:51:11 -04:00
if (!target) {
throw new Error('[[error:invalid-data]]');
} else if (target.deleted) {
throw new Error('[[error:post-deleted]]');
} else if (!reporter || !reporter.userslug) {
throw new Error('[[error:no-user]]');
} else if (reporter.banned) {
throw new Error('[[error:user-banned]]');
}
2017-02-24 12:47:46 -05:00
2019-09-26 22:51:11 -04:00
if (payload.type === 'post') {
const editable = await privileges.posts.canEdit(payload.id, payload.uid);
if (!editable.flag && !meta.config['reputation:disabled'] && reporter.reputation < meta.config['min:rep:flag']) {
throw new Error('[[error:not-enough-reputation-to-flag]]');
2017-02-24 12:47:46 -05:00
}
2019-09-26 22:51:11 -04:00
} else if (payload.type === 'user') {
const editable = await privileges.users.canEdit(payload.uid, payload.id);
if (!editable && !meta.config['reputation:disabled'] && reporter.reputation < meta.config['min:rep:flag']) {
throw new Error('[[error:not-enough-reputation-to-flag]]');
}
} else {
throw new Error('[[error:invalid-data]]');
}
2016-12-06 16:11:56 -05:00
};
2019-09-26 22:51:11 -04:00
Flags.getNotes = async function (flagId) {
let notes = await db.getSortedSetRevRangeWithScores('flag:' + flagId + ':notes', 0, -1);
const uids = [];
notes = notes.map(function (note) {
const noteObj = JSON.parse(note.value);
uids.push(noteObj[0]);
return {
uid: noteObj[0],
content: noteObj[1],
datetime: note.score,
datetimeISO: utils.toISOString(note.score),
};
});
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
return notes.map(function (note, idx) {
note.user = userData[idx];
note.content = validator.escape(note.content);
return note;
});
};
2019-09-26 22:51:11 -04:00
Flags.create = async function (type, id, uid, reason, timestamp) {
let doHistoryAppend = false;
if (!timestamp) {
2016-12-09 14:33:59 -05:00
timestamp = Date.now();
doHistoryAppend = true;
}
2019-09-26 22:51:11 -04:00
const [exists, targetExists, targetUid, targetCid] = await Promise.all([
// Sanity checks
Flags.exists(type, id, uid),
Flags.targetExists(type, id),
// Extra data for zset insertion
Flags.getTargetUid(type, id),
Flags.getTargetCid(type, id),
]);
if (exists) {
throw new Error('[[error:already-flagged]]');
} else if (!targetExists) {
throw new Error('[[error:invalid-data]]');
}
const flagId = await db.incrObjectField('global', 'nextFlagId');
await db.setObject('flag:' + flagId, {
flagId: flagId,
type: type,
targetId: id,
description: reason,
uid: uid,
datetime: timestamp,
});
await db.sortedSetAdd('flags:datetime', timestamp, flagId); // by time, the default
await db.sortedSetAdd('flags:byReporter:' + uid, timestamp, flagId); // by reporter
await db.sortedSetAdd('flags:byType:' + type, timestamp, flagId); // by flag type
await db.sortedSetAdd('flags:hash', flagId, [type, id, uid].join(':')); // save zset for duplicate checking
await analytics.increment('flags'); // some fancy analytics
if (targetUid) {
await db.sortedSetAdd('flags:byTargetUid:' + targetUid, timestamp, flagId); // by target uid
}
2016-12-06 16:11:56 -05:00
2019-09-26 22:51:11 -04:00
if (targetCid) {
await db.sortedSetAdd('flags:byCid:' + targetCid, timestamp, flagId); // by target cid
}
2017-11-12 08:45:08 -05:00
2019-09-26 22:51:11 -04:00
if (type === 'post') {
await db.sortedSetAdd('flags:byPid:' + id, timestamp, flagId); // by target pid
if (targetUid) {
await db.sortedSetIncrBy('users:flags', 1, targetUid);
await user.incrementUserFieldBy(targetUid, 'flags', 1);
}
}
2017-02-24 12:47:46 -05:00
2019-09-26 22:51:11 -04:00
if (doHistoryAppend) {
await Flags.update(flagId, uid, { state: 'open' });
}
2016-12-09 14:33:59 -05:00
2019-09-26 22:51:11 -04:00
return await Flags.get(flagId);
2016-11-23 19:41:35 -05:00
};
2019-09-24 00:30:59 -04:00
Flags.exists = async function (type, id, uid) {
return await db.isSortedSetMember('flags:hash', [type, id, uid].join(':'));
2016-11-23 19:41:35 -05:00
};
2019-09-24 00:30:59 -04:00
Flags.getTarget = async function (type, id, uid) {
if (type === 'user') {
const userData = await user.getUserData(id);
return userData && userData.uid ? userData : {};
}
if (type === 'post') {
let postData = await posts.getPostData(id);
if (!postData) {
return {};
}
postData = await posts.parsePost(postData);
postData = await topics.addPostData([postData], uid);
return postData[0];
}
throw new Error('[[error:invalid-data]]');
};
2019-09-24 00:30:59 -04:00
Flags.targetExists = async function (type, id) {
if (type === 'post') {
return await posts.exists(id);
} else if (type === 'user') {
return await user.exists(id);
2016-11-23 19:41:35 -05:00
}
2019-09-24 00:30:59 -04:00
throw new Error('[[error:invalid-data]]');
2016-11-23 19:41:35 -05:00
};
2019-09-24 00:30:59 -04:00
Flags.getTargetUid = async function (type, id) {
if (type === 'post') {
return await posts.getPostField(id, 'uid');
2016-12-06 16:11:56 -05:00
}
2019-09-24 00:30:59 -04:00
return id;
2016-12-06 16:11:56 -05:00
};
2019-09-24 00:30:59 -04:00
Flags.getTargetCid = async function (type, id) {
if (type === 'post') {
return await posts.getCidByPid(id);
}
return null;
};
Flags.update = function (flagId, uid, changeset, callback) {
2016-11-23 19:41:35 -05:00
// Retrieve existing flag data to compare for history-saving purposes
var fields = ['state', 'assignee'];
2016-12-05 12:40:25 -05:00
var tasks = [];
2016-12-09 14:33:59 -05:00
var now = changeset.datetime || Date.now();
var notifyAssignee = function (assigneeId, next) {
if (assigneeId === '' || parseInt(uid, 10) === parseInt(assigneeId, 10)) {
// Do nothing
return next();
}
// Notify assignee of this update
notifications.create({
type: 'my-flags',
bodyShort: '[[notifications:flag_assigned_to_you, ' + flagId + ']]',
bodyLong: '',
path: '/flags/' + flagId,
nid: 'flags:assign:' + flagId + ':uid:' + assigneeId,
from: uid,
}, function (err, notification) {
if (err || !notification) {
return next(err);
}
notifications.push(notification, [assigneeId], next);
});
};
2016-11-23 19:41:35 -05:00
async.waterfall([
async.apply(db.getObjectFields.bind(db), 'flag:' + flagId, fields),
function (current, next) {
for (var prop in changeset) {
if (changeset.hasOwnProperty(prop)) {
if (current[prop] === changeset[prop]) {
delete changeset[prop];
} else {
2016-12-05 12:40:25 -05:00
// Add tasks as necessary
switch (prop) {
2017-02-24 12:47:46 -05:00
case 'state':
tasks.push(async.apply(db.sortedSetAdd.bind(db), 'flags:byState:' + changeset[prop], now, flagId));
tasks.push(async.apply(db.sortedSetRemove.bind(db), 'flags:byState:' + current[prop], flagId));
break;
case 'assignee':
tasks.push(async.apply(db.sortedSetAdd.bind(db), 'flags:byAssignee:' + changeset[prop], now, flagId));
tasks.push(async.apply(notifyAssignee, changeset[prop]));
2017-02-24 12:47:46 -05:00
break;
2016-12-05 12:40:25 -05:00
}
2016-11-23 19:41:35 -05:00
}
}
}
2016-11-23 19:41:35 -05:00
if (!Object.keys(changeset).length) {
// No changes
return next();
2016-11-23 19:41:35 -05:00
}
2016-12-05 12:40:25 -05:00
// Save new object to db (upsert)
tasks.push(async.apply(db.setObject, 'flag:' + flagId, changeset));
2017-01-12 10:03:45 -05:00
2016-12-05 12:40:25 -05:00
// Append history
tasks.push(async.apply(Flags.appendHistory, flagId, uid, changeset));
2016-12-05 12:40:25 -05:00
2017-01-12 10:03:45 -05:00
// Fire plugin hook
tasks.push(async.apply(plugins.fireHook, 'action:flags.update', { flagId: flagId, changeset: changeset, uid: uid }));
2017-01-12 10:03:45 -05:00
2017-02-24 12:55:07 -05:00
async.parallel(tasks, function (err) {
return next(err);
});
2017-02-24 12:47:46 -05:00
},
], callback);
2016-11-23 19:41:35 -05:00
};
Flags.getHistory = function (flagId, callback) {
var history;
var uids = [];
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]);
// Deserialise changeset
var changeset = entry.value[1];
if (changeset.hasOwnProperty('state')) {
changeset.state = changeset.state === undefined ? '' : '[[flags:state-' + changeset.state + ']]';
}
return {
uid: entry.value[0],
fields: changeset,
datetime: entry.score,
2018-03-15 12:43:11 -04:00
datetimeISO: utils.toISOString(entry.score),
};
});
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
2017-02-24 12:47:46 -05:00
},
2017-05-26 23:30:23 -04:00
function (users, next) {
// Append user data to each history event
history = history.map(function (event, idx) {
event.user = users[idx];
return event;
});
2017-05-26 23:30:23 -04:00
next(null, history);
},
], callback);
};
Flags.appendHistory = function (flagId, uid, changeset, callback) {
var payload;
2016-12-09 14:33:59 -05:00
var datetime = changeset.datetime || Date.now();
delete changeset.datetime;
try {
2016-12-09 14:33:59 -05:00
payload = JSON.stringify([uid, changeset, datetime]);
} catch (e) {
return callback(e);
}
2016-12-09 14:33:59 -05:00
db.sortedSetAdd('flag:' + flagId + ':history', datetime, payload, callback);
};
2016-11-23 19:41:35 -05:00
2016-12-09 14:33:59 -05:00
Flags.appendNote = function (flagId, uid, note, datetime, callback) {
if (typeof datetime === 'function' && !callback) {
callback = datetime;
datetime = Date.now();
}
var payload;
try {
payload = JSON.stringify([uid, note]);
} catch (e) {
return callback(e);
}
2016-11-23 19:41:35 -05:00
async.waterfall([
2016-12-09 14:33:59 -05:00
async.apply(db.sortedSetAdd, 'flag:' + flagId + ':notes', datetime, payload),
async.apply(Flags.appendHistory, flagId, uid, {
2016-12-09 14:33:59 -05:00
notes: null,
2017-02-24 12:47:46 -05:00
datetime: datetime,
}),
], callback);
2016-11-23 19:41:35 -05:00
};
2016-12-06 16:11:56 -05:00
Flags.notify = function (flagObj, uid, callback) {
// Notify administrators, mods, and other associated people
if (!callback) {
callback = function () {};
}
2016-12-06 16:11:56 -05:00
switch (flagObj.type) {
2017-02-24 12:47:46 -05:00
case 'post':
async.parallel({
post: function (next) {
async.waterfall([
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) {
2018-02-07 20:02:07 -05:00
var cid;
2017-02-24 12:47:46 -05:00
async.waterfall([
async.apply(posts.getCidByPid, flagObj.targetId),
2018-02-07 20:02:07 -05:00
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);
2018-02-07 18:29:56 -05:00
},
function (members, next) {
next(null, _.flatten(members));
2017-02-24 12:47:46 -05:00
},
], next);
},
}, function (err, results) {
if (err) {
return callback(err);
}
2017-10-13 21:02:41 -06:00
var title = utils.decodeHTMLEntities(results.title);
2017-02-24 12:47:46 -05:00
var titleEscaped = title.replace(/%/g, '&#37;').replace(/,/g, '&#44;');
notifications.create({
2017-03-14 23:03:03 +03:00
type: 'new-post-flag',
2017-02-24 12:47:46 -05:00
bodyShort: '[[notifications:user_flagged_post_in, ' + flagObj.reporter.username + ', ' + titleEscaped + ']]',
bodyLong: flagObj.description,
pid: flagObj.targetId,
path: '/flags/' + flagObj.flagId,
2017-02-24 12:47:46 -05:00
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) {
2016-12-06 16:11:56 -05:00
return callback(err);
}
plugins.fireHook('action:flags.create', {
flag: flagObj,
2016-12-06 16:11:56 -05:00
});
2018-04-11 13:18:52 -04:00
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);
2016-12-06 16:11:56 -05:00
});
2017-02-24 12:47:46 -05:00
});
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);
}
2017-02-24 12:47:46 -05:00
notifications.create({
type: 'new-user-flag',
2017-02-24 12:47:46 -05:00
bodyShort: '[[notifications:user_flagged_user, ' + flagObj.reporter.username + ', ' + flagObj.target.username + ']]',
bodyLong: flagObj.description,
path: '/flags/' + flagObj.flagId,
2017-02-24 12:47:46 -05:00
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);
}
2017-02-24 12:47:46 -05:00
plugins.fireHook('action:flag.create', {
flag: flagObj,
}); // delete @ NodeBB v1.6.0
plugins.fireHook('action:flags.create', {
flag: flagObj,
});
2017-02-24 12:47:46 -05:00
notifications.push(notification, results.admins.concat(results.globalMods), callback);
});
2017-02-24 12:47:46 -05:00
});
break;
default:
callback(new Error('[[error:invalid-data]]'));
break;
2016-12-06 16:11:56 -05:00
}
};
2019-07-22 18:16:18 -04:00
require('./promisify')(Flags);