From 93f9898bdf89333b9f82d65099c3e1956ad62bf7 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Thu, 13 May 2021 12:57:00 +0700 Subject: [PATCH] #6 added duplicate events detection --- src/service/vk/index.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/service/vk/index.ts b/src/service/vk/index.ts index 2f91bb2..38d3cb5 100644 --- a/src/service/vk/index.ts +++ b/src/service/vk/index.ts @@ -19,6 +19,9 @@ export class VkService { private readonly instances: Record; private readonly groups: Record; + // Used to register all incoming events + private incomingEvents: string[] = []; + constructor( private config: VkConfig, private telegram: TelegramService, @@ -56,6 +59,9 @@ export class VkService { const { body } = req; const { groups } = this; const groupId = body?.group_id; + const group = this.groups[groupId]; + const eventId = body?.event_id; + const type = body?.type; if (!groupId || !has(groupId, groups) || !has(groupId, this.instances)) { logger.warn(`vk received unknown call`, { body }); @@ -63,11 +69,26 @@ export class VkService { return; } + if (eventId) { + if (this.incomingEvents.includes(eventId)) { + logger.warn( + `received duplicate event "${type} (${eventId})" for group "${group.name} (skipping)"` + ); + return res.send("OK"); + } + + this.incomingEvents.push(eventId); + } + logger.debug(`received vk event`, { body }); const inst = this.instances[groupId] as GroupInstance; - inst.updates.getWebhookCallback(this.config.endpoint)(req, res, next); + await inst.updates.getWebhookCallback(this.config.endpoint)( + req, + res, + next + ); } catch (e) { next(e); }