Răsfoiți Sursa

fix: check if link should create visit before sending to queue

poeti8 6 ani în urmă
părinte
comite
4e6cbc4720
3 a modificat fișierele cu 9 adăugiri și 9 ștergeri
  1. 8 3
      server/controllers/linkController.ts
  2. 0 1
      server/db/link.ts
  3. 1 5
      server/queues.ts

+ 8 - 3
server/controllers/linkController.ts

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

+ 0 - 1
server/db/link.ts

@@ -59,7 +59,6 @@ interface ICreateVisit {
   country: string;
   domain?: string;
   id: number;
-  limit: number;
   os: string;
   referrer: string;
 }

+ 1 - 5
server/queues.ts

@@ -4,7 +4,6 @@ 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,
@@ -29,8 +28,6 @@ visitQueue.process(({ data }) => {
   const location = geoip.lookup(data.realIP);
   const country = location && location.country;
 
-  if (data.visit_count > getStatsLimit()) return;
-
   return Promise.all([
     addLinkCount(data.link.id),
     createVisit({
@@ -39,8 +36,7 @@ visitQueue.process(({ data }) => {
       domain: data.customDomain,
       id: data.link.id,
       os: os.toLowerCase().replace(/\s/gi, ""),
-      referrer: (referrer && referrer.replace(/\./gi, "[dot]")) || "Direct",
-      limit: getStatsLimit()
+      referrer: (referrer && referrer.replace(/\./gi, "[dot]")) || "Direct"
     })
   ]);
 });