@@ -11,7 +11,7 @@ var User = require('../models/user').User;
11
11
var async = require ( 'async' ) ;
12
12
13
13
// 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
15
15
var models = { } ;
16
16
17
17
modelNames . forEach ( function ( aModelName ) {
@@ -35,13 +35,19 @@ function removeable(aModel, aContent, aUser, aCallback) {
35
35
aContent ) ;
36
36
}
37
37
38
- User . findOne ( { _id : aContent . _authorId } , function ( aErr , aAuthor ) {
38
+ User . findOne ( {
39
+ _id : aContent . _authorId
40
+ } , function ( aErr , aAuthor ) {
39
41
// Content without an author shouldn't exist
40
- if ( aErr || ! aAuthor ) { return aCallback ( false ) ; }
42
+ if ( aErr || ! aAuthor ) {
43
+ return aCallback ( false ) ;
44
+ }
41
45
42
46
// You can't remove your own content this way
43
47
// 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
+ }
45
51
46
52
// You can only remove content by an author with a lesser user role
47
53
aCallback ( aAuthor . role > aUser . role , aAuthor ) ;
@@ -50,6 +56,34 @@ function removeable(aModel, aContent, aUser, aCallback) {
50
56
exports . removeable = removeable ;
51
57
52
58
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
+
53
87
var removeModel = new Remove ( {
54
88
'model' : aModel . modelName ,
55
89
'content' : aContent . toObject ( ) ,
@@ -62,27 +96,56 @@ function remove(aModel, aContent, aUser, aReason, aAutomated, aCallback) {
62
96
} ) ;
63
97
64
98
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
+ } ) ;
66
118
} ) ;
67
119
}
68
120
69
121
exports . remove = function ( aModel , aContent , aUser , aReason , aCallback ) {
70
122
removeable ( aModel , aContent , aUser , function ( aCanRemove , aAuthor ) {
71
123
if ( ! aCanRemove ) {
72
- return aCallback ( false ) ;
124
+ aCallback ( false ) ;
125
+ return ;
73
126
}
74
127
75
128
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
+ } ) ;
77
138
} else {
78
139
// Remove all the user's content
79
- async . each ( modelNames , function ( aModelName , aCallback ) {
140
+ async . eachSeries ( modelNames , function ( aModelName , aEachOuterCallback ) {
80
141
var model = models [ aModelName ] ;
81
- model . find ( { _authorId : aContent . _id } ,
142
+ model . find ( {
143
+ _authorId : aContent . _id
144
+ } ,
82
145
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 ) ;
86
149
} ) ;
87
150
} , function ( ) {
88
151
remove ( aModel , aContent , aUser , aReason , false , aCallback ) ;
@@ -114,7 +177,10 @@ exports.findDeadorAlive = function (aModel, aQuery, aUser, aCallback) {
114
177
var name = null ;
115
178
var rmQuery = { model : modelName } ;
116
179
117
- if ( ! aErr && aContent ) { return aCallback ( true , aContent , null ) ; }
180
+ if ( ! aErr && aContent ) {
181
+ return aCallback ( true , aContent , null ) ;
182
+ }
183
+
118
184
if ( modelName !== 'User' && - 1 === modelNames . indexOf ( modelName ) ) {
119
185
return aCallback ( null , null , null ) ;
120
186
}
@@ -124,7 +190,10 @@ exports.findDeadorAlive = function (aModel, aQuery, aUser, aCallback) {
124
190
}
125
191
126
192
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
+
128
197
if ( ! aUser || ( aUser !== true && aUser . role > aRemoved . removerRole ) ) {
129
198
return aCallback ( false , null , aRemoved ) ;
130
199
}
0 commit comments