Files
NodeBB/src/user/delete.js

245 lines
6.7 KiB
JavaScript
Raw Normal View History

'use strict';
2016-10-08 19:09:48 +03:00
var async = require('async');
var db = require('../database');
var posts = require('../posts');
var topics = require('../topics');
var groups = require('../groups');
var plugins = require('../plugins');
var batch = require('../batch');
module.exports = function (User) {
User.delete = function (callerUid, uid, callback) {
2014-12-29 15:11:52 -05:00
if (!parseInt(uid, 10)) {
return callback(new Error('[[error:invalid-uid]]'));
}
2016-11-23 17:15:31 +03:00
async.waterfall([
function (next) {
2016-03-12 17:29:33 +02:00
deletePosts(callerUid, uid, next);
},
function (next) {
2016-03-12 17:29:33 +02:00
deleteTopics(callerUid, uid, next);
2014-08-08 17:42:03 -04:00
},
function (next) {
2014-08-26 13:47:48 -04:00
User.deleteAccount(uid, next);
2017-02-17 19:31:21 -07:00
},
2014-08-08 17:42:03 -04:00
], callback);
};
2016-03-12 17:29:33 +02:00
function deletePosts(callerUid, uid, callback) {
batch.processSortedSet('uid:' + uid + ':posts', function (ids, next) {
async.eachSeries(ids, function (pid, next) {
2016-03-12 17:29:33 +02:00
posts.purge(pid, callerUid, next);
}, next);
}, {alwaysStartAt: 0}, callback);
}
2016-03-12 17:29:33 +02:00
function deleteTopics(callerUid, uid, callback) {
batch.processSortedSet('uid:' + uid + ':topics', function (ids, next) {
async.eachSeries(ids, function (tid, next) {
2016-03-12 17:29:33 +02:00
topics.purge(tid, callerUid, next);
}, next);
2014-09-16 22:25:12 -04:00
}, {alwaysStartAt: 0}, callback);
}
User.deleteAccount = function (uid, callback) {
2015-11-11 00:00:02 -05:00
var userData;
async.waterfall([
function (next) {
2016-11-23 17:15:31 +03:00
User.exists(uid, next);
},
function (exists, next) {
if (!exists) {
return callback();
}
2015-11-11 00:00:02 -05:00
User.getUserFields(uid, ['username', 'userslug', 'fullname', 'email'], next);
},
function (_userData, next) {
2015-11-11 00:00:02 -05:00
userData = _userData;
plugins.fireHook('static:user.delete', {uid: uid}, next);
},
function (next) {
deleteVotes(uid, next);
},
2015-12-16 09:30:55 +02:00
function (next) {
deleteChats(uid, next);
},
2015-12-24 12:20:27 +02:00
function (next) {
User.auth.revokeAllSessions(uid, next);
},
2015-11-11 00:00:02 -05:00
function (next) {
async.parallel([
function (next) {
db.sortedSetRemove('username:uid', userData.username, next);
},
function (next) {
db.sortedSetRemove('username:sorted', userData.username.toLowerCase() + ':' + uid, next);
},
function (next) {
db.sortedSetRemove('userslug:uid', userData.userslug, next);
},
function (next) {
db.sortedSetRemove('fullname:uid', userData.fullname, next);
},
function (next) {
if (userData.email) {
async.parallel([
async.apply(db.sortedSetRemove, 'email:uid', userData.email.toLowerCase()),
2017-02-17 19:31:21 -07:00
async.apply(db.sortedSetRemove, 'email:sorted', userData.email.toLowerCase() + ':' + uid),
], next);
} else {
next();
}
},
function (next) {
db.sortedSetsRemove([
'users:joindate',
'users:postcount',
'users:reputation',
'users:banned',
2016-01-20 16:12:57 +02:00
'users:online',
'users:notvalidated',
'digest:day:uids',
'digest:week:uids',
2017-02-17 19:31:21 -07:00
'digest:month:uids',
], uid, next);
},
function (next) {
db.decrObjectField('global', 'userCount', next);
},
function (next) {
var keys = [
2016-05-18 19:02:43 +03:00
'uid:' + uid + ':notifications:read',
'uid:' + uid + ':notifications:unread',
2016-10-08 19:09:48 +03:00
'uid:' + uid + ':bookmarks',
2016-05-18 19:02:43 +03:00
'uid:' + uid + ':followed_tids',
'uid:' + uid + ':ignored_tids',
'user:' + uid + ':settings',
'uid:' + uid + ':topics', 'uid:' + uid + ':posts',
'uid:' + uid + ':chats', 'uid:' + uid + ':chats:unread',
2015-12-16 09:30:55 +02:00
'uid:' + uid + ':chat:rooms', 'uid:' + uid + ':chat:rooms:unread',
'uid:' + uid + ':upvote', 'uid:' + uid + ':downvote',
2015-12-24 12:20:27 +02:00
'uid:' + uid + ':ignored:cids', 'uid:' + uid + ':flag:pids',
2017-02-17 19:31:21 -07:00
'uid:' + uid + ':sessions', 'uid:' + uid + ':sessionUUID:sessionId',
];
db.deleteAll(keys, next);
},
function (next) {
deleteUserIps(uid, next);
},
function (next) {
deleteUserFromFollowers(uid, next);
},
function (next) {
groups.leaveAllGroups(uid, next);
2017-02-17 19:31:21 -07:00
},
2015-11-11 00:00:02 -05:00
], next);
},
function (results, next) {
db.deleteAll(['followers:' + uid, 'following:' + uid, 'user:' + uid], next);
2017-02-17 19:31:21 -07:00
},
2015-11-11 00:00:02 -05:00
], callback);
};
2015-11-11 00:00:02 -05:00
function deleteVotes(uid, callback) {
async.waterfall([
function (next) {
async.parallel({
upvotedPids: async.apply(db.getSortedSetRange, 'uid:' + uid + ':upvote', 0, -1),
2017-02-17 19:31:21 -07:00
downvotedPids: async.apply(db.getSortedSetRange, 'uid:' + uid + ':downvote', 0, -1),
2015-11-11 00:00:02 -05:00
}, next);
},
function (pids, next) {
pids = pids.upvotedPids.concat(pids.downvotedPids).filter(function (pid, index, array) {
2015-11-11 00:00:02 -05:00
return pid && array.indexOf(pid) === index;
});
2015-11-11 00:00:02 -05:00
async.eachSeries(pids, function (pid, next) {
2016-10-08 19:09:48 +03:00
posts.unvote(pid, uid, next);
2015-11-11 00:00:02 -05:00
}, next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
2015-11-11 00:00:02 -05:00
callback(err);
});
2015-11-11 00:00:02 -05:00
}
2015-12-16 09:30:55 +02:00
function deleteChats(uid, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('uid:' + uid + ':chat:rooms', 0, -1, next);
},
function (roomIds, next) {
var userKeys = roomIds.map(function (roomId) {
2015-12-16 09:30:55 +02:00
return 'uid:' + uid + ':chat:room:' + roomId + ':mids';
});
var roomKeys = roomIds.map(function (roomId) {
2015-12-16 09:30:55 +02:00
return 'chat:room:' + roomId + ':uids';
});
async.parallel([
async.apply(db.sortedSetsRemove, roomKeys, uid),
2017-02-17 19:31:21 -07:00
async.apply(db.deleteAll, userKeys),
2015-12-16 09:30:55 +02:00
], next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
2015-12-16 09:30:55 +02:00
callback(err);
});
}
function deleteUserIps(uid, callback) {
2015-10-22 15:16:31 -04:00
async.waterfall([
function (next) {
db.getSortedSetRange('uid:' + uid + ':ip', 0, -1, next);
},
function (ips, next) {
var keys = ips.map(function (ip) {
2015-10-22 15:16:31 -04:00
return 'ip:' + ip + ':uid';
});
db.sortedSetsRemove(keys, uid, next);
},
function (next) {
db.delete('uid:' + uid + ':ip', next);
2017-02-17 19:31:21 -07:00
},
2015-10-22 15:16:31 -04:00
], callback);
}
function deleteUserFromFollowers(uid, callback) {
2015-04-09 15:37:20 -04:00
async.parallel({
followers: async.apply(db.getSortedSetRange, 'followers:' + uid, 0, -1),
2017-02-17 19:31:21 -07:00
following: async.apply(db.getSortedSetRange, 'following:' + uid, 0, -1),
}, function (err, results) {
2015-04-09 15:37:20 -04:00
function updateCount(uids, name, fieldName, next) {
async.each(uids, function (uid, next) {
db.sortedSetCard(name + uid, function (err, count) {
2015-04-09 15:37:20 -04:00
if (err) {
return next(err);
}
count = parseInt(count, 10) || 0;
db.setObjectField('user:' + uid, fieldName, count, next);
});
}, next);
}
if (err) {
return callback(err);
}
var followingSets = results.followers.map(function (uid) {
2014-09-16 22:25:12 -04:00
return 'following:' + uid;
});
var followerSets = results.following.map(function (uid) {
2015-04-09 15:37:20 -04:00
return 'followers:' + uid;
});
async.parallel([
async.apply(db.sortedSetsRemove, followerSets.concat(followingSets), uid),
async.apply(updateCount, results.following, 'followers:', 'followerCount'),
2017-02-17 19:31:21 -07:00
async.apply(updateCount, results.followers, 'following:', 'followingCount'),
2015-04-09 15:37:20 -04:00
], callback);
});
}
2014-03-23 23:34:04 -04:00
};