patches to reliability computation based on settings

This commit is contained in:
Wyatt Johnson
2018-05-23 16:18:22 -06:00
parent 921b85fba3
commit 975448ec69
8 changed files with 131 additions and 18 deletions
@@ -228,6 +228,14 @@ export const withUserDetailQuery = withQuery(
}
${getSlotFragmentSpreads(slots, 'user')}
}
settings {
karma {
comment {
reliable
unreliable
}
}
}
me {
id
}
+22 -7
View File
@@ -24,14 +24,23 @@ const userRoleFragment = gql`
}
`;
const toBoolean = value => {
if (value === 0) {
return null;
} else if (value > 0) {
/**
* calculateReliability will determine the reliability of a karma score based on
* the settings for the karma type.
*
* @param {Number} karma - the current karma value/score for the given user
* @param {Object} thresholds - the karma thresholds to base the karma computation on
*/
const calculateReliability = (karma, { reliable, unreliable }) => {
if (karma >= reliable) {
return true;
} else {
}
if (karma <= unreliable) {
return false;
}
return null;
};
export default {
@@ -312,7 +321,10 @@ export default {
user: {
reliable: {
commenter: {
$set: toBoolean(prev.user.reliable.commenterScore - 1),
$set: calculateReliability(
prev.user.reliable.commenterScore - 1,
prev.settings.karma.comment
),
},
commenterScore: {
$apply: count => count - 1,
@@ -328,7 +340,10 @@ export default {
user: {
reliable: {
commenter: {
$set: toBoolean(prev.user.reliable.commenterScore + 1),
$set: calculateReliability(
prev.user.reliable.commenterScore + 1,
prev.settings.karma.comment
),
},
commenterScore: {
$apply: count => count + 1,
+2
View File
@@ -15,6 +15,7 @@ const DontAgreeActionSummary = require('./dont_agree_action_summary');
const FlagAction = require('./flag_action');
const FlagActionSummary = require('./flag_action_summary');
const GenericUserError = require('./generic_user_error');
const KarmaThreshold = require('./karma_threshold');
const LocalUserProfile = require('./local_user_profile');
const RootMutation = require('./root_mutation');
const RootQuery = require('./root_query');
@@ -48,6 +49,7 @@ let resolvers = {
FlagAction,
FlagActionSummary,
GenericUserError,
KarmaThreshold,
LocalUserProfile,
RootMutation,
RootQuery,
+6
View File
@@ -0,0 +1,6 @@
const { property } = require('lodash');
module.exports = {
reliable: property('RELIABLE'),
unreliable: property('UNRELIABLE'),
};
+8 -2
View File
@@ -1,8 +1,13 @@
const { VIEW_PROTECTED_SETTINGS } = require('../../perms/constants');
const { decorateWithPermissionCheck } = require('./util');
const Settings = {};
const Settings = {
karma: (
settings,
args,
{ connectors: { services: { Karma: { THRESHOLDS } } } }
) => THRESHOLDS,
};
// PROTECTED_SETTINGS are the settings keys that must be protected for only some
// eyes.
@@ -11,6 +16,7 @@ const PROTECTED_SETTINGS = {
autoCloseStream: [VIEW_PROTECTED_SETTINGS],
wordlist: [VIEW_PROTECTED_SETTINGS],
domains: [VIEW_PROTECTED_SETTINGS],
karma: [VIEW_PROTECTED_SETTINGS],
};
// decorate the fields on the settings resolver with a permission check.
+26
View File
@@ -801,6 +801,29 @@ type Domains {
whitelist: [String!]!
}
# KarmaThreshold defines the bounds for which a User will become unreliable or
# reliable based on their karma score. If the score is equal or less than the
# unreliable value, they are unreliable. If the score is equal or more than the
# reliable value, they are reliable. If they are neither reliable or unreliable
# then they are neutral.
type KarmaThreshold {
reliable: Int!
unreliable: Int!
}
# KarmaThresholds contains the currently set thresholds for triggering Trust
# beheviour.
type KarmaThresholds {
# flag represents karma settings in relation to how well a User's flagging
# ability aligns with the moderation decicions made by moderators.
flag: KarmaThreshold!
# comment represents the karma setting in relation to how well a User's
# comments are moderated.
comment: KarmaThreshold!
}
# Settings stores the global settings for a given installation.
type Settings {
@@ -873,6 +896,9 @@ type Settings {
# domains will return a given list of domains.
domains: Domains
# karma contains the currently set thresholds for triggering Trust beheviour.
karma: KarmaThresholds
}
################################################################################
+6 -9
View File
@@ -115,18 +115,14 @@ class KarmaService {
/**
* Inspects the reliability of a property and returns it if known.
* @param {String} name - name of the property
* @param {Object} trust - object possibly containing the propertys
* @param {Object} trust - object possibly containing the properties
*/
static isReliable(name, trust) {
if (trust && trust[name]) {
if (trust[name].karma > THRESHOLDS[name].RELIABLE) {
return true;
} else if (trust[name].karma <= THRESHOLDS[name].UNRELIABLE) {
return false;
}
} else if (THRESHOLDS[name].RELIABLE < 0) {
const karma = get(trust, [name, 'karma'], 0);
if (karma >= THRESHOLDS[name].RELIABLE) {
return true;
} else if (THRESHOLDS[name].UNRELIABLE > 0) {
} else if (karma <= THRESHOLDS[name].UNRELIABLE) {
return false;
}
@@ -171,3 +167,4 @@ class KarmaService {
}
module.exports = KarmaService;
module.exports.THRESHOLDS = THRESHOLDS;
+53
View File
@@ -0,0 +1,53 @@
const chai = require('chai');
const { expect } = chai;
const { merge } = require('lodash');
const Karma = require('../../../services/karma');
const thresholdsBackup = {};
const thresholdsOverride = {
comment: {
RELIABLE: 1,
UNRELIABLE: -1,
},
flag: {
RELIABLE: 1,
UNRELIABLE: -1,
},
};
describe('services.Karma', () => {
before(() => {
// Backup the existing thresholds.
merge(thresholdsBackup, Karma.THRESHOLDS);
// Configure the thresholds to a known value.
merge(Karma.THRESHOLDS, thresholdsOverride);
expect(Karma.THRESHOLDS).to.deep.equal(thresholdsOverride);
});
after(() => {
// Restore the thresholds.
merge(Karma.THRESHOLDS, thresholdsBackup);
expect(Karma.THRESHOLDS).to.deep.equal(thresholdsBackup);
});
describe('#isReliable', () => {
it('neutral', () => {
expect(Karma.isReliable('comment', {})).to.be.null;
expect(Karma.isReliable('comment', { comment: {} })).to.be.null;
expect(Karma.isReliable('comment', { comment: { karma: 0 } })).to.be.null;
});
it('unreliable', () => {
expect(Karma.isReliable('comment', { comment: { karma: -1 } })).to.be
.false;
expect(Karma.isReliable('comment', { comment: { karma: -2 } })).to.be
.false;
});
it('reliable', () => {
expect(Karma.isReliable('comment', { comment: { karma: 1 } })).to.be.true;
expect(Karma.isReliable('comment', { comment: { karma: 2 } })).to.be.true;
});
});
});