Skip to content

Commit 1b72e08

Browse files
author
Martii
committed
Change *async* removal to be series instead of parallel
* This will probably be needed for ordered and decrementing mass removals down the line... and uncovered a few logic issues * Add some TODOs ... since this is a work in progress guestimating what should be done... final may be slightly different * Some STYLEGUIDE.md conformance * Add an error handler in `getFlaggedListForContent`... this will tell us if a flag has been found but no user in the non-long-term... Post OpenUserJS#643 fix in Moderation eyeball **NOTES** * Tested mostly on dev and some on local pro Applies to OpenUserJS#126, OpenUserJS#93 and trounces madly upon OpenUserJS#262 (comment) ... sawwy but tiz a boog.
1 parent 0d06785 commit 1b72e08

File tree

2 files changed

+92
-14
lines changed

2 files changed

+92
-14
lines changed

controllers/flag.js

+9
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ exports.getFlaggedListForContent = function (aModelName, aOptions, aCallback) {
169169

170170
async.forEachOfSeries(aFlagList, function (aFlag, aFlagKey, aEachInnerCallback) {
171171
User.findOne({ _id: aFlag._userId }, function (aErr, aUser) {
172+
if (aErr || !aUser) {
173+
// Notify in stdout
174+
console.warn('getFlaggedListForContent(): `_userId` not found for Flag:\n', aFlag);
175+
176+
// Ignore for now and move onto the next flag
177+
aEachInnerCallback();
178+
return;
179+
}
180+
172181
contentList[aContentKey].flaggedList.push({
173182
name: aUser.name,
174183
reason: aFlagList[aFlagKey].reason,

libs/remove.js

+83-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var User = require('../models/user').User;
1111
var async = require('async');
1212

1313
// Get the models for removable content that belongs to a user
14-
var modelNames = ['Script'];
14+
var modelNames = ['Script']; // TODO: , 'Group', 'Comment', 'Discussion', 'Flag', 'Vote' eventually
1515
var models = {};
1616

1717
modelNames.forEach(function (aModelName) {
@@ -35,13 +35,19 @@ function removeable(aModel, aContent, aUser, aCallback) {
3535
aContent);
3636
}
3737

38-
User.findOne({ _id: aContent._authorId }, function (aErr, aAuthor) {
38+
User.findOne({
39+
_id: aContent._authorId
40+
}, function (aErr, aAuthor) {
3941
// Content without an author shouldn't exist
40-
if (aErr || !aAuthor) { return aCallback(false); }
42+
if (aErr || !aAuthor) {
43+
return aCallback(false);
44+
}
4145

4246
// You can't remove your own content this way
4347
// When you remove your own content it's removed for good
44-
if (aAuthor._id == aUser._id) { return aCallback(false, aAuthor); }
48+
if (aAuthor._id == aUser._id) {
49+
return aCallback(false, aAuthor);
50+
}
4551

4652
// You can only remove content by an author with a lesser user role
4753
aCallback(aAuthor.role > aUser.role, aAuthor);
@@ -50,6 +56,34 @@ function removeable(aModel, aContent, aUser, aCallback) {
5056
exports.removeable = removeable;
5157

5258
function remove(aModel, aContent, aUser, aReason, aAutomated, aCallback) {
59+
60+
// TODO: #126, #93
61+
switch (aModel.modelName) {
62+
case 'Script':
63+
// TODO: Remove from any non-owned Groups and decrement that Group counter
64+
break;
65+
case 'Group':
66+
// TODO: Find all Scripts in it and do something
67+
break;
68+
case 'Comment':
69+
// TODO: Find Discussion... if owned do nothing will be caught next...
70+
// if non-owned decrement counter and reset the last
71+
// commentator with that date
72+
break;
73+
case 'Discussion':
74+
// TODO: Find all Comments in it and do something with non-owned...
75+
// probably set `.path` to parent "slug" for non-owned
76+
break;
77+
case 'Flag':
78+
// TODO: Recalculate affected scripts (and any other model) with `.flags`
79+
break;
80+
case 'Vote':
81+
// TODO: Recalculate affected scripts (and any other model) with `.votes`
82+
break;
83+
default:
84+
console.error('Unknown Model not covered in remove');
85+
}
86+
5387
var removeModel = new Remove({
5488
'model': aModel.modelName,
5589
'content': aContent.toObject(),
@@ -62,27 +96,56 @@ function remove(aModel, aContent, aUser, aReason, aAutomated, aCallback) {
6296
});
6397

6498
removeModel.save(function (aErr, aRemove) {
65-
aContent.remove(function (aErr) { aCallback(aRemove); });
99+
if (aErr || !aRemove) {
100+
console.error('Failed to save to the Graveyard');
101+
aCallback(aErr);
102+
return;
103+
}
104+
105+
aContent.remove(function (aErr) {
106+
if (aErr) {
107+
console.error('Failed to remove', aModel.modelName);
108+
aCallback(aErr);
109+
return;
110+
}
111+
112+
if (aModel.modelName === 'User') {
113+
aCallback(true); // NOTE: Stop any series removals
114+
} else {
115+
aCallback(null); // NOTE: Continue any series and non-User single removals
116+
}
117+
});
66118
});
67119
}
68120

69121
exports.remove = function (aModel, aContent, aUser, aReason, aCallback) {
70122
removeable(aModel, aContent, aUser, function (aCanRemove, aAuthor) {
71123
if (!aCanRemove) {
72-
return aCallback(false);
124+
aCallback(false);
125+
return;
73126
}
74127

75128
if (aModel.modelName !== 'User') {
76-
remove(aModel, aContent, aUser, aReason, false, aCallback);
129+
remove(aModel, aContent, aUser, aReason, false, function (aErr) {
130+
if (aErr) {
131+
console.warn('Failed to remove User\n', aErr);
132+
aCallback(false);
133+
return;
134+
}
135+
136+
aCallback(true);
137+
});
77138
} else {
78139
// Remove all the user's content
79-
async.each(modelNames, function (aModelName, aCallback) {
140+
async.eachSeries(modelNames, function (aModelName, aEachOuterCallback) {
80141
var model = models[aModelName];
81-
model.find({ _authorId: aContent._id },
142+
model.find({
143+
_authorId: aContent._id
144+
},
82145
function (aErr, aContentArr) {
83-
async.each(aContentArr, function (aContent, innerCb) {
84-
remove(model, aContent, aUser, '', true, innerCb);
85-
}, aCallback);
146+
async.eachSeries(aContentArr, function (aContent, aEachInnerCallback) {
147+
remove(model, aContent, aUser, '', true, aEachInnerCallback);
148+
}, aEachOuterCallback);
86149
});
87150
}, function () {
88151
remove(aModel, aContent, aUser, aReason, false, aCallback);
@@ -114,7 +177,10 @@ exports.findDeadorAlive = function (aModel, aQuery, aUser, aCallback) {
114177
var name = null;
115178
var rmQuery = { model: modelName };
116179

117-
if (!aErr && aContent) { return aCallback(true, aContent, null); }
180+
if (!aErr && aContent) {
181+
return aCallback(true, aContent, null);
182+
}
183+
118184
if (modelName !== 'User' && -1 === modelNames.indexOf(modelName)) {
119185
return aCallback(null, null, null);
120186
}
@@ -124,7 +190,10 @@ exports.findDeadorAlive = function (aModel, aQuery, aUser, aCallback) {
124190
}
125191

126192
Remove.findOne(rmQuery, function (aErr, aRemoved) {
127-
if (aErr || !aRemoved) { return aCallback(null, null, null); }
193+
if (aErr || !aRemoved) {
194+
return aCallback(null, null, null);
195+
}
196+
128197
if (!aUser || (aUser !== true && aUser.role > aRemoved.removerRole)) {
129198
return aCallback(false, null, aRemoved);
130199
}

0 commit comments

Comments
 (0)