Files
NodeBB/src/user/delete.js

172 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
var async = require('async'),
2014-06-10 14:24:50 -04:00
db = require('../database'),
posts = require('../posts'),
topics = require('../topics'),
groups = require('../groups'),
2014-09-16 22:25:12 -04:00
plugins = require('../plugins'),
batch = require('../batch');
module.exports = function(User) {
User.delete = function(uid, callback) {
2014-12-29 15:11:52 -05:00
if (!parseInt(uid, 10)) {
return callback(new Error('[[error:invalid-uid]]'));
}
async.waterfall([
function(next) {
deletePosts(uid, next);
},
function(next) {
deleteTopics(uid, next);
2014-08-08 17:42:03 -04:00
},
function(next) {
2014-08-26 13:47:48 -04:00
User.deleteAccount(uid, next);
}
2014-08-08 17:42:03 -04:00
], callback);
};
function deletePosts(uid, callback) {
2014-06-10 16:56:55 -04:00
deleteSortedSetElements('uid:' + uid + ':posts', posts.purge, callback);
}
function deleteTopics(uid, callback) {
2014-06-10 16:56:55 -04:00
deleteSortedSetElements('uid:' + uid + ':topics', topics.purge, callback);
}
function deleteSortedSetElements(set, deleteMethod, callback) {
batch.processSortedSet(set, function(ids, next) {
2014-09-16 22:25:12 -04:00
async.eachLimit(ids, 10, deleteMethod, next);
}, {alwaysStartAt: 0}, callback);
}
2014-09-24 00:07:24 -04:00
User.deleteAccount = function(uid, callback) {
User.getUserFields(uid, ['username', 'userslug', 'fullname', 'email'], function(err, userData) {
if (err) {
return callback(err);
}
async.parallel([
function(next) {
db.sortedSetRemove('username:uid', userData.username, next);
},
2015-05-19 23:04:28 -04:00
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) {
2014-12-29 15:11:52 -05:00
if (userData.email) {
2015-05-19 23:04:28 -04:00
async.parallel([
async.apply(db.sortedSetRemove, 'email:uid', userData.email.toLowerCase()),
async.apply(db.sortedSetRemove, 'email:sorted', userData.email.toLowerCase() + ':' + uid)
], next);
2014-12-29 15:11:52 -05:00
} else {
next();
}
},
function(next) {
db.sortedSetsRemove([
'users:joindate',
'users:postcount',
'users:reputation',
'users:banned',
'users:online'
], uid, next);
},
2015-05-21 14:52:39 -04:00
function(next) {
db.decrObjectField('global', 'userCount', next);
},
function(next) {
2014-09-16 22:25:12 -04:00
var keys = [
'uid:' + uid + ':notifications:read', 'uid:' + uid + ':notifications:unread',
2015-01-13 14:54:13 -05:00
'uid:' + uid + ':favourites', 'uid:' + uid + ':followed_tids', 'user:' + uid + ':settings',
2014-09-16 22:25:12 -04:00
'uid:' + uid + ':topics', 'uid:' + uid + ':posts',
'uid:' + uid + ':chats', 'uid:' + uid + ':chats:unread',
'uid:' + uid + ':upvote', 'uid:' + uid + ':downvote',
2015-02-19 15:56:04 -05:00
'uid:' + uid + ':ignored:cids', 'uid:' + uid + ':flag:pids'
2014-09-16 22:25:12 -04:00
];
db.deleteAll(keys, next);
},
function(next) {
2015-01-22 14:13:39 -05:00
deleteUserIps(uid, next);
},
function(next) {
deleteUserFromFollowers(uid, next);
},
function(next) {
groups.leaveAllGroups(uid, next);
},
function(next) {
plugins.fireHook('filter:user.delete', uid, next);
}
], function(err) {
if (err) {
return callback(err);
}
2015-05-21 14:52:39 -04:00
db.deleteAll(['followers:' + uid, 'following:' + uid, 'user:' + uid], callback);
});
});
2014-09-24 00:07:24 -04:00
};
function deleteUserIps(uid, callback) {
db.getSortedSetRange('uid:' + uid + ':ip', 0, -1, function(err, ips) {
if (err) {
return callback(err);
}
async.each(ips, function(ip, next) {
db.sortedSetRemove('ip:' + ip + ':uid', uid, next);
}, function(err) {
if (err) {
return callback(err);
}
db.delete('uid:' + uid + ':ip', callback);
});
2015-01-22 14:19:23 -05:00
});
}
function deleteUserFromFollowers(uid, callback) {
2015-04-09 15:37:20 -04:00
async.parallel({
followers: async.apply(db.getSortedSetRange, 'followers:' + uid, 0, -1),
following: async.apply(db.getSortedSetRange, 'following:' + uid, 0, -1)
}, function(err, results) {
function updateCount(uids, name, fieldName, next) {
async.each(uids, function(uid, next) {
db.sortedSetCard(name + uid, function(err, count) {
if (err) {
return next(err);
}
count = parseInt(count, 10) || 0;
db.setObjectField('user:' + uid, fieldName, count, next);
});
}, next);
}
if (err) {
return callback(err);
}
2015-04-09 15:37:20 -04:00
var followingSets = results.followers.map(function(uid) {
2014-09-16 22:25:12 -04:00
return 'following:' + uid;
});
2015-04-09 15:37:20 -04:00
var followerSets = results.following.map(function(uid) {
return 'followers:' + uid;
});
async.parallel([
async.apply(db.sortedSetsRemove, followerSets.concat(followingSets), uid),
async.apply(updateCount, results.following, 'followers:', 'followerCount'),
async.apply(updateCount, results.followers, 'following:', 'followingCount')
], callback);
});
}
2014-03-23 23:34:04 -04:00
};