mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-11-08 05:03:39 +08:00
Fix quote ratings (#2586) by Bruception
* Fix quote ratings * Reorder logic * Update message * Fix consistency
This commit is contained in:
parent
4daf76e7d9
commit
6b1e9efe73
5 changed files with 64 additions and 64 deletions
|
|
@ -1,54 +0,0 @@
|
|||
import MonkeyError from "../../handlers/error";
|
||||
import UserDAO from "../../dao/user";
|
||||
import QuoteRatingsDAO from "../../dao/quote-ratings";
|
||||
import { MonkeyResponse } from "../../handlers/monkey-response";
|
||||
|
||||
class QuoteRatingsController {
|
||||
static async getRating(req, _res) {
|
||||
const { quoteId, language } = req.query;
|
||||
const data = await QuoteRatingsDAO.get(parseInt(quoteId), language);
|
||||
return new MonkeyResponse("Rating retrieved", data);
|
||||
}
|
||||
|
||||
static async submitRating(req, _res) {
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
let { quoteId, rating, language } = req.body;
|
||||
|
||||
quoteId = parseInt(quoteId);
|
||||
rating = Math.round(parseInt(rating));
|
||||
|
||||
//check if user already submitted a rating
|
||||
const user = await UserDAO.getUser(uid);
|
||||
|
||||
if (!user) {
|
||||
throw new MonkeyError(401, "User not found.");
|
||||
}
|
||||
let quoteRatings = user.quoteRatings;
|
||||
|
||||
if (quoteRatings === undefined) quoteRatings = {};
|
||||
if (quoteRatings[language] === undefined) quoteRatings[language] = {};
|
||||
if (quoteRatings[language][quoteId] == undefined)
|
||||
quoteRatings[language][quoteId] = undefined;
|
||||
|
||||
const quoteRating = quoteRatings[language][quoteId];
|
||||
|
||||
let newRating;
|
||||
let update;
|
||||
if (quoteRating) {
|
||||
//user already voted for this
|
||||
newRating = rating - quoteRating;
|
||||
update = true;
|
||||
} else {
|
||||
//user has not voted for this
|
||||
newRating = rating;
|
||||
update = false;
|
||||
}
|
||||
|
||||
await QuoteRatingsDAO.submit(quoteId, language, newRating, update);
|
||||
quoteRatings[language][quoteId] = rating;
|
||||
await UserDAO.updateQuoteRatings(uid, quoteRatings);
|
||||
return new MonkeyResponse("Rating updated");
|
||||
}
|
||||
}
|
||||
|
||||
export default QuoteRatingsController;
|
||||
|
|
@ -1,5 +1,8 @@
|
|||
import _ from "lodash";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import UserDAO from "../../dao/user";
|
||||
import ReportDAO from "../../dao/report";
|
||||
import QuoteRatingsDAO from "../../dao/quote-ratings";
|
||||
import UsersDAO from "../../dao/user";
|
||||
import MonkeyError from "../../handlers/error";
|
||||
import { verify } from "../../handlers/captcha";
|
||||
|
|
@ -7,6 +10,57 @@ import Logger from "../../handlers/logger";
|
|||
import { MonkeyResponse } from "../../handlers/monkey-response";
|
||||
|
||||
class QuotesController {
|
||||
static async getRating(req: MonkeyTypes.Request): Promise<MonkeyResponse> {
|
||||
const { quoteId, language } = req.query;
|
||||
|
||||
const data = await QuoteRatingsDAO.get(
|
||||
parseInt(quoteId as string),
|
||||
language
|
||||
);
|
||||
|
||||
return new MonkeyResponse("Rating retrieved", data);
|
||||
}
|
||||
|
||||
static async submitRating(req: MonkeyTypes.Request): Promise<MonkeyResponse> {
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
const { quoteId, rating, language } = req.body;
|
||||
|
||||
const user = await UserDAO.getUser(uid);
|
||||
if (!user) {
|
||||
throw new MonkeyError(401, "User not found.");
|
||||
}
|
||||
|
||||
const normalizedQuoteId = parseInt(quoteId as string);
|
||||
const normalizedRating = Math.round(parseInt(rating as string));
|
||||
|
||||
const userQuoteRatings = user.quoteRatings ?? {};
|
||||
const currentRating = userQuoteRatings[language]?.[normalizedQuoteId] ?? 0;
|
||||
|
||||
const newRating = normalizedRating - currentRating;
|
||||
const shouldUpdateRating = currentRating !== 0;
|
||||
|
||||
await QuoteRatingsDAO.submit(
|
||||
quoteId,
|
||||
language,
|
||||
newRating,
|
||||
shouldUpdateRating
|
||||
);
|
||||
|
||||
_.setWith(
|
||||
userQuoteRatings,
|
||||
`[${language}][${normalizedQuoteId}]`,
|
||||
normalizedRating,
|
||||
Object
|
||||
);
|
||||
|
||||
await UserDAO.updateQuoteRatings(uid, userQuoteRatings);
|
||||
|
||||
const responseMessage = `Rating ${
|
||||
shouldUpdateRating ? "updated" : "submitted"
|
||||
}`;
|
||||
return new MonkeyResponse(responseMessage);
|
||||
}
|
||||
|
||||
static async reportQuote(req: MonkeyTypes.Request): Promise<MonkeyResponse> {
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
const {
|
||||
|
|
@ -43,7 +97,7 @@ class QuotesController {
|
|||
details: newReport.details,
|
||||
});
|
||||
|
||||
return new MonkeyResponse("Quote reported successfully");
|
||||
return new MonkeyResponse("Quote reported");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import joi from "joi";
|
|||
import { authenticateRequest } from "../../middlewares/auth";
|
||||
import { Router } from "express";
|
||||
import NewQuotesController from "../controllers/new-quotes";
|
||||
import QuoteRatingsController from "../controllers/quote-ratings";
|
||||
import QuotesController from "../controllers/quotes";
|
||||
import * as RateLimit from "../../middlewares/rate-limit";
|
||||
import {
|
||||
|
|
@ -81,7 +80,7 @@ quotesRouter.get(
|
|||
language: joi.string().required(),
|
||||
},
|
||||
}),
|
||||
asyncHandler(QuoteRatingsController.getRating)
|
||||
asyncHandler(QuotesController.getRating)
|
||||
);
|
||||
|
||||
quotesRouter.post(
|
||||
|
|
@ -95,7 +94,7 @@ quotesRouter.post(
|
|||
language: joi.string().required(),
|
||||
},
|
||||
}),
|
||||
asyncHandler(QuoteRatingsController.submitRating)
|
||||
asyncHandler(QuotesController.submitRating)
|
||||
);
|
||||
|
||||
quotesRouter.post(
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ async function errorHandlingMiddleware(
|
|||
}
|
||||
} else {
|
||||
console.error(error.message);
|
||||
console.error(error.stack);
|
||||
}
|
||||
|
||||
return handleMonkeyResponse(monkeyResponse, res);
|
||||
|
|
|
|||
|
|
@ -164,11 +164,8 @@ async function submit(): Promise<void> {
|
|||
);
|
||||
}
|
||||
|
||||
const quoteRatings = DB.getSnapshot().quoteRatings;
|
||||
|
||||
if (quoteRatings === undefined) {
|
||||
return;
|
||||
}
|
||||
const snapshot = DB.getSnapshot();
|
||||
const quoteRatings = snapshot.quoteRatings ?? {};
|
||||
|
||||
if (quoteRatings?.[currentQuote.language]?.[currentQuote.id]) {
|
||||
const oldRating = quoteRatings[currentQuote.language][currentQuote.id];
|
||||
|
|
@ -184,7 +181,7 @@ async function submit(): Promise<void> {
|
|||
} as QuoteStats;
|
||||
Notifications.add("Rating updated", 1);
|
||||
} else {
|
||||
if (quoteRatings[currentQuote.language] === undefined) {
|
||||
if (!quoteRatings[currentQuote.language]) {
|
||||
quoteRatings[currentQuote.language] = {};
|
||||
}
|
||||
quoteRatings[currentQuote.language][currentQuote.id] = rating;
|
||||
|
|
@ -202,6 +199,9 @@ async function submit(): Promise<void> {
|
|||
Notifications.add("Rating submitted", 1);
|
||||
}
|
||||
|
||||
snapshot.quoteRatings = quoteRatings;
|
||||
DB.setSnapshot(snapshot);
|
||||
|
||||
quoteStats.average = getRatingAverage(quoteStats);
|
||||
$(".pageTest #result #rateQuoteButton .rating").text(
|
||||
quoteStats.average?.toFixed(1)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue