From cfcb6c6b64c214fa84e9dccbc6db1c869954dfcb Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 23 May 2021 16:53:35 -0400 Subject: [PATCH] Add reputation system --- src/middleware/queryCacher.ts | 2 +- src/middleware/redisKeys.ts | 7 +++++- src/middleware/reputation.ts | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/middleware/reputation.ts diff --git a/src/middleware/queryCacher.ts b/src/middleware/queryCacher.ts index e1f9f09..ec48114 100644 --- a/src/middleware/queryCacher.ts +++ b/src/middleware/queryCacher.ts @@ -3,7 +3,7 @@ import { Logger } from "../utils/logger"; import { skipSegmentsHashKey, skipSegmentsKey } from "./redisKeys"; import { Service, VideoID, VideoIDHash } from "../types/segments.model"; -async function get(fetchFromDB: () => Promise, key: string): Promise { +async function get(fetchFromDB: () => Promise, key: string): Promise { const {err, reply} = await redis.getAsync(key); if (!err && reply) { diff --git a/src/middleware/redisKeys.ts b/src/middleware/redisKeys.ts index 920b2da..5234a51 100644 --- a/src/middleware/redisKeys.ts +++ b/src/middleware/redisKeys.ts @@ -1,4 +1,5 @@ import { Service, VideoID, VideoIDHash } from "../types/segments.model"; +import { UserID } from "../types/user.model"; import { Logger } from "../utils/logger"; export function skipSegmentsKey(videoID: VideoID, service: Service): string { @@ -10,4 +11,8 @@ export function skipSegmentsHashKey(hashedVideoIDPrefix: VideoIDHash, service: S if (hashedVideoIDPrefix.length !== 4) Logger.warn("Redis skip segment hash-prefix key is not length 4! " + hashedVideoIDPrefix); return "segments." + service + "." + hashedVideoIDPrefix; -} \ No newline at end of file +} + +export function userKey(userID: UserID): string { + return "user." + userID; +} \ No newline at end of file diff --git a/src/middleware/reputation.ts b/src/middleware/reputation.ts new file mode 100644 index 0000000..99bda65 --- /dev/null +++ b/src/middleware/reputation.ts @@ -0,0 +1,45 @@ +import { db } from "../databases/databases"; +import { UserID } from "../types/user.model"; +import { QueryCacher } from "./queryCacher"; +import { userKey } from "./redisKeys"; + +interface ReputationDBResult { + totalSubmissions: number, + downvotedSubmissions: number, + upvotedSum: number, + oldUpvotedSubmissions: number +} + +export async function getReputation(userID: UserID) { + const pastDate = Date.now() - 1000 * 1000 * 60 * 60 * 24 * 45; // 45 days ago + const fetchFromDB = () => db.prepare("get", + `SELECT COUNT(*) AS "totalSubmissions", + SUM(CASE WHEN "votes" < 0 THEN 1 ELSE 0 END) AS "downvotedSubmissions", + SUM(CASE WHEN "votes" > 0 THEN "votes" ELSE 0 END) AS "upvotedSum", + SUM(CASE WHEN "timeSubmitted" < ? AND "votes" > 0 THEN 1 ELSE 0 END) AS "oldUpvotedSubmissions" + FROM "sponsorTimes" WHERE "userID" = ?`, [pastDate, userID]) as Promise; + + const result = await QueryCacher.get(fetchFromDB, userKey(userID)); + + // Grace period + if (result.totalSubmissions < 5) { + return 0; + } + + const downvoteRatio = result.downvotedSubmissions / result.totalSubmissions; + if (downvoteRatio > 0.3) { + return convertRange(downvoteRatio, 0.3, 1, -0.5, -1.5); + } + + if (result.oldUpvotedSubmissions < 3 || result.upvotedSum < 5) { + return 0 + } + + return convertRange(Math.min(result.upvotedSum, 50), 5, 50, 0, 15); +} + +function convertRange(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number { + const currentRange = currentMax - currentMin; + const targetRange = targetMax - targetMin; + return ((value - currentMin) / currentRange) * targetRange + targetMin; +} \ No newline at end of file