Эх сурвалжийг харах

fix: apply non-logged-in cooldown

poeti8 5 жил өмнө
parent
commit
c5ba1d67ae

+ 6 - 10
server/handlers/auth.ts

@@ -8,10 +8,10 @@ import axios from "axios";
 
 import { CustomError } from "../utils";
 import * as utils from "../utils";
+import * as redis from "../redis";
+import queries from "../queries";
 import * as mail from "../mail";
 import query from "../queries";
-import knex from "../knex";
-import * as redis from "../redis";
 import env from "../env";
 
 const authenticate = (
@@ -65,14 +65,10 @@ export const cooldown: Handler = async (req, res, next) => {
   const cooldownConfig = env.NON_USER_COOLDOWN;
   if (req.user || !cooldownConfig) return next();
 
-  const ip = await knex<IP>("ips")
-    .where({ ip: req.realIP.toLowerCase() })
-    .andWhere(
-      "created_at",
-      ">",
-      subMinutes(new Date(), cooldownConfig).toISOString()
-    )
-    .first();
+  const ip = await queries.ip.find({
+    ip: req.realIP.toLowerCase(),
+    created_at: [">", subMinutes(new Date(), cooldownConfig).toISOString()]
+  });
 
   if (ip) {
     const timeToWait =

+ 12 - 0
server/queries/ip.ts

@@ -25,6 +25,18 @@ export const add = async (ipToAdd: string) => {
   return ip;
 };
 
+export const find = async (match: Match<IP>) => {
+  const query = knex<IP>("ips");
+
+  Object.entries(match).forEach(([key, value]) => {
+    query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
+  });
+
+  const ip = await query.first();
+
+  return ip;
+};
+
 export const clear = async () =>
   knex<IP>("ips")
     .where(

+ 1 - 0
server/routes/links.ts

@@ -23,6 +23,7 @@ router.post(
   asyncHandler(auth.apikey),
   asyncHandler(auth.jwtLoose),
   asyncHandler(auth.recaptcha),
+  asyncHandler(auth.cooldown),
   validators.createLink,
   asyncHandler(helpers.verify),
   asyncHandler(link.create)