delete groups and users from firebase when deleted on habitica

This commit is contained in:
Matteo Pagliazzi
2015-09-06 16:09:13 +02:00
parent 9764e31e2a
commit 521c999963
3 changed files with 22 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ var logging = require('./../logging');
var acceptablePUTPaths; var acceptablePUTPaths;
var api = module.exports; var api = module.exports;
var qs = require('qs'); var qs = require('qs');
var firebase = require('../libs/firebase');
var webhook = require('../webhook'); var webhook = require('../webhook');
// api.purchase // Shared.ops // api.purchase // Shared.ops
@@ -386,6 +387,8 @@ api['delete'] = function(req, res, next) {
user.remove(function(err){ user.remove(function(err){
if(err) return next(err); if(err) return next(err);
firebase.deleteUser(user._id);
res.send(200); res.send(200);
}); });
}); });

View File

@@ -18,7 +18,6 @@ var api = module.exports = {};
api.addUserToGroup = function(groupId, userId){ api.addUserToGroup = function(groupId, userId){
if(!isProd) return; if(!isProd) return;
// TODO is throw ok? we don't have callbacks // TODO is throw ok? we don't have callbacks
if(!userId || !groupId) throw new Error('groupId, userId are required.'); if(!userId || !groupId) throw new Error('groupId, userId are required.');
@@ -31,7 +30,6 @@ api.addUserToGroup = function(groupId, userId){
api.removeUserFromGroup = function(groupId, userId){ api.removeUserFromGroup = function(groupId, userId){
if(!isProd) return; if(!isProd) return;
if(!userId || !groupId) throw new Error('groupId, userId are required.'); if(!userId || !groupId) throw new Error('groupId, userId are required.');
firebaseRef.child('members/' + groupId + '/' + userId) firebaseRef.child('members/' + groupId + '/' + userId)
@@ -41,4 +39,20 @@ api.removeUserFromGroup = function(groupId, userId){
.remove(); .remove();
}; };
api.deleteGroup = function(groupId){
if(!isProd) return;
if(!groupId) throw new Error('groupId is required.');
firebaseRef.child('members/' + groupId)
.remove();
};
api.deleteUser = function(userId){
if(!isProd) return;
if(!userId) throw new Error('userId is required.');
firebaseRef.child('users/' + userId)
.remove();
};
api.userLeaves api.userLeaves

View File

@@ -358,6 +358,7 @@ GroupSchema.methods.leave = function(user, keep, mainCb){
if(typeof keep !== 'string') keep = 'keep-all'; // can be also 'remove-all' if(typeof keep !== 'string') keep = 'keep-all'; // can be also 'remove-all'
var group = this; var group = this;
var groupWasRemoved = false;
async.parallel([ async.parallel([
// Remove active quest from user if they're leaving the party // Remove active quest from user if they're leaving the party
@@ -411,6 +412,7 @@ GroupSchema.methods.leave = function(user, keep, mainCb){
(group.type === 'guild' && group.privacy === 'private') (group.type === 'guild' && group.privacy === 'private')
)){ )){
// TODO remove invitations to this group // TODO remove invitations to this group
groupWasRemoved = true;
Group.remove({ Group.remove({
_id: group._id _id: group._id
}, cb); }, cb);
@@ -449,6 +451,7 @@ GroupSchema.methods.leave = function(user, keep, mainCb){
if(err) return mainCb(err); if(err) return mainCb(err);
firebase.removeUserFromGroup(group._id, user._id); firebase.removeUserFromGroup(group._id, user._id);
if(groupWasRemoved) firebase.deleteGroup(group._id);
return mainCb(); return mainCb();
}); });
}; };