Skip to content

Commit

Permalink
Fix bug with User not being in removal reparse section
Browse files Browse the repository at this point in the history
* Calling this removal reparse for lack of better naming
* Fix bug on User removal when there is no other content
* Fill in the basic construct for Comment and Discussion removal... designed so the top portion can be moved around to a separate function if needed... yes it might need some compression but not until the remaining switch cases are filled in to see what can code be reused.
* Removal of an owned discussion "orphans" others comment however it's not fatal and keeps their comments around
* When comment editing comes around... will need to call remove Comment and check if the Discussion needs to be removed as well.
* Tested orphans to ensure they didn't get reassigned if a topic is resurrected.
* May need a little tweaking on User Comments list with breadcrumbs but that is UI specific.
* Very literal coding here... using `null` in all *async* fields... I'm aware of `undefined` but lessens the readability and contributor understanding... negligible impact
* Watching for VPS timing issue with potential multiple comments... and it has been attempted on pro already by others... added a TODO for this and will watch stderr

**NOTES**
* Keeping OpenUserJS#126 open until all removal issues are with minimal coverage

Applies to OpenUserJS#126 ... shamelessly treads on OpenUserJS#262 (comment)
Martii committed Feb 26, 2016
1 parent 5ec8f8e commit 9969864
Showing 1 changed file with 241 additions and 50 deletions.
291 changes: 241 additions & 50 deletions libs/remove.js
Original file line number Diff line number Diff line change
@@ -57,65 +57,256 @@ exports.removeable = removeable;

