From fe38ca296a5d9f288208b164ac7eea3780444efa Mon Sep 17 00:00:00 2001 From: Miodec Date: Tue, 6 Jul 2021 15:22:05 +0100 Subject: [PATCH] converted result getting --- backend/api/controllers/result.js | 83 ++++++++++++++++++++++--------- backend/api/routes/result.js | 9 ++++ backend/dao/result.js | 14 +++--- backend/server.js | 2 + src/js/db.js | 16 +++--- 5 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 backend/api/routes/result.js diff --git a/backend/api/controllers/result.js b/backend/api/controllers/result.js index 79ebf15ef..6e9e2bea0 100644 --- a/backend/api/controllers/result.js +++ b/backend/api/controllers/result.js @@ -1,15 +1,29 @@ -import ResultDAO from "../../dao/result"; -import UserDAO from "../../dao/user"; -import PublicStatsDAO from "../../dao/public-stats"; -import { validateObjectValues, validateResult } from "../../handlers/validation"; -import { stdDev, roundTo2 } from "../../handlers/misc"; +const ResultDAO = require("../../dao/result"); +const UserDAO = require("../../dao/user"); +const PublicStatsDAO = require("../../dao/public-stats"); +const { + validateObjectValues, + validateResult, +} = require("../../handlers/validation"); +const { stdDev, roundTo2 } = require("../../handlers/misc"); class ResultController { - static async addResult(req, res, next){ + static async getResults(req, res, next) { + try { + const { uid } = req.decodedToken; + const results = await ResultDAO.getResults(uid); + return res.status(200).json(results); + } catch (e) { + next(e); + } + } + + static async addResult(req, res, next) { try { const { uid } = req.decodedToken; let result = req.result; - if(!validateObjectValues(result)) return res.sendStatus(400).json({message: "Bad input"}); + if (!validateObjectValues(result)) + return res.sendStatus(400).json({ message: "Bad input" }); if ( result.wpm <= 0 || result.wpm > 350 || @@ -17,13 +31,17 @@ class ResultController { result.acc > 100 || result.consistency > 100 ) { - return res.sendStatus(400).json({message: "Bad input"}); + return res.sendStatus(400).json({ message: "Bad input" }); } if ( (result.mode === "time" && result.mode2 < 15 && result.mode2 > 0) || - (result.mode === "time" && result.mode2 == 0 && result.testDuration < 15) || + (result.mode === "time" && + result.mode2 == 0 && + result.testDuration < 15) || (result.mode === "words" && result.mode2 < 10 && result.mode2 > 0) || - (result.mode === "words" && result.mode2 == 0 && result.testDuration < 15) || + (result.mode === "words" && + result.mode2 == 0 && + result.testDuration < 15) || (result.mode === "custom" && result.customText !== undefined && !result.customText.isWordRandom && @@ -40,10 +58,12 @@ class ResultController { result.customText.isTimeRandom && result.customText.time < 15) ) { - return res.sendStatus(400).json({message: "Test too short"}); + return res.sendStatus(400).json({ message: "Test too short" }); } if (!validateResult(result)) { - return res.sendStatus(400).json({message: "Result data doesn't make sense"}); + return res + .sendStatus(400) + .json({ message: "Result data doesn't make sense" }); } result.keySpacingStats = { @@ -66,27 +86,39 @@ class ResultController { result.name = user.name; //check keyspacing and duration here for bots - if (result.mode === "time" && result.wpm > 130 && result.testDuration < 122) { + if ( + result.mode === "time" && + result.wpm > 130 && + result.testDuration < 122 + ) { if (user.verified === false || user.verified === undefined) { - if (result.keySpacingStats !== null && result.keyDurationStats !== null) { + if ( + result.keySpacingStats !== null && + result.keyDurationStats !== null + ) { if ( result.keySpacingStats.sd <= 15 || result.keyDurationStats.sd <= 10 || result.keyDurationStats.average < 15 || (result.wpm > 200 && result.consistency < 70) ) { - //possible bot - return res.sendStatus(400).json({message: "Possible bot detected"}); + //possible bot + return res + .sendStatus(400) + .json({ message: "Possible bot detected" }); } if ( - (result.keySpacingStats.sd > 15 && result.keySpacingStats.sd <= 25) || - (result.keyDurationStats.sd > 10 && result.keyDurationStats.sd <= 15) || - (result.keyDurationStats.average > 15 && result.keyDurationStats.average <= 20) + (result.keySpacingStats.sd > 15 && + result.keySpacingStats.sd <= 25) || + (result.keyDurationStats.sd > 10 && + result.keyDurationStats.sd <= 15) || + (result.keyDurationStats.average > 15 && + result.keyDurationStats.average <= 20) ) { //close to the bot detection threshold } } else { - return res.sendStatus(400).json({message: "Missing key data"}); + return res.sendStatus(400).json({ message: "Missing key data" }); } } } @@ -94,7 +126,9 @@ class ResultController { delete result.keySpacing; delete result.keyDuration; - result.keyDurationStats.average = roundTo2(result.keyDurationStats.average); + result.keyDurationStats.average = roundTo2( + result.keyDurationStats.average + ); result.keyDurationStats.sd = roundTo2(result.keyDurationStats.sd); result.keySpacingStats.average = roundTo2(result.keySpacingStats.average); result.keySpacingStats.sd = roundTo2(result.keySpacingStats.sd); @@ -103,7 +137,7 @@ class ResultController { const tagPbs = await UserDAO.checkIfTagPb(uid, result); if (result.mode === "time" && String(result.mode2) === "60") { - UserDAO.incrementBananas(uid,result.wpm); + UserDAO.incrementBananas(uid, result.wpm); } let tt = 0; @@ -117,8 +151,9 @@ class ResultController { await ResultDAO.addResult(uid, result); - return res.sendStatus(200).json({message: "Result saved", isPb, name, tagPbs}); - + return res + .sendStatus(200) + .json({ message: "Result saved", isPb, name, tagPbs }); } catch (e) { next(e); } diff --git a/backend/api/routes/result.js b/backend/api/routes/result.js new file mode 100644 index 000000000..de712ef0a --- /dev/null +++ b/backend/api/routes/result.js @@ -0,0 +1,9 @@ +const { authenticateRequest } = require("../../middlewares/auth"); +const { Router } = require("express"); +const ResultController = require("../controllers/result"); + +const router = Router(); + +router.get("/", authenticateRequest, ResultController.getResults); + +module.exports = router; diff --git a/backend/dao/result.js b/backend/dao/result.js index c255020d9..d8a13078a 100644 --- a/backend/dao/result.js +++ b/backend/dao/result.js @@ -5,9 +5,9 @@ const UserDAO = require("./user"); class ResultDAO { static async addResult(uid, result) { let user; - try{ + try { user = await UserDAO.getUser(uid); - }catch(e){ + } catch (e) { user = null; } if (!user) throw new MonkeyError(404, "User not found"); @@ -20,10 +20,11 @@ class ResultDAO { if (!result) throw new MonkeyError(404, "Result not found"); const userTags = await UserDAO.getTags(uid); let validTags = true; - tags.forEach(tagId => { - if(!userTags.includes(tagId)) validTags = false; + tags.forEach((tagId) => { + if (!userTags.includes(tagId)) validTags = false; }); - if (!validTags) throw new MonkeyError(400, "One of the tag id's is not vaild"); + if (!validTags) + throw new MonkeyError(400, "One of the tag id's is not vaild"); return await mongoDB() .collection("results") .updateOne({ id, uid }, { $set: { tags } }); @@ -43,7 +44,8 @@ class ResultDAO { .find({ uid }) .sort({ timestamp: -1 }) .skip(start) - .limit(end); // this needs to be changed to later take patreon into consideration + .limit(end) + .toArray(); // this needs to be changed to later take patreon into consideration if (!result) throw new MonkeyError(404, "Result not found"); return result; } diff --git a/backend/server.js b/backend/server.js index 9d4d9fee5..0ea055ef5 100644 --- a/backend/server.js +++ b/backend/server.js @@ -21,6 +21,8 @@ const userRouter = require("./api/routes/user"); app.use("/user", userRouter); const configRouter = require("./api/routes/config"); app.use("/config", configRouter); +const resultRouter = require("./api/routes/result"); +app.use("/result", resultRouter); app.use(function (e, req, res, next) { console.log("Error", e); diff --git a/src/js/db.js b/src/js/db.js index 3fdcddc3c..9b9f99467 100644 --- a/src/js/db.js +++ b/src/js/db.js @@ -90,14 +90,14 @@ export async function getUserResults() { if (dbSnapshot.results !== undefined) { return true; } else { - axiosInstance - .get("/userResults") - .then((response) => { - dbSnapshot.results = response.data.results; - }) - .catch((error) => { - console.log(error); - }); + try { + let results = await axiosInstance.get("/result"); + dbSnapshot.results = results.data; + return true; + } catch (e) { + Notifications.add("Error getting results", -1); + return false; + } } /* try {