2014-03-15 15:09:54 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-10-03 23:31:42 -04:00
|
|
|
const async = require('async');
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const nconf = require('nconf');
|
2016-10-08 19:09:48 +03:00
|
|
|
|
2019-10-03 23:31:42 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const posts = require('../posts');
|
|
|
|
|
const topics = require('../topics');
|
|
|
|
|
const groups = require('../groups');
|
|
|
|
|
const messaging = require('../messaging');
|
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const batch = require('../batch');
|
|
|
|
|
const file = require('../file');
|
2014-03-15 15:09:54 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (User) {
|
2019-10-03 23:31:42 -04:00
|
|
|
const deletesInProgress = {};
|
2018-10-03 19:12:46 -04:00
|
|
|
|
2020-06-08 08:43:25 -04:00
|
|
|
User.delete = async (callerUid, uid) => {
|
|
|
|
|
await User.deleteContent(callerUid, uid);
|
|
|
|
|
await removeFromSortedSets(uid);
|
|
|
|
|
return await User.deleteAccount(uid);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.deleteContent = async function (callerUid, uid) {
|
2018-11-12 00:20:44 -05:00
|
|
|
if (parseInt(uid, 10) <= 0) {
|
2019-07-14 00:22:17 -04:00
|
|
|
throw new Error('[[error:invalid-uid]]');
|
2014-12-29 15:11:52 -05:00
|
|
|
}
|
2018-10-03 19:12:46 -04:00
|
|
|
if (deletesInProgress[uid]) {
|
2019-07-14 00:22:17 -04:00
|
|
|
throw new Error('[[error:already-deleting]]');
|
2018-10-03 19:12:46 -04:00
|
|
|
}
|
|
|
|
|
deletesInProgress[uid] = 'user.delete';
|
2019-07-14 00:22:17 -04:00
|
|
|
await deletePosts(callerUid, uid);
|
|
|
|
|
await deleteTopics(callerUid, uid);
|
|
|
|
|
await deleteUploads(uid);
|
2020-06-01 22:29:56 -04:00
|
|
|
await deleteQueued(uid);
|
2014-03-15 15:09:54 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function deletePosts(callerUid, uid) {
|
|
|
|
|
await batch.processSortedSet('uid:' + uid + ':posts', async function (ids) {
|
|
|
|
|
await async.eachSeries(ids, async function (pid) {
|
|
|
|
|
await posts.purge(pid, callerUid);
|
|
|
|
|
});
|
|
|
|
|
}, { alwaysStartAt: 0 });
|
2014-03-15 15:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function deleteTopics(callerUid, uid) {
|
|
|
|
|
await batch.processSortedSet('uid:' + uid + ':topics', async function (ids) {
|
|
|
|
|
await async.eachSeries(ids, async function (tid) {
|
|
|
|
|
await topics.purge(tid, callerUid);
|
|
|
|
|
});
|
|
|
|
|
}, { alwaysStartAt: 0 });
|
2014-03-15 15:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function deleteUploads(uid) {
|
|
|
|
|
await batch.processSortedSet('uid:' + uid + ':uploads', async function (uploadNames) {
|
|
|
|
|
await async.each(uploadNames, async function (uploadName) {
|
|
|
|
|
await file.delete(path.join(nconf.get('upload_path'), uploadName));
|
|
|
|
|
});
|
|
|
|
|
await db.sortedSetRemove('uid:' + uid + ':uploads', uploadNames);
|
|
|
|
|
}, { alwaysStartAt: 0 });
|
2018-04-06 15:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
2020-06-01 22:29:56 -04:00
|
|
|
async function deleteQueued(uid) {
|
2020-07-25 18:26:09 -04:00
|
|
|
let deleteIds = [];
|
2020-06-01 22:29:56 -04:00
|
|
|
await batch.processSortedSet('post:queue', async function (ids) {
|
|
|
|
|
const data = await db.getObjects(ids.map(id => 'post:queue:' + id));
|
2020-07-25 18:26:09 -04:00
|
|
|
const userQueuedIds = data.filter(d => parseInt(d.uid, 10) === parseInt(uid, 10)).map(d => d.id);
|
|
|
|
|
deleteIds = deleteIds.concat(userQueuedIds);
|
|
|
|
|
}, { batch: 500 });
|
|
|
|
|
await async.eachSeries(deleteIds, posts.removeFromQueue);
|
2020-06-01 22:29:56 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function removeFromSortedSets(uid) {
|
|
|
|
|
await db.sortedSetsRemove([
|
2018-10-03 19:12:46 -04:00
|
|
|
'users:joindate',
|
|
|
|
|
'users:postcount',
|
|
|
|
|
'users:reputation',
|
|
|
|
|
'users:banned',
|
|
|
|
|
'users:banned:expire',
|
|
|
|
|
'users:flags',
|
|
|
|
|
'users:online',
|
|
|
|
|
'users:notvalidated',
|
|
|
|
|
'digest:day:uids',
|
|
|
|
|
'digest:week:uids',
|
|
|
|
|
'digest:month:uids',
|
2019-07-14 00:22:17 -04:00
|
|
|
], uid);
|
2018-10-03 19:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
User.deleteAccount = async function (uid) {
|
2018-10-03 19:12:46 -04:00
|
|
|
if (deletesInProgress[uid] === 'user.deleteAccount') {
|
2019-07-14 00:22:17 -04:00
|
|
|
throw new Error('[[error:already-deleting]]');
|
2018-10-03 19:12:46 -04:00
|
|
|
}
|
|
|
|
|
deletesInProgress[uid] = 'user.deleteAccount';
|
2019-07-14 00:22:17 -04:00
|
|
|
|
|
|
|
|
await removeFromSortedSets(uid);
|
|
|
|
|
const userData = await db.getObject('user:' + uid);
|
|
|
|
|
|
|
|
|
|
if (!userData || !userData.username) {
|
2018-10-03 19:12:46 -04:00
|
|
|
delete deletesInProgress[uid];
|
2019-07-14 00:22:17 -04:00
|
|
|
throw new Error('[[error:no-user]]');
|
|
|
|
|
}
|
2015-08-25 17:48:18 -04:00
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
await plugins.fireHook('static:user.delete', { uid: uid });
|
|
|
|
|
await deleteVotes(uid);
|
|
|
|
|
await deleteChats(uid);
|
|
|
|
|
await User.auth.revokeAllSessions(uid);
|
|
|
|
|
|
|
|
|
|
const keys = [
|
|
|
|
|
'uid:' + uid + ':notifications:read',
|
|
|
|
|
'uid:' + uid + ':notifications:unread',
|
|
|
|
|
'uid:' + uid + ':bookmarks',
|
|
|
|
|
'uid:' + uid + ':followed_tids',
|
|
|
|
|
'uid:' + uid + ':ignored_tids',
|
|
|
|
|
'user:' + uid + ':settings',
|
|
|
|
|
'user:' + uid + ':usernames',
|
|
|
|
|
'user:' + uid + ':emails',
|
|
|
|
|
'uid:' + uid + ':topics', 'uid:' + uid + ':posts',
|
|
|
|
|
'uid:' + uid + ':chats', 'uid:' + uid + ':chats:unread',
|
|
|
|
|
'uid:' + uid + ':chat:rooms', 'uid:' + uid + ':chat:rooms:unread',
|
|
|
|
|
'uid:' + uid + ':upvote', 'uid:' + uid + ':downvote',
|
|
|
|
|
'uid:' + uid + ':flag:pids',
|
|
|
|
|
'uid:' + uid + ':sessions', 'uid:' + uid + ':sessionUUID:sessionId',
|
|
|
|
|
'invitation:uid:' + uid,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const bulkRemove = [
|
|
|
|
|
['username:uid', userData.username],
|
|
|
|
|
['username:sorted', userData.username.toLowerCase() + ':' + uid],
|
|
|
|
|
['userslug:uid', userData.userslug],
|
|
|
|
|
['fullname:uid', userData.fullname],
|
|
|
|
|
];
|
|
|
|
|
if (userData.email) {
|
|
|
|
|
bulkRemove.push(['email:uid', userData.email.toLowerCase()]);
|
|
|
|
|
bulkRemove.push(['email:sorted', userData.email.toLowerCase() + ':' + uid]);
|
|
|
|
|
}
|
2015-11-11 00:00:02 -05:00
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
await Promise.all([
|
|
|
|
|
db.sortedSetRemoveBulk(bulkRemove),
|
|
|
|
|
db.decrObjectField('global', 'userCount'),
|
|
|
|
|
db.deleteAll(keys),
|
|
|
|
|
db.setRemove('invitation:uids', uid),
|
|
|
|
|
deleteUserIps(uid),
|
|
|
|
|
deleteBans(uid),
|
|
|
|
|
deleteUserFromFollowers(uid),
|
|
|
|
|
groups.leaveAllGroups(uid),
|
|
|
|
|
]);
|
|
|
|
|
await db.deleteAll(['followers:' + uid, 'following:' + uid, 'user:' + uid]);
|
|
|
|
|
delete deletesInProgress[uid];
|
|
|
|
|
return userData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function deleteVotes(uid) {
|
|
|
|
|
const [upvotedPids, downvotedPids] = await Promise.all([
|
|
|
|
|
db.getSortedSetRange('uid:' + uid + ':upvote', 0, -1),
|
|
|
|
|
db.getSortedSetRange('uid:' + uid + ':downvote', 0, -1),
|
|
|
|
|
]);
|
|
|
|
|
const pids = _.uniq(upvotedPids.concat(downvotedPids).filter(Boolean));
|
|
|
|
|
await async.eachSeries(pids, async function (pid) {
|
|
|
|
|
await posts.unvote(pid, uid);
|
2014-03-15 15:09:54 -04:00
|
|
|
});
|
2015-11-11 00:00:02 -05:00
|
|
|
}
|
2014-03-15 15:09:54 -04:00
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function deleteChats(uid) {
|
|
|
|
|
const roomIds = await db.getSortedSetRange('uid:' + uid + ':chat:rooms', 0, -1);
|
|
|
|
|
const userKeys = roomIds.map(roomId => 'uid:' + uid + ':chat:room:' + roomId + ':mids');
|
2015-12-16 09:30:55 +02:00
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
await Promise.all([
|
|
|
|
|
messaging.leaveRooms(uid, roomIds),
|
|
|
|
|
db.deleteAll(userKeys),
|
|
|
|
|
]);
|
2015-12-16 09:30:55 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function deleteUserIps(uid) {
|
|
|
|
|
const ips = await db.getSortedSetRange('uid:' + uid + ':ip', 0, -1);
|
|
|
|
|
await db.sortedSetsRemove(ips.map(ip => 'ip:' + ip + ':uid'), uid);
|
|
|
|
|
await db.delete('uid:' + uid + ':ip');
|
2015-01-17 16:22:04 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function deleteBans(uid) {
|
|
|
|
|
const bans = await db.getSortedSetRange('uid:' + uid + ':bans:timestamp', 0, -1);
|
|
|
|
|
await db.deleteAll(bans);
|
|
|
|
|
await db.delete('uid:' + uid + ':bans:timestamp');
|
2018-09-24 20:20:52 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
async function deleteUserFromFollowers(uid) {
|
|
|
|
|
const [followers, following] = await Promise.all([
|
|
|
|
|
db.getSortedSetRange('followers:' + uid, 0, -1),
|
|
|
|
|
db.getSortedSetRange('following:' + uid, 0, -1),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
async function updateCount(uids, name, fieldName) {
|
|
|
|
|
await async.each(uids, async function (uid) {
|
|
|
|
|
let count = await db.sortedSetCard(name + uid);
|
|
|
|
|
count = parseInt(count, 10) || 0;
|
|
|
|
|
await db.setObjectField('user:' + uid, fieldName, count);
|
2014-09-16 22:25:12 -04:00
|
|
|
});
|
2019-07-14 00:22:17 -04:00
|
|
|
}
|
2014-09-16 22:25:12 -04:00
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
const followingSets = followers.map(uid => 'following:' + uid);
|
|
|
|
|
const followerSets = following.map(uid => 'followers:' + uid);
|
2015-04-09 15:37:20 -04:00
|
|
|
|
2019-07-14 00:22:17 -04:00
|
|
|
await Promise.all([
|
|
|
|
|
db.sortedSetsRemove(followerSets.concat(followingSets), uid),
|
|
|
|
|
updateCount(following, 'followers:', 'followerCount'),
|
|
|
|
|
updateCount(followers, 'following:', 'followingCount'),
|
|
|
|
|
]);
|
2014-03-15 15:09:54 -04:00
|
|
|
}
|
2014-03-23 23:34:04 -04:00
|
|
|
};
|