import { NextMiddleware } from "middleware-io"; import { ConfigGroup, GroupInstance, VkEvent } from "../types"; import { VkService } from "../index"; import { TelegramService } from "../../telegram"; import { Template } from "../../template"; import { Storage } from "../../db"; import { Event } from "../../db/postgres/entities/Event"; export class VkEventHandler< F extends Record = any, V extends Record = any > { public constructor( protected type: VkEvent, protected group: ConfigGroup, protected channel: string, protected instance: GroupInstance, protected vk: VkService, protected telegram: TelegramService, protected template: Template, protected db: Storage ) {} public execute: ( context: any, next: NextMiddleware ) => Promise = async (ctx, next) => { console.log(`vk received unknown event`, ctx); await next(); }; /** * Fetches user by id * @param id */ protected getUserByID = async (id: string) => { const users = await this.instance.api.users.get({ user_ids: [id], fields: ["sex"], }); return users[0]; }; /** * Returns url for group dialog */ protected makeDialogUrl = (groupId: number, userId: number): string => `https://vk.com/gim${groupId}?sel=${userId}`; /** * Checks for duplicates */ getEventFromDB = async (id?: number): Promise => { if (!id) { return undefined; } return await this.db.getEvent(this.type, id, this.group.id, this.channel); }; /** * Creates event record in DB */ storeInDB = async (id: number, tgMessageId: number) => { return await this.db.createEvent( this.type, id, this.group.id, this.channel, tgMessageId ); }; }