mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Moved inivtation removal functions to group pre remove middleware
This commit is contained in:
@@ -84,6 +84,27 @@ GroupSchema.pre('save', function(next){
|
||||
next();
|
||||
})
|
||||
|
||||
GroupSchema.pre('remove', function(next) {
|
||||
var group = this;
|
||||
async.waterfall([
|
||||
function(cb2) {
|
||||
User.find({
|
||||
'invitations.guilds.id': group._id
|
||||
}, cb2);
|
||||
},
|
||||
function(users, cb2) {
|
||||
if (users) {
|
||||
users.forEach(function (user, index, array) {
|
||||
var i = _.findIndex(user.invitations.guilds, {id: group._id});
|
||||
user.invitations.guilds.splice(i, 1);
|
||||
user.save();
|
||||
});
|
||||
}
|
||||
cb2();
|
||||
}
|
||||
], next);
|
||||
});
|
||||
|
||||
GroupSchema.methods.toJSON = function(){
|
||||
var doc = this.toObject();
|
||||
removeDuplicates(doc);
|
||||
@@ -404,29 +425,12 @@ GroupSchema.methods.leave = function(user, keep, mainCb){
|
||||
group.type === 'party' ||
|
||||
(group.type === 'guild' && group.privacy === 'private')
|
||||
)){
|
||||
async.waterfall([
|
||||
function(cb2) {
|
||||
User.find({
|
||||
'invitations.guilds.id': group._id
|
||||
}, cb2);
|
||||
},
|
||||
function(users, cb2) {
|
||||
if (users) {
|
||||
users.forEach(function (user, index, array) {
|
||||
var i = _.findIndex(user.invitations.guilds, {id: group._id});
|
||||
user.invitations.guilds.splice(i, 1);
|
||||
user.save();
|
||||
});
|
||||
}
|
||||
cb2();
|
||||
},
|
||||
function(cb2) {
|
||||
groupWasRemoved = true;
|
||||
Group.remove({
|
||||
_id: group._id
|
||||
}, cb2);
|
||||
},
|
||||
], cb);
|
||||
groupWasRemoved = true;
|
||||
Group.findOne({
|
||||
_id: group._id
|
||||
}, function(err, groupDoc){
|
||||
groupDoc.remove(cb)
|
||||
});
|
||||
}else{ // otherwise just remove a member
|
||||
var update = {$pull: {members: user._id}};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user