diff --git a/src/utils/reputation.ts b/src/utils/reputation.ts index 61233e8..e1d5e40 100644 --- a/src/utils/reputation.ts +++ b/src/utils/reputation.ts @@ -33,7 +33,9 @@ export async function getReputation(userID: UserID): Promise { SUM(CASE WHEN "votes" > 0 AND NOT EXISTS ( SELECT * FROM "sponsorTimes" as c - WHERE c."videoID" = "a"."videoID" AND c."votes" > "a"."votes" LIMIT 1) + WHERE (c."votes" > "a"."votes" OR c."locked" > "a"."locked") AND + c."videoID" = "a"."videoID" AND + c."category" = "a"."category" LIMIT 1) AND EXISTS ( SELECT * FROM "lockCategories" as l WHERE l."videoID" = "a"."videoID" AND l."category" = "a"."category" LIMIT 1) @@ -42,7 +44,7 @@ export async function getReputation(userID: UserID): Promise { const result = await QueryCacher.get(fetchFromDB, reputationKey(userID)); - return calculateFromMetrics(result); + return calculateReputationFromMetrics(result); } // convert a number from one range to another. @@ -52,7 +54,7 @@ function convertRange(value: number, currentMin: number, currentMax: number, tar return ((value - currentMin) / currentRange) * targetRange + targetMin; } -export function calculateFromMetrics(metrics: ReputationDBResult): number { +export function calculateReputationFromMetrics(metrics: ReputationDBResult): number { // Grace period if (metrics.totalSubmissions < 5) { return 0; diff --git a/test/cases/reputation.ts b/test/cases/reputation.ts index dd41d29..4b33e59 100644 --- a/test/cases/reputation.ts +++ b/test/cases/reputation.ts @@ -2,7 +2,7 @@ import assert from "assert"; import { db } from "../../src/databases/databases"; import { UserID } from "../../src/types/user.model"; import { getHash } from "../../src/utils/getHash"; -import { getReputation, calculateFromMetrics } from "../../src/utils/reputation"; +import { getReputation, calculateReputationFromMetrics } from "../../src/utils/reputation"; const userIDLowSubmissions = "reputation-lowsubmissions" as UserID; const userIDHighDownvotes = "reputation-highdownvotes" as UserID; @@ -130,7 +130,7 @@ describe("reputation", () => { mostUpvotedInLockedVideoSum: 0 }; - assert.strictEqual(await getReputation(getHash(userIDHighDownvotes)), calculateFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHighDownvotes)), calculateReputationFromMetrics(metrics)); }); it("user with high non self downvote ratio", async () => { @@ -145,7 +145,7 @@ describe("reputation", () => { oldUpvotedSubmissions: 1, mostUpvotedInLockedVideoSum: 0 }; - assert.strictEqual(await getReputation(getHash(userIDHighNonSelfDownvotes)), calculateFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHighNonSelfDownvotes)), calculateReputationFromMetrics(metrics)); }); it("user with mostly new submissions", async () => { @@ -161,7 +161,6 @@ describe("reputation", () => { }); it("user with high reputation", async () => { - // 0.19310344827586207 const metrics = { totalSubmissions: 8, downvotedSubmissions: 1, @@ -172,12 +171,11 @@ describe("reputation", () => { oldUpvotedSubmissions: 5, mostUpvotedInLockedVideoSum: 0 }; - - assert.strictEqual(await getReputation(getHash(userIDHighRep)), calculateFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHighRep)), calculateReputationFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHighRep)), 0.19310344827586207); }); it("user with high reputation and locked segments", async () => { - // 1.793103448275862 const metrics = { totalSubmissions: 8, downvotedSubmissions: 1, @@ -188,11 +186,11 @@ describe("reputation", () => { oldUpvotedSubmissions: 5, mostUpvotedInLockedVideoSum: 0 }; - assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), calculateFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), calculateReputationFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), 1.793103448275862); }); it("user with most upvoted segments in locked video", async () => { - // 6.158620689655172 const metrics = { totalSubmissions: 10, downvotedSubmissions: 1, @@ -203,7 +201,8 @@ describe("reputation", () => { oldUpvotedSubmissions: 6, mostUpvotedInLockedVideoSum: 2 }; - assert.strictEqual(await getReputation(getHash(userIDHaveMostUpvotedInLockedVideo)), calculateFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHaveMostUpvotedInLockedVideo)), calculateReputationFromMetrics(metrics)); + assert.strictEqual(await getReputation(getHash(userIDHaveMostUpvotedInLockedVideo)), 6.158620689655172); }); });