mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-04 13:01:10 +08:00
added a result spacing check
if the timestamp of the last result + the time of the current result is greater than the current timestamp that most likely means the result is invalid, created manually or shouldnt be saved because of a bug
This commit is contained in:
parent
7da9afb557
commit
f953fe596a
1 changed files with 36 additions and 6 deletions
|
@ -124,18 +124,48 @@ class ResultController {
|
|||
// uid
|
||||
// );
|
||||
// return res.status(400).json({ message: "Time traveler detected" });
|
||||
|
||||
// this probably wont work if we replace the timestamp with the server time later
|
||||
// let timestampres = await ResultDAO.getResultByTimestamp(
|
||||
// uid,
|
||||
// result.timestamp
|
||||
// );
|
||||
// if (timestampres) {
|
||||
// return res.status(400).json({ message: "Duplicate result" });
|
||||
// }
|
||||
|
||||
let timestampres = await ResultDAO.getResultByTimestamp(
|
||||
uid,
|
||||
result.timestamp
|
||||
);
|
||||
if (timestampres) {
|
||||
return res.status(400).json({ message: "Duplicate result" });
|
||||
//convert result test duration to miliseconds
|
||||
const testDurationMilis = result.testDuration * 1000;
|
||||
//get latest result ordered by timestamp
|
||||
let lastResultTimestamp;
|
||||
try {
|
||||
lastResultTimestamp = (await ResultDAO.getLastResult(uid)).timestamp;
|
||||
} catch (e) {
|
||||
lastResultTimestamp = null;
|
||||
}
|
||||
|
||||
result.timestamp = Math.round(Date.now() / 1000) * 1000;
|
||||
|
||||
//check if its greater than server time - milis or result time - milis
|
||||
if (
|
||||
lastResultTimestamp &&
|
||||
(lastResultTimestamp + testDurationMilis > result.timestamp ||
|
||||
lastResultTimestamp + testDurationMilis >
|
||||
Math.round(Date.now() / 1000) * 1000)
|
||||
) {
|
||||
Logger.log(
|
||||
"invalid_result_spacing",
|
||||
{
|
||||
lastTimestamp: lastResultTimestamp,
|
||||
resultTime: result.timestamp,
|
||||
difference:
|
||||
lastResultTimestamp + testDurationMilis - result.timestamp,
|
||||
},
|
||||
uid
|
||||
);
|
||||
return res.status(400).json({ message: "Invalid result spacing" });
|
||||
}
|
||||
|
||||
try {
|
||||
result.keySpacingStats = {
|
||||
average:
|
||||
|
|
Loading…
Reference in a new issue