diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index 594ef57..b42d58a 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -6,21 +6,28 @@ import {Logger} from "../utils/logger"; import { HashedUserID, UserID } from "../types/user.model"; import { getReputation } from "../utils/reputation"; import { SegmentUUID } from "../types/segments.model"; +import {config} from "../config"; +const maxRewardTime = config.maxRewardTimePerSegmentInSeconds; async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ minutesSaved: number, segmentCount: number }> { - const row = await db.prepare("get", `SELECT SUM((("endTime" - "startTime") / 60) * "views") as "minutesSaved", - count(*) as "segmentCount" FROM "sponsorTimes" - WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]); - if (row.minutesSaved != null) { - return { - minutesSaved: row.minutesSaved, - segmentCount: row.segmentCount, - }; - } else { - return { - minutesSaved: 0, - segmentCount: 0, - }; + try { + const row = await db.prepare("get", + `SELECT SUM(((CASE WHEN "endTime" - "startTime" > ? THEN ? ELSE "endTime" - "startTime" END) / 60) * "views") as "minutesSaved", + count(*) as "segmentCount" FROM "sponsorTimes" + WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [maxRewardTime, maxRewardTime, userID]); + if (row.minutesSaved != null) { + return { + minutesSaved: row.minutesSaved, + segmentCount: row.segmentCount, + }; + } else { + return { + minutesSaved: 0, + segmentCount: 0, + }; + } + } catch (err) { + return null; } } diff --git a/test/cases/getUserInfo.ts b/test/cases/getUserInfo.ts index c48d734..e0a4d65 100644 --- a/test/cases/getUserInfo.ts +++ b/test/cases/getUserInfo.ts @@ -18,6 +18,7 @@ describe("getUserInfo", () => { await db.prepare("run", sponsorTimesQuery, ["zzzxxxyyy", 1, 11, 2, "uuid000006", getHash("getuserinfo_user_02"), 6, 10, "sponsor", 0]); await db.prepare("run", sponsorTimesQuery, ["xxxyyyzzz", 1, 11, 2, "uuid000007", getHash("getuserinfo_user_02"), 7, 10, "sponsor", 1]); await db.prepare("run", sponsorTimesQuery, ["xxxyyyzzz", 1, 11, 2, "uuid000008", getHash("getuserinfo_user_02"), 8, 10, "sponsor", 1]); + await db.prepare("run", sponsorTimesQuery, ["xxxyyyzzz", 0, 36000, 2, "uuid000009", getHash("getuserinfo_user_03"), 8, 2, "sponsor", 0]); const insertWarningQuery = 'INSERT INTO warnings ("userID", "issueTime", "issuerUserID", "enabled", "reason") VALUES (?, ?, ?, ?, ?)'; await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_0"), 10, "getuserinfo_vip", 1, "warning0-0"]); @@ -294,4 +295,29 @@ describe("getUserInfo", () => { }) .catch(err => done(err)); }); + + it("Should only count long segments as 10 minutes", (done: Done) => { + fetch(`${getbaseURL()}/api/userInfo?userID=getuserinfo_user_03`) + .then(async res => { + assert.strictEqual(res.status, 200); + const expected = { + userName: "807e0a5d0a62c9c4365fae3d403e4618a3226f231314a898fa1555a0e55eab9e", + userID: "807e0a5d0a62c9c4365fae3d403e4618a3226f231314a898fa1555a0e55eab9e", + minutesSaved: 20, + viewCount: 2, + ignoredViewCount: 0, + segmentCount: 1, + ignoredSegmentCount: 0, + reputation: 0, + lastSegmentID: "uuid000009", + vip: false, + warnings: 0, + warningReason: "" + }; + const data = await res.json(); + assert.deepStrictEqual(data, expected); + done(); + }) + .catch(err => done(err)); + }); });