1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-24 22:46:41 +07:00

#6 added duplicate events detection

This commit is contained in:
Fedor Katurov 2021-05-13 12:57:00 +07:00
parent 2feae715f0
commit 93f9898bdf

View file

@ -19,6 +19,9 @@ export class VkService {
private readonly instances: Record<string, GroupInstance>; private readonly instances: Record<string, GroupInstance>;
private readonly groups: Record<number, ConfigGroup>; private readonly groups: Record<number, ConfigGroup>;
// Used to register all incoming events
private incomingEvents: string[] = [];
constructor( constructor(
private config: VkConfig, private config: VkConfig,
private telegram: TelegramService, private telegram: TelegramService,
@ -56,6 +59,9 @@ export class VkService {
const { body } = req; const { body } = req;
const { groups } = this; const { groups } = this;
const groupId = body?.group_id; 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)) { if (!groupId || !has(groupId, groups) || !has(groupId, this.instances)) {
logger.warn(`vk received unknown call`, { body }); logger.warn(`vk received unknown call`, { body });
@ -63,11 +69,26 @@ export class VkService {
return; 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 }); logger.debug(`received vk event`, { body });
const inst = this.instances[groupId] as GroupInstance; 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) { } catch (e) {
next(e); next(e);
} }