mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 06:56:40 +07:00
#4 fixed event handling
This commit is contained in:
parent
e2457eb8c8
commit
948f5ae67c
9 changed files with 265 additions and 11 deletions
|
@ -10,7 +10,8 @@ export interface Storage {
|
|||
groupId: number,
|
||||
channel: string
|
||||
): Promise<Event | undefined>;
|
||||
getEventById(
|
||||
getEventById(eventId: number): Promise<Event | undefined>;
|
||||
getEventByVKEventId(
|
||||
type: VkEvent,
|
||||
eventId: number,
|
||||
groupId: number,
|
||||
|
|
|
@ -19,6 +19,8 @@ export class PostgresDB implements Storage {
|
|||
constructor(private config: PostgresConfig) {}
|
||||
|
||||
connect = async () => {
|
||||
logger.info(`connecting to ${this.config.uri}`);
|
||||
|
||||
this.connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: this.config.uri,
|
||||
|
@ -50,19 +52,24 @@ export class PostgresDB implements Storage {
|
|||
});
|
||||
};
|
||||
|
||||
getEventById = async (
|
||||
getEventByVKEventId = async (
|
||||
type: VkEvent,
|
||||
id: number,
|
||||
vkEventId: number,
|
||||
vkGroupId: number,
|
||||
channel: string
|
||||
) => {
|
||||
return await this.events.findOne({
|
||||
type,
|
||||
id,
|
||||
vkEventId,
|
||||
vkGroupId,
|
||||
channel,
|
||||
});
|
||||
};
|
||||
getEventById = async (id: number) => {
|
||||
return await this.events.findOne({
|
||||
id,
|
||||
});
|
||||
};
|
||||
|
||||
createEvent = async (
|
||||
type: VkEvent,
|
||||
|
|
|
@ -60,7 +60,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
return;
|
||||
}
|
||||
|
||||
const exist = await this.getEventById(id);
|
||||
const exist = await this.getEventByVkEventId(id);
|
||||
if (exist) {
|
||||
logger.warn(
|
||||
`received duplicate entry for ${this.group.name}, ${this.type}, ${id}`
|
||||
|
@ -212,13 +212,13 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
|
||||
return this.likes.map((like) => ({
|
||||
text: withCount[like] ? `${like} ${withCount[like]}` : like,
|
||||
callback_data: `/like ${this.channel} ${like}`,
|
||||
callback_data: `/like ${this.channel.id} ${like}`,
|
||||
}));
|
||||
}
|
||||
|
||||
return this.likes.map((like) => ({
|
||||
text: like,
|
||||
callback_data: `/like ${this.channel} ${like}`,
|
||||
callback_data: `/like ${this.channel.id} ${like}`,
|
||||
}));
|
||||
};
|
||||
|
||||
|
@ -254,11 +254,16 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
const [, channel, emo] = ctx.match;
|
||||
const event = await this.getEventByTgMessageId(id);
|
||||
|
||||
if (!event) {
|
||||
logger.warn(`event not found for tgMessageId ${id}`);
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
!channel ||
|
||||
!emo ||
|
||||
!id ||
|
||||
!event ||
|
||||
channel != this.channel.id ||
|
||||
!this.likes.includes(emo)
|
||||
) {
|
||||
|
|
|
@ -56,7 +56,18 @@ export class VkEventHandler<
|
|||
return undefined;
|
||||
}
|
||||
|
||||
return await this.db.getEventById(
|
||||
return await this.db.getEventById(id);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks for duplicates
|
||||
*/
|
||||
getEventByVkEventId = async (id?: number): Promise<Event | undefined> => {
|
||||
if (!id) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return await this.db.getEventByVKEventId(
|
||||
this.type,
|
||||
id,
|
||||
this.group.id,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue