mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
#4 storing events in db
This commit is contained in:
parent
a78fc0bfe3
commit
a6e6209770
5 changed files with 38 additions and 12 deletions
|
@ -15,7 +15,8 @@ export interface Storage {
|
||||||
eventId: number,
|
eventId: number,
|
||||||
groupId: number,
|
groupId: number,
|
||||||
channel: string,
|
channel: string,
|
||||||
tgMessageId: number
|
tgMessageId: number,
|
||||||
|
text: Record<any, any>
|
||||||
): Promise<Event>;
|
): Promise<Event>;
|
||||||
|
|
||||||
createOrUpdateLike(like: Partial<Like>): Promise<Like>;
|
createOrUpdateLike(like: Partial<Like>): Promise<Like>;
|
||||||
|
|
|
@ -25,6 +25,6 @@ export class Event {
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
@UpdateDateColumn()
|
@UpdateDateColumn()
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
@Column({ type: "text", default: "", nullable: false })
|
@Column("simple-json", { default: {}, nullable: false })
|
||||||
text: string;
|
text: Record<any, any>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,8 @@ export class PostgresDB implements Storage {
|
||||||
eventId: number,
|
eventId: number,
|
||||||
groupId: number,
|
groupId: number,
|
||||||
channel: string,
|
channel: string,
|
||||||
tgMessageId: number
|
tgMessageId: number,
|
||||||
|
text: Record<any, any>
|
||||||
) => {
|
) => {
|
||||||
const event = this.events.create({
|
const event = this.events.create({
|
||||||
type,
|
type,
|
||||||
|
@ -55,6 +56,7 @@ export class PostgresDB implements Storage {
|
||||||
groupId,
|
groupId,
|
||||||
channel,
|
channel,
|
||||||
tgMessageId,
|
tgMessageId,
|
||||||
|
text,
|
||||||
});
|
});
|
||||||
|
|
||||||
return await this.events.save(event);
|
return await this.events.save(event);
|
||||||
|
|
|
@ -13,7 +13,10 @@ import CallbackQueryUpdate = Update.CallbackQueryUpdate;
|
||||||
|
|
||||||
type Button = "links" | "likes";
|
type Button = "links" | "likes";
|
||||||
type UrlPrefix = string;
|
type UrlPrefix = string;
|
||||||
type ExtraGenerator = (text: string) => InlineKeyboardButton[];
|
type ExtraGenerator = (
|
||||||
|
text: string,
|
||||||
|
messageId?: number
|
||||||
|
) => InlineKeyboardButton[];
|
||||||
|
|
||||||
interface Fields {
|
interface Fields {
|
||||||
image?: boolean;
|
image?: boolean;
|
||||||
|
@ -52,7 +55,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const exist = await this.getEventFromDB(id);
|
const exist = await this.getEvent(id);
|
||||||
if (exist) {
|
if (exist) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`received duplicate entry for ${this.group.name}, ${this.type}, ${id}`
|
`received duplicate entry for ${this.group.name}, ${this.type}, ${id}`
|
||||||
|
@ -85,7 +88,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
extras
|
extras
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.storeInDB(id, msg.message_id);
|
await this.createEvent(id, msg.message_id, context.wall.toJSON());
|
||||||
|
|
||||||
await next();
|
await next();
|
||||||
};
|
};
|
||||||
|
@ -100,14 +103,18 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
/**
|
/**
|
||||||
* Creates extras
|
* Creates extras
|
||||||
*/
|
*/
|
||||||
private appendExtras = (extras: ExtraReplyMessage, text: string) => {
|
private appendExtras = (
|
||||||
|
extras: ExtraReplyMessage,
|
||||||
|
text: string,
|
||||||
|
messageId?: number
|
||||||
|
) => {
|
||||||
const { buttons } = this.template.fields;
|
const { buttons } = this.template.fields;
|
||||||
if (!buttons?.length) {
|
if (!buttons?.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const keyboard = buttons
|
const keyboard = buttons
|
||||||
.map((button) => this.extrasGenerators[button](text))
|
.map((button) => this.extrasGenerators[button](text, messageId))
|
||||||
.filter((el) => el && el.length);
|
.filter((el) => el && el.length);
|
||||||
|
|
||||||
if (!keyboard.length) {
|
if (!keyboard.length) {
|
||||||
|
@ -185,11 +192,13 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
onLikeAction = async (ctx: LikeCtx, next) => {
|
onLikeAction = async (ctx: LikeCtx, next) => {
|
||||||
const id = ctx.update.callback_query.message.message_id;
|
const id = ctx.update.callback_query.message.message_id;
|
||||||
const [_, channel, emo] = ctx.match;
|
const [_, channel, emo] = ctx.match;
|
||||||
|
const exist = await this.getEvent(id);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!channel ||
|
!channel ||
|
||||||
!emo ||
|
!emo ||
|
||||||
!id ||
|
!id ||
|
||||||
|
!exist ||
|
||||||
channel != this.channel ||
|
channel != this.channel ||
|
||||||
!this.likes.includes(emo)
|
!this.likes.includes(emo)
|
||||||
) {
|
) {
|
||||||
|
@ -197,6 +206,15 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// const extras: ExtraReplyMessage = {};
|
||||||
|
// this.appendExtras(extras, exist.text);
|
||||||
|
// await ctx.telegram.editMessageReplyMarkup(
|
||||||
|
// ctx.chat.id,
|
||||||
|
// id,
|
||||||
|
// ctx.inlineMessageId,
|
||||||
|
// extras.reply_markup.inline_keyboard
|
||||||
|
// );
|
||||||
|
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`someone reacted with ${emo} to message ${id} on channel ${channel}`
|
`someone reacted with ${emo} to message ${id} on channel ${channel}`
|
||||||
);
|
);
|
||||||
|
|
|
@ -51,7 +51,7 @@ export class VkEventHandler<
|
||||||
/**
|
/**
|
||||||
* Checks for duplicates
|
* Checks for duplicates
|
||||||
*/
|
*/
|
||||||
getEventFromDB = async (id?: number): Promise<Event | undefined> => {
|
getEvent = async (id?: number): Promise<Event | undefined> => {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
@ -62,13 +62,18 @@ export class VkEventHandler<
|
||||||
/**
|
/**
|
||||||
* Creates event record in DB
|
* Creates event record in DB
|
||||||
*/
|
*/
|
||||||
storeInDB = async (id: number, tgMessageId: number) => {
|
createEvent = async (
|
||||||
|
id: number,
|
||||||
|
tgMessageId: number,
|
||||||
|
text: Record<any, any>
|
||||||
|
) => {
|
||||||
return await this.db.createEvent(
|
return await this.db.createEvent(
|
||||||
this.type,
|
this.type,
|
||||||
id,
|
id,
|
||||||
this.group.id,
|
this.group.id,
|
||||||
this.channel,
|
this.channel,
|
||||||
tgMessageId
|
tgMessageId,
|
||||||
|
text
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue