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 {
|
export interface Storage {
|
||||||
getEvent(
|
getEvent(
|
||||||
type: VkEvent,
|
type: VkEvent,
|
||||||
id: number,
|
eventId: number,
|
||||||
groupId: number,
|
groupId: number,
|
||||||
channel: string
|
channel: string
|
||||||
): Promise<Event>;
|
): 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>;
|
createOrUpdateLike(like: Partial<Like>): Promise<Like>;
|
||||||
|
|
||||||
|
|
|
@ -5,16 +5,17 @@ import {
|
||||||
PrimaryGeneratedColumn,
|
PrimaryGeneratedColumn,
|
||||||
UpdateDateColumn,
|
UpdateDateColumn,
|
||||||
} from "typeorm";
|
} from "typeorm";
|
||||||
import { StoredEvent } from "../../types";
|
|
||||||
import { VkEvent } from "../../../vk/types";
|
import { VkEvent } from "../../../vk/types";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Event implements StoredEvent {
|
export class Event {
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
id: number;
|
id: number;
|
||||||
@Column()
|
@Column()
|
||||||
type: VkEvent;
|
type: VkEvent;
|
||||||
@Column()
|
@Column()
|
||||||
|
eventId: number;
|
||||||
|
@Column()
|
||||||
groupId: number;
|
groupId: number;
|
||||||
@Column()
|
@Column()
|
||||||
channel: string;
|
channel: string;
|
||||||
|
|
|
@ -5,10 +5,9 @@ import {
|
||||||
PrimaryGeneratedColumn,
|
PrimaryGeneratedColumn,
|
||||||
UpdateDateColumn,
|
UpdateDateColumn,
|
||||||
} from "typeorm";
|
} from "typeorm";
|
||||||
import { StoredLike } from "../../types";
|
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Like implements StoredLike {
|
export class Like {
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
id: number;
|
id: number;
|
||||||
@Column()
|
@Column()
|
||||||
|
|
|
@ -35,19 +35,29 @@ export class PostgresDB implements Storage {
|
||||||
|
|
||||||
getEvent = async (
|
getEvent = async (
|
||||||
type: VkEvent,
|
type: VkEvent,
|
||||||
id: number,
|
eventId: number,
|
||||||
groupId: number,
|
groupId: number,
|
||||||
channel: string
|
channel: string
|
||||||
) => {
|
) => {
|
||||||
return await this.events.findOne({ type, id, groupId, channel });
|
return await this.events.findOne({ type, eventId, groupId, channel });
|
||||||
};
|
};
|
||||||
|
|
||||||
createEvent = async (event) => {
|
createEvent = async (
|
||||||
const result = this.events.create({
|
type: VkEvent,
|
||||||
...event,
|
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) => {
|
getLikesFor = async (channel, messageId) => {
|
||||||
|
|
|
@ -85,7 +85,6 @@ export class TelegramService {
|
||||||
extra?: ExtraReplyMessage
|
extra?: ExtraReplyMessage
|
||||||
) => {
|
) => {
|
||||||
logger.debug(`sending message "${message}" to chan "${channel}"`);
|
logger.debug(`sending message "${message}" to chan "${channel}"`);
|
||||||
await this.bot.telegram.sendMessage(channel, message, extra);
|
return await this.bot.telegram.sendMessage(channel, message, extra);
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,14 +41,26 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
private likes: string[] = ["👎", "👍"];
|
private likes: string[] = ["👎", "👍"];
|
||||||
|
|
||||||
public execute = async (context: WallPostContext, next: NextMiddleware) => {
|
public execute = async (context: WallPostContext, next: NextMiddleware) => {
|
||||||
|
const id = context?.wall?.id;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
context.isRepost ||
|
context.isRepost ||
|
||||||
!PostNewHandler.isValidPostType(context?.wall?.postType)
|
!PostNewHandler.isValidPostType(context?.wall?.postType) ||
|
||||||
|
!id
|
||||||
) {
|
) {
|
||||||
await next();
|
await next();
|
||||||
return;
|
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
|
const user = context.wall.signerId
|
||||||
? await this.getUserByID(String(context.wall.signerId))
|
? await this.getUserByID(String(context.wall.signerId))
|
||||||
: undefined;
|
: undefined;
|
||||||
|
@ -67,7 +79,13 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
|
|
||||||
this.appendExtras(extras, text);
|
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();
|
await next();
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { VkService } from "../index";
|
||||||
import { TelegramService } from "../../telegram";
|
import { TelegramService } from "../../telegram";
|
||||||
import { Template } from "../../template";
|
import { Template } from "../../template";
|
||||||
import { Storage } from "../../db";
|
import { Storage } from "../../db";
|
||||||
|
import { Event } from "../../db/postgres/entities/Event";
|
||||||
|
|
||||||
export class VkEventHandler<
|
export class VkEventHandler<
|
||||||
F extends Record<string, any> = any,
|
F extends Record<string, any> = any,
|
||||||
|
@ -46,4 +47,28 @@ export class VkEventHandler<
|
||||||
*/
|
*/
|
||||||
protected makeDialogUrl = (groupId: number, userId: number): string =>
|
protected makeDialogUrl = (groupId: number, userId: number): string =>
|
||||||
`https://vk.com/gim${groupId}?sel=${userId}`;
|
`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 });
|
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);
|
inst.updates.getWebhookCallback(this.config.endpoint)(req, res, next);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue