From aa1529a6d3b3e098257b0eb98b316fae75efbe45 Mon Sep 17 00:00:00 2001 From: Martii Date: Tue, 20 Oct 2015 21:16:58 -0600 Subject: [PATCH] Shift `flags` to `flags.critical` in affected models * Project version bump... e.g. a "hump" in the DB... going back will require an earlier commit HEAD and an earlier DB * Change everything affected * Tiny bit of STYLEGUIDE.md compliance * Error trap flag user... if currently seen means that a user migration didn't occur **NOTES** More to go... Applies to #641 --- controllers/admin.js | 2 +- controllers/discussion.js | 2 +- controllers/script.js | 2 +- controllers/scriptStorage.js | 2 +- controllers/user.js | 4 +- libs/flag.js | 53 ++++++++++++++++++++---- libs/modelParser.js | 5 ++- libs/modelQuery.js | 4 +- models/comment.js | 5 ++- models/script.js | 7 +++- models/user.js | 5 ++- package.json | 2 +- views/includes/flagModelSnippet.html | 2 +- views/includes/scriptUserToolsPanel.html | 2 +- 14 files changed, 71 insertions(+), 26 deletions(-) diff --git a/controllers/admin.js b/controllers/admin.js index efabf6715..92bf8c9c8 100644 --- a/controllers/admin.js +++ b/controllers/admin.js @@ -474,6 +474,6 @@ exports.authAsUser = function (aReq, aRes, aNext) { aReq.session.user = user; - aRes.redirect(user.userPageUrl); + aRes.redirect(encodeURI(user.userPageUrl)); }); }; diff --git a/controllers/discussion.js b/controllers/discussion.js index 07e6f2ad7..1da06ac57 100644 --- a/controllers/discussion.js +++ b/controllers/discussion.js @@ -338,7 +338,7 @@ function postComment(aUser, aDiscussion, aContent, aCreator, aCallback) { created: created, rating: 0, creator: aCreator, - flags: 0, + flags: { critical: 0 }, flagged: false, id: created.getTime().toString(16), _discussionId: aDiscussion._id, diff --git a/controllers/script.js b/controllers/script.js index 8df1f7756..89c125927 100644 --- a/controllers/script.js +++ b/controllers/script.js @@ -257,7 +257,7 @@ var getScriptPageTasks = function (aOptions) { removeLib.removeable(Script, script, authedUser, function (aCanRemove, aAuthor) { aOptions.canRemove = aCanRemove; - aOptions.flags = script.flags || 0; + aOptions.flags = (script.flags ? script.flags.critical : null) || 0; aOptions.removeUrl = '/remove' + (script.isLib ? '/libs/' : '/scripts/') + script.installNameSlug; if (!aCanRemove) { diff --git a/controllers/scriptStorage.js b/controllers/scriptStorage.js index f9cd99e41..2801eb971 100644 --- a/controllers/scriptStorage.js +++ b/controllers/scriptStorage.js @@ -472,7 +472,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) { about: '', updated: new Date(), votes: 0, - flags: 0, + flags: { critical: 0 }, installName: installName, fork: null, meta: isLibrary ? { name: aMeta } : aMeta, diff --git a/controllers/user.js b/controllers/user.js index 50ed610e3..5536463d4 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -79,7 +79,7 @@ var setupUserModerationUITask = function (aOptions) { removeLib.removeable(User, user, authedUser, function (aCanRemove, aAuthor) { aOptions.canRemove = aCanRemove; - aOptions.flags = user.flags || 0; + aOptions.flags = (user.flags ? user.flags.critical : null) || 0; if (!aCanRemove) { return aCallback(); @@ -1481,7 +1481,7 @@ exports.flag = function (aReq, aRes, aNext) { if (aErr || !aUser) { return aNext(); } fn(User, aUser, aReq.session.user, function (aFlagged) { // NOTE: Inline function here - aRes.redirect('/users/' + username); + aRes.redirect('/users/' + encodeURI(username)); }); }); }; diff --git a/libs/flag.js b/libs/flag.js index 22139fd44..e333ab150 100644 --- a/libs/flag.js +++ b/libs/flag.js @@ -87,19 +87,40 @@ function getThreshold(aModel, aContent, aAuthor, aCallback) { exports.getThreshold = getThreshold; function saveContent(aModel, aContent, aAuthor, aFlags, aCallback) { - if (!aContent.flags) { aContent.flags = 0; } - aContent.flags += aFlags; + if (!aContent.flags) { + aContent.flags = {}; + } + + if (!aContent.flags.critical) { + aContent.flags.critical = 0; + } + aContent.flags.critical += aFlags; - if (aContent.flags >= thresholds[aModel.modelName] * (aAuthor.role < 4 ? 2 : 1)) { + if (aContent.flags.critical >= thresholds[aModel.modelName] * (aAuthor.role < 4 ? 2 : 1)) { return getThreshold(aModel, aContent, aAuthor, function (aThreshold) { - aContent.flagged = aContent.flags >= aThreshold; - aContent.save(function (aErr, aContent) { aCallback(aContent.flagged); }); + aContent.flagged = aContent.flags.critical >= aThreshold; + + aContent.save(function (aErr, aContent) { + if (aErr) { + console.warn('Error flagging content', aErr); + aCallback(null); + return; + } + aCallback(aContent.flagged); + }); }); } else { aContent.flagged = false; } - aContent.save(function (aErr, aContent) { aCallback(aContent.flagged); }); + aContent.save(function (aErr, aContent) { + if (aErr) { + console.warn('Error unflagging content', aErr); + aCallback(null); + return; + } + aCallback(aContent.flagged); + }); } exports.saveContent = saveContent; @@ -111,7 +132,13 @@ function flag(aModel, aContent, aUser, aAuthor, aCallback) { }); flag.save(function (aErr, aFlag) { - if (!aContent.flags) { aContent.flags = 0; } + if (!aContent.flags) { + aContent.flags = {}; + } + + if (!aContent.flags.critical) { + aContent.flags.critical = 0; + } if (!aContent.flagged) { aContent.flagged = false; } saveContent(aModel, aContent, aAuthor, aUser.role < 4 ? 2 : 1, aCallback); @@ -130,9 +157,17 @@ exports.unflag = function (aModel, aContent, aUser, aCallback) { if (!aUser) { return aCallback(null); } getFlag(aModel, aContent, aUser, function (aFlag) { - if (!aFlag) { return aCallback(null); } + if (!aFlag) { + return aCallback(null); + } - if (!aContent.flags) { aContent.flags = 0; } + if (!aContent.flags) { + aContent.flags = {}; + } + + if (!aContent.flags.critical) { + aContent.flags.critical = 0; + } if (!aContent.flagged) { aContent.flagged = false; } function removeFlag(aAuthor) { diff --git a/libs/modelParser.js b/libs/modelParser.js index 104577b8f..3535f13b8 100644 --- a/libs/modelParser.js +++ b/libs/modelParser.js @@ -175,10 +175,11 @@ var parseScript = function (aScriptData) { script.isFork = script.fork && script.fork.length > 0; // Script Good/Bad bar. - var sumVotesAndFlags = script.votes + script.flags; + var criticalFlags = (script.flags ? script.flags.critical : null) || 0; + var sumVotesAndFlags = script.votes + criticalFlags; var votesRatio = sumVotesAndFlags > 0 ? script.votes / sumVotesAndFlags : 1; - var flagsRatio = sumVotesAndFlags > 0 ? script.flags / sumVotesAndFlags : 0; + var flagsRatio = sumVotesAndFlags > 0 ? criticalFlags / sumVotesAndFlags : 0; var votesPercent = votesRatio * 100; var flagsPercent = flagsRatio * 100; diff --git a/libs/modelQuery.js b/libs/modelQuery.js index 3d5411553..26fd154ea 100644 --- a/libs/modelQuery.js +++ b/libs/modelQuery.js @@ -198,7 +198,7 @@ var applyModelListQueryFlaggedFilter = function (aModelListQuery, aOptions, aFla case 'absolute': if (aOptions.isAdmin) { aOptions.filterAbsolute = true; - aModelListQuery.and({ flagsAbsolute: { $gt: 0 } }); // TODO: This does not exist yet + aModelListQuery.and({ 'flags.absolute': { $gt: 0 } }); // TODO: Exists in schema but needs linkage break; } // fallthrough @@ -212,7 +212,7 @@ var applyModelListQueryFlaggedFilter = function (aModelListQuery, aOptions, aFla aOptions.isFlagged = 'true'; } - aModelListQuery.and({ flags: { $gt: 0 } }); + aModelListQuery.and({ 'flags.critical': { $gt: 0 } }); break; } diff --git a/models/comment.js b/models/comment.js index c4e7120b1..10732f103 100644 --- a/models/comment.js +++ b/models/comment.js @@ -18,7 +18,10 @@ var commentSchema = new Schema({ // Moderation creator: Boolean, - flags: Number, + flags: { + critical: Number, + absolute: Number + }, flagged: Boolean, // Extra info diff --git a/models/script.js b/models/script.js index cae33820e..64b08d2d9 100644 --- a/models/script.js +++ b/models/script.js @@ -20,8 +20,11 @@ var scriptSchema = new Schema({ updated: Date, // Moderation - votes: Number, // upvotes negate flags - flags: Number, + votes: Number, // upvotes negate flags.critical + flags: { + critical: Number, + absolute: Number + }, flagged: Boolean, installName: String, diff --git a/models/user.js b/models/user.js index 6c5358acf..59ba84349 100644 --- a/models/user.js +++ b/models/user.js @@ -23,7 +23,10 @@ var userSchema = new Schema({ // Moderation role: Number, - flags: Number, + flags: { + critical: Number, + absolute: Number + }, flagged: Boolean, sessionIds: [String] }); diff --git a/package.json b/package.json index 64550e1ad..762116a3f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "OpenUserJS.org", "description": "An open source user scripts repo built using Node.js", - "version": "0.2.0", + "version": "0.2.1", "main": "app", "dependencies": { "ace-builds": "git://github.com/ajaxorg/ace-builds#b082bcb", diff --git a/views/includes/flagModelSnippet.html b/views/includes/flagModelSnippet.html index ebdaa3247..979b2ffcb 100644 --- a/views/includes/flagModelSnippet.html +++ b/views/includes/flagModelSnippet.html @@ -2,7 +2,7 @@
-

{{flags}}

+

{{flags.critical}}

diff --git a/views/includes/scriptUserToolsPanel.html b/views/includes/scriptUserToolsPanel.html index c36934509..09e604481 100644 --- a/views/includes/scriptUserToolsPanel.html +++ b/views/includes/scriptUserToolsPanel.html @@ -8,7 +8,7 @@

Rating: {{script.rating}}

{{#script.votes}}{{script.votes}} Votes{{/script.votes}}
-
{{#script.flags}}{{script.flags}} {{/script.flags}}
+
{{#script.flags.critical}}{{script.flags.critical}} {{/script.flags.critical}}