Skip to content

Commit

Permalink
Merge pull request #774 from Martii/Issue-641
Browse files Browse the repository at this point in the history
Shift `flags` to `flags.critical` in affected models

Auto-merge...migration complete... updating VPS
  • Loading branch information
Martii committed Oct 21, 2015
2 parents 1836ce3 + aa1529a commit a473fe7
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 24 deletions.
2 changes: 1 addition & 1 deletion controllers/discussion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion controllers/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
53 changes: 44 additions & 9 deletions libs/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions libs/modelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions libs/modelQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ var commentSchema = new Schema({

// Moderation
creator: Boolean,
flags: Number,
flags: {
critical: Number,
absolute: Number
},
flagged: Boolean,

// Extra info
Expand Down
7 changes: 5 additions & 2 deletions models/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
5 changes: 4 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ var userSchema = new Schema({

// Moderation
role: Number,
flags: Number,
flags: {
critical: Number,
absolute: Number
},
flagged: Boolean,
sessionIds: [String]
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion views/includes/flagModelSnippet.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="form-group">
<label class="col-sm-4 control-label">Flags</label>
<div class="col-sm-8">
<p class="form-control-static">{{flags}}</p>
<p class="form-control-static">{{flags.critical}}</p>
</div>
</div>
<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion views/includes/scriptUserToolsPanel.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<p><i class="fa fa-fw fa-heartbeat"></i> <b>Rating:</b> {{script.rating}}</p>
<div class="progress">
<div style="width: {{script.votesPercent}}%" class="progress-bar progress-bar-good">{{#script.votes}}{{script.votes}} Votes{{/script.votes}}</div>
<div style="width: {{script.flagsPercent}}%" class="progress-bar progress-bar-danger">{{#script.flags}}{{script.flags}} <i class="fa fa-fw fa-flag"></i>{{/script.flags}}</div>
<div style="width: {{script.flagsPercent}}%" class="progress-bar progress-bar-danger">{{#script.flags.critical}}{{script.flags.critical}} <i class="fa fa-fw fa-flag"></i>{{/script.flags.critical}}</div>
</div>
</div>
<ul class="nav nav-pills nav-justified">
Expand Down

0 comments on commit a473fe7

Please sign in to comment.