mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-26 15:36:41 +07:00
#4 added duplicate check for wall_post_new
This commit is contained in:
parent
e73ab3cb4f
commit
f75e3068a0
8 changed files with 75 additions and 16 deletions
|
@ -41,14 +41,26 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
private likes: string[] = ["👎", "👍"];
|
||||
|
||||
public execute = async (context: WallPostContext, next: NextMiddleware) => {
|
||||
const id = context?.wall?.id;
|
||||
|
||||
if (
|
||||
context.isRepost ||
|
||||
!PostNewHandler.isValidPostType(context?.wall?.postType)
|
||||
!PostNewHandler.isValidPostType(context?.wall?.postType) ||
|
||||
!id
|
||||
) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
const exist = await this.getEventFromDB(id);
|
||||
if (exist) {
|
||||
logger.warn(
|
||||
`received duplicate entry for ${this.group.name}, ${this.type}, ${id}`
|
||||
);
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
const user = context.wall.signerId
|
||||
? await this.getUserByID(String(context.wall.signerId))
|
||||
: undefined;
|
||||
|
@ -67,7 +79,13 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
|
||||
this.appendExtras(extras, text);
|
||||
|
||||
await this.telegram.sendMessageToChan(this.channel, parsed, extras);
|
||||
const msg = await this.telegram.sendMessageToChan(
|
||||
this.channel,
|
||||
parsed,
|
||||
extras
|
||||
);
|
||||
|
||||
await this.storeInDB(id, msg.message_id);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
|
|
@ -4,6 +4,7 @@ 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<string, any> = any,
|
||||
|
@ -46,4 +47,28 @@ export class VkEventHandler<
|
|||
*/
|
||||
protected makeDialogUrl = (groupId: number, userId: number): string =>
|
||||
`https://vk.com/gim${groupId}?sel=${userId}`;
|
||||
|
||||
/**
|
||||
* Checks for duplicates
|
||||
*/
|
||||
getEventFromDB = async (id?: number): Promise<Event | undefined> => {
|
||||
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
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue