diff --git a/src/utils/redisKeys.ts b/src/utils/redisKeys.ts index fe37691..fb744c3 100644 --- a/src/utils/redisKeys.ts +++ b/src/utils/redisKeys.ts @@ -14,5 +14,5 @@ export function skipSegmentsHashKey(hashedVideoIDPrefix: VideoIDHash, service: S } export function reputationKey(userID: UserID): string { - return "reputation.user." + userID; + return "reputation.user.v2." + userID; } \ No newline at end of file diff --git a/src/utils/reputation.ts b/src/utils/reputation.ts index 3fe42c1..3d15cd8 100644 --- a/src/utils/reputation.ts +++ b/src/utils/reputation.ts @@ -7,12 +7,14 @@ interface ReputationDBResult { totalSubmissions: number, downvotedSubmissions: number, nonSelfDownvotedSubmissions: number, - upvotedSum: number, + votedSum: number, lockedSum: number, + semiOldUpvotedSubmissions: number, oldUpvotedSubmissions: number } export async function getReputation(userID: UserID): Promise { + const weekAgo = Date.now() - 1000 * 60 * 60 * 24 * 45; // 45 days ago const pastDate = Date.now() - 1000 * 60 * 60 * 24 * 45; // 45 days ago // 1596240000000 is August 1st 2020, a little after auto upvote was disabled const fetchFromDB = () => db.prepare("get", @@ -23,10 +25,11 @@ export async function getReputation(userID: UserID): Promise { WHERE b."userID" = ? AND b."votes" > 0 AND b."category" = "a"."category" AND b."videoID" = "a"."videoID" LIMIT 1) THEN 1 ELSE 0 END) AS "nonSelfDownvotedSubmissions", - SUM(CASE WHEN "votes" > 0 AND "timeSubmitted" > 1596240000000 THEN "votes" ELSE 0 END) AS "upvotedSum", + SUM(CASE WHEN "timeSubmitted" > 1596240000000 THEN "votes" ELSE 0 END) AS "votedSum", SUM(locked) AS "lockedSum", + SUM(CASE WHEN "timeSubmitted" < ? AND "timeSubmitted" > 1596240000000 AND "votes" > 0 THEN 1 ELSE 0 END) AS "semiOldUpvotedSubmissions", SUM(CASE WHEN "timeSubmitted" < ? AND "timeSubmitted" > 1596240000000 AND "votes" > 0 THEN 1 ELSE 0 END) AS "oldUpvotedSubmissions" - FROM "sponsorTimes" as "a" WHERE "userID" = ?`, [userID, pastDate, userID]) as Promise; + FROM "sponsorTimes" as "a" WHERE "userID" = ?`, [userID, weekAgo, pastDate, userID]) as Promise; const result = await QueryCacher.get(fetchFromDB, reputationKey(userID)); @@ -45,11 +48,19 @@ export async function getReputation(userID: UserID): Promise { return convertRange(Math.min(nonSelfDownvoteRatio, 0.4), 0.05, 0.4, -0.5, -2.5); } - if (result.oldUpvotedSubmissions < 3 || result.upvotedSum < 5) { + if (result.votedSum < 5) { return 0; } - return convertRange(Math.min(result.upvotedSum, 150), 5, 150, 0, 7) + convertRange(Math.min(result.lockedSum ?? 0, 50), 0, 50, 0, 20); + if (result.oldUpvotedSubmissions < 3) { + if (result.semiOldUpvotedSubmissions > 3) { + return convertRange(Math.min(result.votedSum, 150), 5, 150, 0, 2) + convertRange(Math.min(result.lockedSum ?? 0, 50), 0, 50, 0, 5); + } else { + return 0; + } + } + + return convertRange(Math.min(result.votedSum, 150), 5, 150, 0, 7) + convertRange(Math.min(result.lockedSum ?? 0, 50), 0, 50, 0, 20); } function convertRange(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number { diff --git a/test/cases/reputation.ts b/test/cases/reputation.ts index e6029e2..de4d123 100644 --- a/test/cases/reputation.ts +++ b/test/cases/reputation.ts @@ -114,11 +114,11 @@ describe('reputation', () => { }); it("user with high reputation", async () => { - assert.strictEqual(await getReputation(getHash(userIDHighRep)), 0.24137931034482757); + assert.strictEqual(await getReputation(getHash(userIDHighRep)), 0.19310344827586207); }); it("user with high reputation and locked segments", async () => { - assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), 1.8413793103448277); + assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), 1.793103448275862); }); });