mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46: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
|
@ -5,12 +5,18 @@ import { Event } from "./postgres/entities/Event";
|
|||
export interface Storage {
|
||||
getEvent(
|
||||
type: VkEvent,
|
||||
id: number,
|
||||
eventId: number,
|
||||
groupId: number,
|
||||
channel: string
|
||||
): Promise<Event>;
|
||||
|
||||
createEvent(event: Partial<Event>): Promise<Event>;
|
||||
createEvent(
|
||||
type: VkEvent,
|
||||
eventId: number,
|
||||
groupId: number,
|
||||
channel: string,
|
||||
tgMessageId: number
|
||||
): Promise<Event>;
|
||||
|
||||
createOrUpdateLike(like: Partial<Like>): Promise<Like>;
|
||||
|
||||
|
|
|
@ -5,16 +5,17 @@ import {
|
|||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm";
|
||||
import { StoredEvent } from "../../types";
|
||||
import { VkEvent } from "../../../vk/types";
|
||||
|
||||
@Entity()
|
||||
export class Event implements StoredEvent {
|
||||
export class Event {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column()
|
||||
type: VkEvent;
|
||||
@Column()
|
||||
eventId: number;
|
||||
@Column()
|
||||
groupId: number;
|
||||
@Column()
|
||||
channel: string;
|
||||
|
|
|
@ -5,10 +5,9 @@ import {
|
|||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm";
|
||||
import { StoredLike } from "../../types";
|
||||
|
||||
@Entity()
|
||||
export class Like implements StoredLike {
|
||||
export class Like {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column()
|
||||
|
|
|
@ -35,19 +35,29 @@ export class PostgresDB implements Storage {
|
|||
|
||||
getEvent = async (
|
||||
type: VkEvent,
|
||||
id: number,
|
||||
eventId: number,
|
||||
groupId: number,
|
||||
channel: string
|
||||
) => {
|
||||
return await this.events.findOne({ type, id, groupId, channel });
|
||||
return await this.events.findOne({ type, eventId, groupId, channel });
|
||||
};
|
||||
|
||||
createEvent = async (event) => {
|
||||
const result = this.events.create({
|
||||
...event,
|
||||
createEvent = async (
|
||||
type: VkEvent,
|
||||
eventId: number,
|
||||
groupId: number,
|
||||
channel: string,
|
||||
tgMessageId: number
|
||||
) => {
|
||||
const event = this.events.create({
|
||||
type,
|
||||
eventId,
|
||||
groupId,
|
||||
channel,
|
||||
tgMessageId,
|
||||
});
|
||||
|
||||
return result[0];
|
||||
return await this.events.save(event);
|
||||
};
|
||||
|
||||
getLikesFor = async (channel, messageId) => {
|
||||
|
|
|
@ -85,7 +85,6 @@ export class TelegramService {
|
|||
extra?: ExtraReplyMessage
|
||||
) => {
|
||||
logger.debug(`sending message "${message}" to chan "${channel}"`);
|
||||
await this.bot.telegram.sendMessage(channel, message, extra);
|
||||
return;
|
||||
return await this.bot.telegram.sendMessage(channel, message, extra);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -66,6 +66,7 @@ export class VkService {
|
|||
logger.debug(`received vk event`, { body });
|
||||
|
||||
const inst = this.instances[groupId] as GroupInstance;
|
||||
|
||||
inst.updates.getWebhookCallback(this.config.endpoint)(req, res, next);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue