feat: resolve flag on delete/purge/ban/delete account

v1.18.x
Barış Soner Uşaklı 5 years ago
parent c54b6b9c7e
commit 8bd63f61e0

@ -381,10 +381,10 @@ Flags.getReports = async function (flagId) {
Flags.addReport = async function (flagId, uid, reason, timestamp) { Flags.addReport = async function (flagId, uid, reason, timestamp) {
// adds to reporters/report zsets // adds to reporters/report zsets
await Promise.all([ await db.sortedSetAddBulk([
db.sortedSetAdd(`flags:byReporter:${uid}`, timestamp, flagId), [`flags:byReporter:${uid}`, timestamp, flagId],
db.sortedSetAdd(`flag:${flagId}:reports`, timestamp, reason), [`flag:${flagId}:reports`, timestamp, reason],
db.sortedSetAdd(`flag:${flagId}:reporters`, timestamp, uid), [`flag:${flagId}:reporters`, timestamp, uid],
]); ]);
}; };
@ -463,6 +463,9 @@ Flags.getTargetCid = async function (type, id) {
Flags.update = async function (flagId, uid, changeset) { Flags.update = async function (flagId, uid, changeset) {
const current = await db.getObjectFields('flag:' + flagId, ['uid', 'state', 'assignee', 'type', 'targetId']); const current = await db.getObjectFields('flag:' + flagId, ['uid', 'state', 'assignee', 'type', 'targetId']);
if (!current.type) {
return;
}
const now = changeset.datetime || Date.now(); const now = changeset.datetime || Date.now();
const notifyAssignee = async function (assigneeId) { const notifyAssignee = async function (assigneeId) {
if (assigneeId === '' || parseInt(uid, 10) === parseInt(assigneeId, 10)) { if (assigneeId === '' || parseInt(uid, 10) === parseInt(assigneeId, 10)) {
@ -525,8 +528,16 @@ Flags.update = async function (flagId, uid, changeset) {
tasks.push(db.setObject('flag:' + flagId, changeset)); tasks.push(db.setObject('flag:' + flagId, changeset));
tasks.push(Flags.appendHistory(flagId, uid, changeset)); tasks.push(Flags.appendHistory(flagId, uid, changeset));
tasks.push(plugins.fireHook('action:flags.update', { flagId: flagId, changeset: changeset, uid: uid }));
await Promise.all(tasks); await Promise.all(tasks);
plugins.fireHook('action:flags.update', { flagId: flagId, changeset: changeset, uid: uid });
};
Flags.resolveFlag = async function (type, id, uid) {
const flagId = await Flags.getFlagIdByTarget(type, id);
if (parseInt(flagId, 10)) {
await Flags.update(flagId, uid, { state: 'resolved' });
}
}; };
Flags.getHistory = async function (flagId) { Flags.getHistory = async function (flagId) {

@ -9,6 +9,7 @@ const user = require('../user');
const groups = require('../groups'); const groups = require('../groups');
const notifications = require('../notifications'); const notifications = require('../notifications');
const plugins = require('../plugins'); const plugins = require('../plugins');
const flags = require('../flags');
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.delete = async function (pid, uid) { Posts.delete = async function (pid, uid) {
@ -38,6 +39,9 @@ module.exports = function (Posts) {
]); ]);
await categories.updateRecentTidForCid(postData.cid); await categories.updateRecentTidForCid(postData.cid);
plugins.fireHook('action:post.' + type, { post: _.clone(postData), uid: uid }); plugins.fireHook('action:post.' + type, { post: _.clone(postData), uid: uid });
if (type === 'delete') {
await flags.resolveFlag('post', pid, uid);
}
return postData; return postData;
} }
@ -59,6 +63,7 @@ module.exports = function (Posts) {
db.sortedSetsRemove(['posts:pid', 'posts:votes', 'posts:flagged'], pid), db.sortedSetsRemove(['posts:pid', 'posts:votes', 'posts:flagged'], pid),
Posts.uploads.dissociateAll(pid), Posts.uploads.dissociateAll(pid),
]); ]);
await flags.resolveFlag('post', pid, uid);
plugins.fireHook('action:post.purge', { post: postData, uid: uid }); plugins.fireHook('action:post.purge', { post: postData, uid: uid });
await db.delete('post:' + pid); await db.delete('post:' + pid);
}; };

@ -10,6 +10,7 @@ const events = require('../../events');
const meta = require('../../meta'); const meta = require('../../meta');
const plugins = require('../../plugins'); const plugins = require('../../plugins');
const translator = require('../../translator'); const translator = require('../../translator');
const flags = require('../../flags');
const User = module.exports; const User = module.exports;
@ -155,6 +156,7 @@ async function deleteUsers(socket, uids, method) {
throw new Error('[[error:cant-delete-other-admins]]'); throw new Error('[[error:cant-delete-other-admins]]');
} }
async function doDelete(uid) { async function doDelete(uid) {
await flags.resolveFlag('user', uid, socket.uid);
const userData = await method(uid); const userData = await method(uid);
await events.log({ await events.log({
type: 'user-delete', type: 'user-delete',

@ -17,6 +17,7 @@ const db = require('../database');
const userController = require('../controllers/user'); const userController = require('../controllers/user');
const privileges = require('../privileges'); const privileges = require('../privileges');
const utils = require('../utils'); const utils = require('../utils');
const flags = require('../flags');
const SocketUser = module.exports; const SocketUser = module.exports;
@ -52,7 +53,10 @@ SocketUser.deleteAccount = async function (socket, data) {
if (meta.config.allowAccountDelete !== 1) { if (meta.config.allowAccountDelete !== 1) {
throw new Error('[[error:no-privileges]]'); throw new Error('[[error:no-privileges]]');
} }
await flags.resolveFlag('user', socket.uid, socket.uid);
const userData = await user.deleteAccount(socket.uid); const userData = await user.deleteAccount(socket.uid);
require('./index').server.sockets.emit('event:user_status_change', { uid: socket.uid, status: 'offline' }); require('./index').server.sockets.emit('event:user_status_change', { uid: socket.uid, status: 'offline' });
await events.log({ await events.log({

@ -12,6 +12,7 @@ const plugins = require('../../plugins');
const emailer = require('../../emailer'); const emailer = require('../../emailer');
const translator = require('../../translator'); const translator = require('../../translator');
const utils = require('../../../public/src/utils'); const utils = require('../../../public/src/utils');
const flags = require('../../flags');
module.exports = function (SocketUser) { module.exports = function (SocketUser) {
SocketUser.banUsers = async function (socket, data) { SocketUser.banUsers = async function (socket, data) {
@ -21,6 +22,7 @@ module.exports = function (SocketUser) {
await toggleBan(socket.uid, data.uids, async function (uid) { await toggleBan(socket.uid, data.uids, async function (uid) {
await banUser(socket.uid, uid, data.until || 0, data.reason || ''); await banUser(socket.uid, uid, data.until || 0, data.reason || '');
await flags.resolveFlag('user', uid, socket.uid);
await events.log({ await events.log({
type: 'user-ban', type: 'user-ban',
uid: socket.uid, uid: socket.uid,

@ -63,11 +63,13 @@ module.exports = function (User) {
} }
async function deleteQueued(uid) { async function deleteQueued(uid) {
let deleteIds = [];
await batch.processSortedSet('post:queue', async function (ids) { await batch.processSortedSet('post:queue', async function (ids) {
const data = await db.getObjects(ids.map(id => 'post:queue:' + id)); const data = await db.getObjects(ids.map(id => 'post:queue:' + id));
const deleteIds = data.filter(d => parseInt(d.uid, 10) === parseInt(uid, 10)).map(d => d.id); const userQueuedIds = data.filter(d => parseInt(d.uid, 10) === parseInt(uid, 10)).map(d => d.id);
await async.eachSeries(deleteIds, posts.removeFromQueue); deleteIds = deleteIds.concat(userQueuedIds);
}, { alwaysStartAt: 0 }); }, { batch: 500 });
await async.eachSeries(deleteIds, posts.removeFromQueue);
} }
async function removeFromSortedSets(uid) { async function removeFromSortedSets(uid) {

Loading…
Cancel
Save