Explorar el Código

fix: move back check visit limit to queue process

poeti8 hace 6 años
padre
commit
a0e5fa8aac
Se han modificado 2 ficheros con 13 adiciones y 16 borrados
  1. 3 8
      server/controllers/linkController.ts
  2. 10 8
      server/queues.ts

+ 3 - 8
server/controllers/linkController.ts

@@ -20,12 +20,7 @@ import {
 } from "../db/link";
 import transporter from "../mail/mail";
 import * as redis from "../redis";
-import {
-  addProtocol,
-  generateShortLink,
-  getStatsCacheTime,
-  getStatsLimit
-} from "../utils";
+import { addProtocol, generateShortLink, getStatsCacheTime } from "../utils";
 import {
   checkBannedDomain,
   checkBannedHost,
@@ -160,7 +155,7 @@ export const goToLink: Handler = async (req, res, next) => {
     if (!isMatch) {
       return res.status(401).json({ error: "Password is not correct" });
     }
-    if (link.user_id && !isBot && link.visit_count < getStatsLimit()) {
+    if (link.user_id && !isBot) {
       visitQueue.add({
         headers: req.headers,
         realIP: req.realIP,
@@ -172,7 +167,7 @@ export const goToLink: Handler = async (req, res, next) => {
     return res.status(200).json({ target: link.target });
   }
 
-  if (link.user_id && !isBot && link.visit_count < getStatsLimit()) {
+  if (link.user_id && !isBot) {
     visitQueue.add({
       headers: req.headers,
       realIP: req.realIP,

+ 10 - 8
server/queues.ts

@@ -4,6 +4,7 @@ import geoip from "geoip-lite";
 import URL from "url";
 
 import { createVisit, addLinkCount } from "./db/link";
+import { getStatsLimit } from "./utils";
 
 const redis = {
   port: Number(process.env.REDIS_PORT) || 6379,
@@ -30,13 +31,14 @@ visitQueue.process(({ data }) => {
 
   return Promise.all([
     addLinkCount(data.link.id),
-    createVisit({
-      browser: browser.toLowerCase(),
-      country: country || "Unknown",
-      domain: data.customDomain,
-      id: data.link.id,
-      os: os.toLowerCase().replace(/\s/gi, ""),
-      referrer: (referrer && referrer.replace(/\./gi, "[dot]")) || "Direct"
-    })
+    data.link.visit_count < getStatsLimit() &&
+      createVisit({
+        browser: browser.toLowerCase(),
+        country: country || "Unknown",
+        domain: data.customDomain,
+        id: data.link.id,
+        os: os.toLowerCase().replace(/\s/gi, ""),
+        referrer: (referrer && referrer.replace(/\./gi, "[dot]")) || "Direct"
+      })
   ]);
 });