2021-05-15 01:56:08 +08:00
|
|
|
const express = require("express");
|
2021-06-07 00:32:37 +08:00
|
|
|
const { config } = require("dotenv");
|
2021-06-16 07:46:37 +08:00
|
|
|
const path = require("path");
|
2021-07-08 19:30:06 +08:00
|
|
|
const MonkeyError = require("./handlers/error");
|
2021-06-07 00:32:37 +08:00
|
|
|
config({ path: path.join(__dirname, ".env") });
|
2021-09-10 06:39:10 +08:00
|
|
|
const CronJob = require("cron").CronJob;
|
2021-06-01 11:47:58 +08:00
|
|
|
const cors = require("cors");
|
|
|
|
const admin = require("firebase-admin");
|
2021-09-10 21:18:40 +08:00
|
|
|
const Logger = require("./handlers/logger.js");
|
2021-06-07 00:32:37 +08:00
|
|
|
const serviceAccount = require("./credentials/serviceAccountKey.json");
|
2021-08-18 07:33:14 +08:00
|
|
|
const { connectDB, mongoDB } = require("./init/mongodb");
|
2021-09-14 21:54:03 +08:00
|
|
|
const BotDAO = require("./dao/bot");
|
2021-05-20 06:39:18 +08:00
|
|
|
|
2021-06-16 07:47:34 +08:00
|
|
|
const PORT = process.env.PORT || 5005;
|
|
|
|
|
2021-06-01 11:47:58 +08:00
|
|
|
// MIDDLEWARE & SETUP
|
2021-05-15 01:56:08 +08:00
|
|
|
const app = express();
|
2021-06-07 00:32:37 +08:00
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
app.use(express.json());
|
2021-08-17 01:01:52 +08:00
|
|
|
app.use(cors());
|
2021-05-15 01:56:08 +08:00
|
|
|
|
2021-08-28 04:52:49 +08:00
|
|
|
app.set("trust proxy", 1);
|
|
|
|
|
2021-09-06 07:52:05 +08:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
if (process.env.MAINTENANCE === "true") {
|
2021-09-06 07:52:32 +08:00
|
|
|
res.status(503).json({ message: "Server is down for maintenance" });
|
2021-09-06 07:52:05 +08:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-14 05:43:02 +08:00
|
|
|
let startingPath = "";
|
|
|
|
|
|
|
|
if (process.env.API_PATH_OVERRIDE) {
|
|
|
|
startingPath = "/" + process.env.API_PATH_OVERRIDE;
|
|
|
|
}
|
|
|
|
|
2021-06-16 07:46:37 +08:00
|
|
|
const userRouter = require("./api/routes/user");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/user", userRouter);
|
2021-07-06 21:52:36 +08:00
|
|
|
const configRouter = require("./api/routes/config");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/config", configRouter);
|
2021-07-06 22:22:05 +08:00
|
|
|
const resultRouter = require("./api/routes/result");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/results", resultRouter);
|
2021-07-09 05:34:36 +08:00
|
|
|
const presetRouter = require("./api/routes/preset");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/presets", presetRouter);
|
2021-08-28 03:10:00 +08:00
|
|
|
const quoteRatings = require("./api/routes/quote-ratings");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/quote-ratings", quoteRatings);
|
2021-08-29 21:30:45 +08:00
|
|
|
const psaRouter = require("./api/routes/psa");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/psa", psaRouter);
|
2021-09-06 06:57:07 +08:00
|
|
|
const leaderboardsRouter = require("./api/routes/leaderboards");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/leaderboard", leaderboardsRouter);
|
Quote submission (#1984)
* 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>
2021-10-11 05:46:35 +08:00
|
|
|
const newQuotesRouter = require("./api/routes/new-quotes");
|
2021-12-14 05:43:02 +08:00
|
|
|
app.use(startingPath + "/new-quotes", newQuotesRouter);
|
2021-05-15 01:56:08 +08:00
|
|
|
|
2022-01-08 00:24:58 +08:00
|
|
|
app.use(function (req, res) {
|
2021-08-18 07:41:33 +08:00
|
|
|
let monkeyError;
|
2022-01-08 00:24:58 +08:00
|
|
|
if (req.errorID) {
|
2021-08-18 07:41:33 +08:00
|
|
|
//its a monkey error
|
2022-01-08 00:26:25 +08:00
|
|
|
monkeyError = req;
|
2021-08-18 07:41:33 +08:00
|
|
|
} else {
|
|
|
|
//its a server error
|
2022-01-08 00:24:58 +08:00
|
|
|
monkeyError = new MonkeyError(req.status, req.message, req.stack);
|
2021-09-13 22:40:12 +08:00
|
|
|
}
|
|
|
|
if (!monkeyError.uid && req.decodedToken) {
|
|
|
|
monkeyError.uid = req.decodedToken.uid;
|
2021-08-18 07:41:33 +08:00
|
|
|
}
|
2021-08-18 03:00:29 +08:00
|
|
|
if (process.env.MODE !== "dev" && monkeyError.status > 400) {
|
2021-09-10 07:03:01 +08:00
|
|
|
Logger.log(
|
2022-01-08 00:05:24 +08:00
|
|
|
"system_error",
|
2021-09-10 07:03:01 +08:00
|
|
|
`${monkeyError.status} ${monkeyError.message}`,
|
|
|
|
monkeyError.uid
|
|
|
|
);
|
2021-08-18 07:33:14 +08:00
|
|
|
mongoDB().collection("errors").insertOne({
|
2021-08-18 03:00:29 +08:00
|
|
|
_id: monkeyError.errorID,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
status: monkeyError.status,
|
|
|
|
uid: monkeyError.uid,
|
|
|
|
message: monkeyError.message,
|
|
|
|
stack: monkeyError.stack,
|
|
|
|
});
|
2022-01-08 00:24:58 +08:00
|
|
|
} else {
|
|
|
|
console.error(req.stack);
|
2021-08-18 03:00:29 +08:00
|
|
|
}
|
2021-07-08 19:30:06 +08:00
|
|
|
return res.status(e.status || 500).json(monkeyError);
|
2021-05-15 01:56:08 +08:00
|
|
|
});
|
2021-06-14 14:49:23 +08:00
|
|
|
|
2021-08-11 07:02:17 +08:00
|
|
|
app.get("/test", (req, res) => {
|
|
|
|
res.send("Hello World!");
|
|
|
|
});
|
|
|
|
|
2021-09-10 06:39:10 +08:00
|
|
|
const LeaderboardsDAO = require("./dao/leaderboards");
|
|
|
|
|
2021-06-16 07:47:34 +08:00
|
|
|
app.listen(PORT, async () => {
|
|
|
|
console.log(`listening on port ${PORT}`);
|
2021-06-14 14:49:23 +08:00
|
|
|
await connectDB();
|
|
|
|
admin.initializeApp({
|
|
|
|
credential: admin.credential.cert(serviceAccount),
|
|
|
|
});
|
|
|
|
console.log("Database Connected");
|
2021-09-06 06:57:07 +08:00
|
|
|
|
2021-09-14 21:54:35 +08:00
|
|
|
let lbjob = new CronJob("30 4/5 * * * *", async () => {
|
2021-09-14 21:54:03 +08:00
|
|
|
let before15 = await mongoDB()
|
|
|
|
.collection("leaderboards.english.time.15")
|
|
|
|
.find()
|
|
|
|
.limit(10)
|
|
|
|
.toArray();
|
|
|
|
LeaderboardsDAO.update("time", "15", "english").then(async () => {
|
|
|
|
let after15 = await mongoDB()
|
|
|
|
.collection("leaderboards.english.time.15")
|
|
|
|
.find()
|
|
|
|
.limit(10)
|
|
|
|
.toArray();
|
|
|
|
|
|
|
|
let changed;
|
2021-11-21 03:45:13 +08:00
|
|
|
let recent = false;
|
2021-09-14 21:54:03 +08:00
|
|
|
for (let index in before15) {
|
|
|
|
if (before15[index].uid !== after15[index].uid) {
|
|
|
|
//something changed at this index
|
2021-11-21 03:45:13 +08:00
|
|
|
if (after15[index].timestamp > Date.now() - 1000 * 60 * 10) {
|
|
|
|
//checking if test is within 10 minutes
|
|
|
|
recent = true;
|
|
|
|
}
|
2021-09-14 21:54:03 +08:00
|
|
|
changed = after15[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-21 03:45:13 +08:00
|
|
|
if (changed && recent) {
|
2021-09-14 21:54:03 +08:00
|
|
|
let name = changed.discordId ?? changed.name;
|
|
|
|
BotDAO.announceLbUpdate(
|
|
|
|
name,
|
|
|
|
changed.rank,
|
|
|
|
"time 15 english",
|
|
|
|
changed.wpm,
|
|
|
|
changed.raw,
|
|
|
|
changed.acc,
|
|
|
|
changed.consistency
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let before60 = await mongoDB()
|
|
|
|
.collection("leaderboards.english.time.60")
|
|
|
|
.find()
|
|
|
|
.limit(10)
|
|
|
|
.toArray();
|
|
|
|
LeaderboardsDAO.update("time", "60", "english").then(async () => {
|
|
|
|
let after60 = await mongoDB()
|
|
|
|
.collection("leaderboards.english.time.60")
|
|
|
|
.find()
|
|
|
|
.limit(10)
|
|
|
|
.toArray();
|
|
|
|
let changed;
|
2021-11-21 02:19:47 +08:00
|
|
|
let recent = false;
|
2021-09-14 21:54:03 +08:00
|
|
|
for (let index in before60) {
|
|
|
|
if (before60[index].uid !== after60[index].uid) {
|
|
|
|
//something changed at this index
|
2021-11-21 02:19:47 +08:00
|
|
|
if (after60[index].timestamp > Date.now() - 1000 * 60 * 10) {
|
|
|
|
//checking if test is within 10 minutes
|
|
|
|
recent = true;
|
|
|
|
}
|
2021-09-14 21:54:03 +08:00
|
|
|
changed = after60[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-21 02:19:47 +08:00
|
|
|
if (changed && recent) {
|
2021-09-14 21:54:03 +08:00
|
|
|
let name = changed.discordId ?? changed.name;
|
|
|
|
BotDAO.announceLbUpdate(
|
|
|
|
name,
|
|
|
|
changed.rank,
|
|
|
|
"time 60 english",
|
|
|
|
changed.wpm,
|
|
|
|
changed.raw,
|
|
|
|
changed.acc,
|
|
|
|
changed.consistency
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2021-09-10 06:39:10 +08:00
|
|
|
});
|
|
|
|
lbjob.start();
|
2021-09-14 07:10:56 +08:00
|
|
|
|
|
|
|
let logjob = new CronJob("0 0 0 * * *", async () => {
|
|
|
|
let data = await mongoDB()
|
|
|
|
.collection("logs")
|
|
|
|
.deleteMany({ timestamp: { $lt: Date.now() - 604800000 } });
|
|
|
|
Logger.log(
|
|
|
|
"system_logs_deleted",
|
|
|
|
`${data.deletedCount} logs deleted older than 7 days`,
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
});
|
|
|
|
logjob.start();
|
2021-09-10 06:39:10 +08:00
|
|
|
});
|