Skip to content
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

Save role weight in flag model #1772

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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