add maxRewardTime

This commit is contained in:
Michael C 2021-08-06 00:47:35 -04:00
parent 5ad7c6a3ba
commit 58551ba37f
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
2 changed files with 46 additions and 13 deletions

View file

@ -6,21 +6,28 @@ import {Logger} from "../utils/logger";
import { HashedUserID, UserID } from "../types/user.model"; import { HashedUserID, UserID } from "../types/user.model";
import { getReputation } from "../utils/reputation"; import { getReputation } from "../utils/reputation";
import { SegmentUUID } from "../types/segments.model"; import { SegmentUUID } from "../types/segments.model";
import {config} from "../config";
const maxRewardTime = config.maxRewardTimePerSegmentInSeconds;
async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ minutesSaved: number, segmentCount: number }> { async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ minutesSaved: number, segmentCount: number }> {
const row = await db.prepare("get", `SELECT SUM((("endTime" - "startTime") / 60) * "views") as "minutesSaved", try {
count(*) as "segmentCount" FROM "sponsorTimes" const row = await db.prepare("get",
WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]); `SELECT SUM(((CASE WHEN "endTime" - "startTime" > ? THEN ? ELSE "endTime" - "startTime" END) / 60) * "views") as "minutesSaved",
if (row.minutesSaved != null) { count(*) as "segmentCount" FROM "sponsorTimes"
return { WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [maxRewardTime, maxRewardTime, userID]);
minutesSaved: row.minutesSaved, if (row.minutesSaved != null) {
segmentCount: row.segmentCount, return {
}; minutesSaved: row.minutesSaved,
} else { segmentCount: row.segmentCount,
return { };
minutesSaved: 0, } else {
segmentCount: 0, return {
}; minutesSaved: 0,
segmentCount: 0,
};
}
} catch (err) {
return null;
} }
} }

View file

@ -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, ["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, "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", 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 (?, ?, ?, ?, ?)'; 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"]); 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)); .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));
});
}); });