-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 643 #770
Issue 643 #770
Changes from 2 commits
7cf7602
96d9d99
58602e3
01cb33e
ad2edb1
15c7c54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,8 +234,29 @@ exports.userListPage = function (aReq, aRes, aNext) { | |
pageMetadata(options, ['Flagged Users', 'Moderation']); | ||
} | ||
} | ||
function render() { aRes.render('pages/userListPage', options); } | ||
function asyncComplete(err) { if (err) { return aNext(); } else { preRender(); render(); } } | ||
function render() { | ||
aRes.render('pages/userListPage', options); | ||
} | ||
function asyncComplete(aErr) { | ||
if (aErr) { | ||
aNext(); | ||
return; | ||
} | ||
|
||
async.parallel([ | ||
function (aCallback) { | ||
if (!options.isFlagged || !options.isAdmin) { // NOTE: Watchpoint | ||
aCallback(); | ||
return; | ||
} | ||
flagLib.setFlaggedListInModel('User', options, aCallback); | ||
} | ||
], function (aErr) { | ||
preRender(); | ||
render(); | ||
}); | ||
|
||
} | ||
async.parallel(tasks, asyncComplete); | ||
}; | ||
|
||
|
@@ -292,9 +313,27 @@ exports.view = function (aReq, aRes, aNext) { | |
tasks = tasks.concat(stats.getSummaryTasks(options)); | ||
|
||
//--- | ||
function preRender() { } | ||
function render() { aRes.render('pages/userPage', options); } | ||
function asyncComplete() { preRender(); render(); } | ||
function preRender() { | ||
} | ||
function render() { | ||
aRes.render('pages/userPage', options); | ||
} | ||
function asyncComplete() { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Elsewhere" after |
||
async.parallel([ | ||
function (aCallback) { | ||
if (!options.isAdmin) { // NOTE: Watchpoint | ||
aCallback(); | ||
return; | ||
} | ||
flagLib.setFlaggedListInModel('User', options, aCallback); | ||
} | ||
], function (aErr) { | ||
preRender(); | ||
render(); | ||
}); | ||
|
||
} | ||
async.parallel(tasks, asyncComplete); | ||
}); | ||
}; | ||
|
@@ -410,13 +449,14 @@ exports.userCommentListPage = function (aReq, aRes, aNext) { | |
|
||
exports.userScriptListPage = function (aReq, aRes, aNext) { | ||
var authedUser = aReq.session.user; | ||
|
||
var username = aReq.params.username; | ||
|
||
User.findOne({ | ||
name: caseInsensitive(username) | ||
}, function (aErr, aUserData) { | ||
if (aErr || !aUserData) { return aNext(); } | ||
if (aErr || !aUserData) { | ||
return aNext(); | ||
} | ||
|
||
// | ||
var options = {}; | ||
|
@@ -497,8 +537,25 @@ exports.userScriptListPage = function (aReq, aRes, aNext) { | |
options.scriptListIsEmptyMessage = 'This user hasn\'t added any scripts yet.'; | ||
} | ||
} | ||
function render() { aRes.render('pages/userScriptListPage', options); } | ||
function asyncComplete() { preRender(); render(); } | ||
function render() { | ||
aRes.render('pages/userScriptListPage', options); | ||
} | ||
function asyncComplete() { | ||
|
||
async.parallel([ | ||
function (aCallback) { | ||
if (!options.isFlagged || !options.isAdmin) { // NOTE: Watchpoint | ||
aCallback(); | ||
return; | ||
} | ||
flagLib.setFlaggedListInModel('Script', options, aCallback); | ||
} | ||
], function (aErr) { | ||
preRender(); | ||
render(); | ||
}); | ||
|
||
} | ||
async.parallel(tasks, asyncComplete); | ||
}); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ var isDev = require('../libs/debug').isDev; | |
var isDbg = require('../libs/debug').isDbg; | ||
|
||
// | ||
var async = require('async'); | ||
|
||
var Flag = require('../models/flag').Flag; | ||
var User = require('../models/user').User; | ||
var getKarma = require('./collectiveRating').getKarma; | ||
|
@@ -148,3 +150,51 @@ exports.unflag = function (aModel, aContent, aUser, aCallback) { | |
} | ||
}); | ||
}; | ||
|
||
exports.setFlaggedListInModel = function (aModelName, aOptions, aCallback) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definitely the wrong module for this function. This module is supposed to be completely devoid of any front-end view logic (notice how "aOptions" or "options" are mentioned nowhere else in this file). This module defines the logic for how flagging is handled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So where should it go?... I don't have a clue since this function is a "one of a kind new" thing AFAIK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Answered in my comment below. |
||
|
||
var content = aModelName.toLowerCase(); | ||
var contentList = aOptions[content + 'List'] || [aOptions[content]]; | ||
|
||
async.forEachOf(contentList, function (aContent, aContentKey, aEachOuterCallback) { | ||
|
||
// NOTE: Directly use indexed parent identifier allowing set of the dynamic, virtual, field | ||
// So basically do not use `aContent` anywhere in this function | ||
|
||
// Always ensure a snapshot copy! | ||
if (contentList[aContentKey].toObject) { | ||
contentList[aContentKey] = contentList[aContentKey].toObject({ | ||
virtuals: true | ||
}); | ||
} | ||
|
||
// Ensure reset | ||
contentList[aContentKey].flaggedList = []; | ||
|
||
// Find any flags | ||
Flag.find({ | ||
model: aModelName, | ||
_contentId: contentList[aContentKey]._id | ||
|
||
}, function (aErr, aFlagList) { | ||
if (aErr || !aFlagList || aFlagList.length === 0) { | ||
aEachOuterCallback(); | ||
return; | ||
} | ||
|
||
aOptions.hasFlagged = true; | ||
|
||
async.forEachOfSeries(aFlagList, function (aFlag, aFlagKey, aEachInnerCallback) { | ||
User.findOne({ _id: aFlag._userId }, function (aErr, aUser) { | ||
contentList[aContentKey].flaggedList.push({ | ||
name: aUser.name, | ||
reason: aFlagList[aFlagKey].reason, | ||
since: aFlagList[aFlagKey]._since | ||
}); | ||
aEachInnerCallback(); | ||
}); | ||
}, aEachOuterCallback); | ||
}); | ||
|
||
}, aCallback); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MITIGATION: Test why this error fall through is the only one (for express)... I am guessing if we (ever) have an error in the changed views ("elsewhere") we're going to 500 and not fail gracefully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asyncComplete should always test for an error and handle it (at the very least a 500 page). If we can catch an error earlier on then great, but this is really the last line of defense. It would be nice if we could find a way to abstract this function and put it in libs/helpers.js since I hate seeing the same logic repeated, but I realize the difficulty of doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of figured that with the missing
aNext()
and it raised an eyebrow yesterday in my background thinking... I think I'll do that in a single PR with some cleanup just on./controllers/user.js
... I read elsewhere that ES6 is going to needundefined
for certain functions instead ofnull
(or worse some other huge object in a bug) so that is why I have been splitting out theaNext()
andreturn
statements... also it makes more sense not to return something that isn't there and could change down the line.Just a misc note too node 4.x went LTS (Long Term Support) according to their site e.g. 5.x is around some corner.