mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-05 13:27:49 +08:00
* added new-quotes get and add routes * added new-quotes/approve route to api * Added front-end for quote submission * fix language typo, force lowercase language * Check for duplicate quote * added unknown language handling to quote-submit * npm i * npm i * language dropdown instead of input field * mouse down instead click * styling changes * moved quotemod check to the controller, added refuse endpoint, added ability to approve and edit * limiting quotes * added log to quote approve * added approve quotes button * typo * using the correct function, lowered limit * added quote approve popup * getting quotes and showing them * error checking * error checking * removed console log * stylng changes, added refresh list button * added disabled class * styling changes * handling button clicks showing langauge and timestamp * check if user is a quote mod * approve button handling * handling quote edit * showing short quotes in red * post, not get * not parsing quote id to int * parsing to objectid * updated styling * fixed lint warnings * fixed up new quotes dao * showing loader * calling function correctly * typo fix * fixed wrong path, fix broken newid, saving file when file exists, searching for the quote correctly when attempting to delete * showing message * blocking buttons and inputs when waiting for response, unlocking after response * deleted workflow Co-authored-by: lukew3 <lukew25073@gmail.com>
131 lines
3.8 KiB
JavaScript
131 lines
3.8 KiB
JavaScript
const MonkeyError = require("../handlers/error");
|
|
const { mongoDB } = require("../init/mongodb");
|
|
const fs = require("fs");
|
|
const simpleGit = require("simple-git");
|
|
const path = require("path");
|
|
const git = simpleGit(path.join(__dirname, "../../../monkeytype-new-quotes"));
|
|
const stringSimilarity = require("string-similarity");
|
|
const { ObjectID } = require("mongodb");
|
|
|
|
class NewQuotesDAO {
|
|
static async add(text, source, language, uid) {
|
|
let quote = {
|
|
text: text,
|
|
source: source,
|
|
language: language.toLowerCase(),
|
|
submittedBy: uid,
|
|
timestamp: Date.now(),
|
|
approved: false,
|
|
};
|
|
//check for duplicate first
|
|
const fileDir = path.join(
|
|
__dirname,
|
|
`../../../monkeytype-new-quotes/static/quotes/${language}.json`
|
|
);
|
|
let duplicateId = -1;
|
|
let similarityScore = -1;
|
|
if (fs.existsSync(fileDir)) {
|
|
// let quoteFile = fs.readFileSync(fileDir);
|
|
// quoteFile = JSON.parse(quoteFile.toString());
|
|
// quoteFile.quotes.every((old) => {
|
|
// if (stringSimilarity.compareTwoStrings(old.text, quote.text) > 0.9) {
|
|
// duplicateId = old.id;
|
|
// similarityScore = stringSimilarity.compareTwoStrings(
|
|
// old.text,
|
|
// quote.text
|
|
// );
|
|
// return false;
|
|
// }
|
|
// return true;
|
|
// });
|
|
} else {
|
|
return { languageError: 1 };
|
|
}
|
|
if (duplicateId != -1) {
|
|
return { duplicateId, similarityScore };
|
|
}
|
|
return await mongoDB().collection("new-quotes").insertOne(quote);
|
|
}
|
|
|
|
static async get() {
|
|
return await mongoDB()
|
|
.collection("new-quotes")
|
|
.find({ approved: false })
|
|
.sort({ timestamp: 1 })
|
|
.limit(10)
|
|
.toArray();
|
|
}
|
|
|
|
static async approve(quoteId, editQuote, editSource) {
|
|
//check mod status
|
|
let quote = await mongoDB()
|
|
.collection("new-quotes")
|
|
.findOne({ _id: ObjectID(quoteId) });
|
|
if (!quote) {
|
|
throw new MonkeyError(404, "Quote not found");
|
|
}
|
|
let language = quote.language;
|
|
quote = {
|
|
text: editQuote ? editQuote : quote.text,
|
|
source: editSource ? editSource : quote.source,
|
|
length: quote.text.length,
|
|
};
|
|
let message = "";
|
|
const fileDir = path.join(
|
|
__dirname,
|
|
`../../../monkeytype-new-quotes/static/quotes/${language}.json`
|
|
);
|
|
await git.pull("upstream", "master");
|
|
if (fs.existsSync(fileDir)) {
|
|
let quoteFile = fs.readFileSync(fileDir);
|
|
quoteFile = JSON.parse(quoteFile.toString());
|
|
quoteFile.quotes.every((old) => {
|
|
if (stringSimilarity.compareTwoStrings(old.text, quote.text) > 0.8) {
|
|
throw new MonkeyError(409, "Duplicate quote");
|
|
}
|
|
});
|
|
let maxid = 0;
|
|
quoteFile.quotes.map(function (q) {
|
|
if (q.id > maxid) {
|
|
maxid = q.id;
|
|
}
|
|
});
|
|
quote.id = maxid + 1;
|
|
quoteFile.quotes.push(quote);
|
|
fs.writeFileSync(fileDir, JSON.stringify(quoteFile, null, 2));
|
|
message = `Added quote to ${language}.json.`;
|
|
} else {
|
|
//file doesnt exist, create it
|
|
quote.id = 1;
|
|
fs.writeFileSync(
|
|
fileDir,
|
|
JSON.stringify({
|
|
language: language,
|
|
groups: [
|
|
[0, 100],
|
|
[101, 300],
|
|
[301, 600],
|
|
[601, 9999],
|
|
],
|
|
quotes: [quote],
|
|
})
|
|
);
|
|
message = `Created file ${language}.json and added quote.`;
|
|
}
|
|
await git.add([`static/quotes/${language}.json`]);
|
|
await git.commit(`Added quote to ${language}.json`);
|
|
await git.push("origin", "master");
|
|
await mongoDB()
|
|
.collection("new-quotes")
|
|
.deleteOne({ _id: ObjectID(quoteId) });
|
|
return { quote, message };
|
|
}
|
|
|
|
static async refuse(quoteId) {
|
|
return await mongoDB()
|
|
.collection("new-quotes")
|
|
.deleteOne({ _id: ObjectID(quoteId) });
|
|
}
|
|
}
|
|
|
|
module.exports = NewQuotesDAO;
|