function remove(aModel, aContent, aUser, aReason, aAutomated, aCallback) {

// TODO: #126, #93
switch (aModel.modelName) {
case 'Script':
// TODO: Remove from any non-owned Groups and decrement that Group counter
break;
case 'Group':
// TODO: Find all Scripts in it and do something
break;
case 'Comment':
// TODO: Find Discussion... if owned do nothing will be caught next...
// if non-owned decrement counter and reset the last
// commentator with that date
break;
case 'Discussion':
// TODO: Find all Comments in it and do something with non-owned...
// probably set `.path` to parent "slug" for non-owned
break;
case 'Flag':
// TODO: Recalculate affected scripts (and any other model) with `.flags`
break;
case 'Vote':
// TODO: Recalculate affected scripts (and any other model) with `.votes`
break;
default:
console.error('Unknown Model not covered in remove');
}
async.series([
function (aOuterCallback) {

var removeModel = new Remove({
'model': aModel.modelName,
'content': aContent.toObject(),
'removed': new Date(),
'reason': aReason,
'removerName': aUser.name,
'removerRole': aUser.role,
'removerAutomated': aAutomated,
'_removerId': aUser._id
});
// TODO: #126
switch (aModel.modelName) {
case 'User':
// NOTE: Do nothing
aOuterCallback(null);
break;
case 'Script':
// TODO: Remove from any non-owned Groups and decrement that Group counter #93
aOuterCallback(null);
break;
case 'Group':
// TODO: Find all Scripts in it and do something #93
aOuterCallback(null);
break;
case 'Comment':

removeModel.save(function (aErr, aRemove) {
if (aErr || !aRemove) {
console.error('Failed to save to the Graveyard', aModel.modelName, aContent._id);
aCallback(aErr);
return;
async.waterfall([
// Find Discussion
function (aInnerCallback) {

models['Discussion'].findOne({
_id: aContent._discussionId
},
function (aErr, aDiscussion) {

if (aErr || !aDiscussion) {
aInnerCallback(null, null);
return;
}

aInnerCallback(null, aDiscussion);
});

},

// Find if any newer comments
function (aDiscussion, aInnerCallback) {
var nextComment = null;

if (!aDiscussion) {
aInnerCallback(null, null, null);
return;
}

models['Comment'].find({
_discussionId: aDiscussion._id,
created: { $gt: aContent.created }
},
null,
{
sort: 'created'
},
function (aErr, aComments) {

if (aErr || !aComments) {
aInnerCallback(null, aDiscussion, null);
return;
}

if (aComments.length !== 0) {
nextComment = aComments[aComments.length - 1];
}

aInnerCallback(null, aDiscussion, nextComment);
});

},

// Find if any older comments
function (aDiscussion, aNextComment, aInnerCallback) {
var prevComment = null;

if (!aDiscussion) {
aInnerCallback(null, null, null, null);
return;
}

models['Comment'].find({
_discussionId: aDiscussion._id,
created: { $lt: aContent.created }
},
null,
{
sort: 'created'
},
function (aErr, aComments) {

if (aErr || !aComments) {
aInnerCallback(null, aDiscussion, aNextComment, null);
return;
}

if (aComments.length !== 0) {
prevComment = aComments[aComments.length - 1];
}

aInnerCallback(null, aDiscussion, aNextComment, prevComment);
});

},

// Find if any same comments
function (aDiscussion, aNextComment, aPrevComment, aInnerCallback) {

if (!aDiscussion) {
console.warn('No discussion found for removal reparse');
aInnerCallback('Removal reparse failure', {
discussion: null,
nextComment: null,
prevComment: null
});
return;
}

models['Comment'].find({
_discussionId: aDiscussion._id,
created: { $eq: aContent.created }
},
null,
{
sort: 'created'
},
function (aErr, aComments) {

if (aErr || !aComments) {
console.warn('No comment with same creation date found for removal reparse');
aInnerCallback('Removal reparse failure', {
discussion: aDiscussion,
nextComment: aNextComment,
prevComment: aPrevComment
});
return;
}

if (aComments.length === 1) {
aInnerCallback(null, {
discussion: aDiscussion,
nextComment: aNextComment,
prevComment: aPrevComment
});

} else {

// TODO: Something fancy for figuring out what aPrevComment should be

console.error('Too many comments with same creation date for removal reparse',
aComments.length);
aInnerCallback('Removal reparse failure', {
discussion: aDiscussion,
nextComment: aNextComment,
prevComment: aPrevComment
});
}

});

}
],
function (aErr, aResult) {

if (aErr || !aResult.discussion) {
aOuterCallback(aErr);
return;
}

if (aResult.nextComment) {
--aResult.discussion.comments;

aResult.discussion.save(function (aErr) {
aOuterCallback(null);
});

} else if (aResult.prevComment) {
--aResult.discussion.comments;
aResult.discussion.lastCommentor = aResult.prevComment.author;
aResult.discussion.updated = aResult.prevComment.created;

aResult.discussion.save(function (aErr) {
aOuterCallback(null);
});
} else {
aOuterCallback(null);
}

});
break;
case 'Discussion':
// TODO: Find all Comments in it and
// possibly do something with non-owned which get orphaned
aOuterCallback(null);
break;
case 'Flag':
// TODO: Recalculate affected scripts (and any other model) with `.flags`
aOuterCallback(null);
break;
case 'Vote':
// TODO: Recalculate affected scripts (and any other model) with `.votes`
aOuterCallback(null);
break;
default:
console.error('Unknown Model not covered in remove:', aModel.modelName);
aOuterCallback(null);
}
}
],
function (aErr, aResults) {

if (aErr) {
console.warn(aErr, aContent._id);
}

aContent.remove(function (aErr) {
if (aErr) {
console.error('Failed to remove', aModel.modelName, aContent._id);
aCallback(aErr); // NOTE: Same as `true` but specific e.g. stop all removal(s)
var removeModel = new Remove({
'model': aModel.modelName,
'content': aContent.toObject(),
'removed': new Date(),
'reason': aReason,
'removerName': aUser.name,
'removerRole': aUser.role,
'removerAutomated': aAutomated,
'_removerId': aUser._id
});

removeModel.save(function (aErr, aRemove) {
if (aErr || !aRemove) {
console.error('Failed to save to the Graveyard', aModel.modelName, aContent._id);
aCallback(aErr);
return;
}

if (aModel.modelName === 'User') {
aCallback(true); // NOTE: Stop any series removals and done
} else {
aCallback(null); // NOTE: Continue any series and non-User single removals
}
aContent.remove(function (aErr) {
if (aErr) {
console.error('Failed to remove', aModel.modelName, aContent._id);
aCallback(aErr); // NOTE: Same as `true` but specific e.g. stop all removal(s)
return;
}

if (aModel.modelName === 'User') {
aCallback(true); // NOTE: Stop any series removals and done
} else {
aCallback(null); // NOTE: Continue any series and non-User single removals
}
});
});


});

}

exports.remove = function (aModel, aContent, aUser, aReason, aCallback) {

0 comments on commit 9969864

Please sign in to comment.