Skip to content

Commit

Permalink
Save role weight in flag model (#1772)
Browse files Browse the repository at this point in the history
* When promoting/demoting a User future flagging/unflagging can wreak havoc on critical *(relative)* flags properties.
* Project bump since requires a bit of migration
* Needs followup testing with #1109

Applies to #126 and post #641

Auto-merge
  • Loading branch information
Martii authored Oct 23, 2020
1 parent a3d2157 commit 76b018e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
15 changes: 9 additions & 6 deletions libs/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ function getThreshold(aModel, aContent, aAuthor, aCallback) {
}
exports.getThreshold = getThreshold;

function saveContent(aModel, aContent, aAuthor, aFlags, aIsFlagging, aCallback) {
function saveContent(aModel, aContent, aAuthor, aWeight, aIsFlagging, aCallback) {
var weight = (aAuthor.role < 4 ? 2 : 1);

if (!aContent.flags) {
aContent.flags = {};
}
Expand All @@ -127,14 +129,14 @@ function saveContent(aModel, aContent, aAuthor, aFlags, aIsFlagging, aCallback)
aContent.flags.absolute = 0;
}

aContent.flags.critical += aFlags;
aContent.flags.critical += aWeight;

if (aIsFlagging) {
aContent.flags.absolute +=
(aFlags > 0 ? 1 : (aFlags < 0 && aContent.flags.absolute !== 0 ? -1 : 0));
(aWeight > 0 ? 1 : (aWeight < 0 && aContent.flags.absolute !== 0 ? -1 : 0));
}

if (aContent.flags.critical >= thresholds[aModel.modelName] * (aAuthor.role < 4 ? 2 : 1)) {
if (aContent.flags.critical >= thresholds[aModel.modelName] * weight) {
return getThreshold(aModel, aContent, aAuthor, function (aThreshold) {
aContent.flagged = aContent.flags.critical >= aThreshold;

Expand Down Expand Up @@ -168,6 +170,7 @@ function flag(aModel, aContent, aUser, aAuthor, aReason, aCallback) {
'model': aModel.modelName,
'_contentId': aContent._id,
'_userId': aUser._id,
'weight': aUser.role < 4 ? 2 : 1,
'reason': aReason,
'created': now
});
Expand All @@ -191,7 +194,7 @@ function flag(aModel, aContent, aUser, aAuthor, aReason, aCallback) {
aContent.flagged = false;
}

saveContent(aModel, aContent, aAuthor, aUser.role < 4 ? 2 : 1, true, aCallback);
saveContent(aModel, aContent, aAuthor, aFlag.weight, true, aCallback);
});
}

Expand Down Expand Up @@ -238,7 +241,7 @@ exports.unflag = function (aModel, aContent, aUser, aReason, aCallback) {
aFlag.remove(function (aErr) {
// WARNING: No err handling

saveContent(aModel, aContent, aAuthor, aUser.role < 4 ? -2 : -1, true, aCallback);
saveContent(aModel, aContent, aAuthor, -aFlag.weight, true, aCallback);
});
}

Expand Down
1 change: 1 addition & 0 deletions models/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var Schema = mongoose.Schema;
var flagSchema = new Schema({
model: String,
reason: String,
weight: Number, // Natural number with the role weight of User at time of flagging
_contentId: Schema.Types.ObjectId,
_userId: Schema.Types.ObjectId,

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.5.1",
"version": "0.5.2",
"main": "app",
"dependencies": {
"ace-builds": "1.4.12",
Expand Down

0 comments on commit 76b018e

Please sign in to comment.