Prechádzať zdrojové kódy

feat: Convert cooldowns to single datetime and increment

marvin-wtt 1 rok pred
rodič
commit
2ff0fa4222

+ 7 - 10
server/handlers/validators.handler.js

@@ -346,12 +346,10 @@ const deleteUser = [
 
 // TODO: if user has posted malware should do something better
 function cooldown(user) {
-  if (!env.GOOGLE_SAFE_BROWSING_KEY || !user || !user.cooldowns) return;
+  if (!env.GOOGLE_SAFE_BROWSING_KEY || !user || !user.cooldown) return;
 
-  // If has active cooldown then throw error
-  const hasCooldownNow = user.cooldowns.some(cooldown =>
-    isAfter(subHours(new Date(), 12), new Date(cooldown))
-  );
+  // If user has active cooldown then throw error
+  const hasCooldownNow = isAfter(subHours(new Date(), 12), new Date(user.cooldown))
 
   if (hasCooldownNow) {
     throw new utils.CustomError("Cooldown because of a malware URL. Wait 12h");
@@ -396,14 +394,13 @@ async function malware(user, target) {
     const [updatedUser] = await query.user.update(
       { id: user.id },
       {
-        cooldowns: knex.raw("array_append(cooldowns, ?)", [
-          new Date().toISOString()
-        ])
-      }
+        cooldown: new Date().toISOString(),
+      },
+      ['malicious_attempts']
     );
 
     // Ban if too many cooldowns
-    if (updatedUser.cooldowns.length > 2) {
+    if (updatedUser.malicious_attempts.length > 2) {
       await query.user.update({ id: user.id }, { banned: true });
       throw new utils.CustomError("Too much malware requests. You are now banned.");
     }

+ 21 - 0
server/migrations/20240911230301_change_cooldown.js

@@ -0,0 +1,21 @@
+async function up(knex) {
+  await knex.schema.alterTable("users", table => {
+    table.dropColumn("cooldowns");
+    table.datetime("cooldown").nullable();
+    table.integer("malicious_attempts").defaultTo(0);
+  });
+}
+
+async function down(knex) {
+  await knex.schema.alterTable("users", table => {
+    table.dropColumn("cooldown");
+    table.json("cooldowns").defaultTo("[]");
+    table.dropColumn("malicious_attempts");
+  });
+}
+
+module.exports = {
+  up,
+  down
+};
+

+ 3 - 1
server/models/user.model.js

@@ -12,7 +12,9 @@ async function createUserTable(knex) {
         .integer("banned_by_id")
         .references("id")
         .inTable("users");
-      table.specificType("cooldowns", "timestamptz[]");
+      table
+        .json("cooldowns")
+        .defaultTo("[]");
       table
         .string("email")
         .unique()

+ 9 - 2
server/queries/user.queries.js

@@ -51,17 +51,24 @@ async function add(params, user) {
   };
 }
 
-async function update(match, update) {
+async function update(match, update, increments = []) {
   const query = knex("users");
   
   Object.entries(match).forEach(([key, value]) => {
     query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
   });
+
+  let updateQuery = query;
+  increments.forEach(columnName => {
+    updateQuery.increment(columnName);
+  });
   
-  const users = await query.update(
+  await updateQuery.update(
     { ...update, updated_at: new Date().toISOString() },
     "*"
   );
+
+  const users = await query.all();
   
   users.forEach(redis.remove.user);