mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 06:56:40 +07:00
#4 added simple DB layer
This commit is contained in:
parent
f3a746efe8
commit
c0588acd26
15 changed files with 476 additions and 11 deletions
|
@ -2,6 +2,7 @@ import { TelegramConfig } from "../service/telegram/types";
|
|||
import { VkConfig, VkEvent } from "../service/vk/types";
|
||||
import { HttpConfig } from "../api/http/types";
|
||||
import { LoggerConfig } from "../service/logger/types";
|
||||
import { PostgresConfig } from "../service/db/postgres/types";
|
||||
|
||||
export type TemplateConfig = Record<VkEvent, string>;
|
||||
|
||||
|
@ -11,4 +12,5 @@ export interface Config extends Record<string, any> {
|
|||
vk: VkConfig;
|
||||
logger?: LoggerConfig;
|
||||
templates?: TemplateConfig;
|
||||
postgres?: PostgresConfig;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import { Config } from "./types";
|
|||
import { vkConfigSchema } from "../service/vk/validation";
|
||||
import { telegramConfigSchema } from "../service/telegram/validation";
|
||||
import { loggerConfigSchema } from "../service/logger/config";
|
||||
import { dbConfigValidatior } from "../service/db/postgres/validation";
|
||||
|
||||
const templateConfigSchema = object().shape({
|
||||
message_new: string().required(),
|
||||
|
@ -15,6 +16,7 @@ const configSchema = object<Config>().required().shape({
|
|||
telegram: telegramConfigSchema,
|
||||
logger: loggerConfigSchema,
|
||||
templates: templateConfigSchema,
|
||||
postgres: dbConfigValidatior,
|
||||
});
|
||||
|
||||
export const validateConfig = (config: Config) =>
|
||||
|
|
|
@ -4,12 +4,17 @@ import logger from "./service/logger";
|
|||
import { VkService } from "./service/vk";
|
||||
import { TelegramApi } from "./api/telegram";
|
||||
import { HttpApi } from "./api/http";
|
||||
import { PostgresDB } from "./service/db/postgres";
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const config = prepareConfig();
|
||||
|
||||
const db = new PostgresDB(config.postgres);
|
||||
await db.connect();
|
||||
|
||||
const telegram = new TelegramService(config.telegram);
|
||||
const vkService = new VkService(config.vk, telegram, config.templates);
|
||||
const vkService = new VkService(config.vk, telegram, config.templates, db);
|
||||
|
||||
const telegramApi = new TelegramApi(telegram).listen();
|
||||
await telegram.start();
|
||||
|
|
17
src/service/db/index.ts
Normal file
17
src/service/db/index.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { VkEvent } from "../vk/types";
|
||||
import { Event, Like } from "./types";
|
||||
|
||||
export interface Storage {
|
||||
getEvent(
|
||||
type: VkEvent,
|
||||
id: number,
|
||||
groupId: number,
|
||||
channel: string
|
||||
): Promise<Event>;
|
||||
|
||||
createEvent(event: Event): Promise<Event>;
|
||||
|
||||
getLikesFor(channel: string, messageId: number): Promise<Like[]>;
|
||||
|
||||
getLikeBy(channel: string, messageId: number, author: number): Promise<Like>;
|
||||
}
|
47
src/service/db/postgres/index.ts
Normal file
47
src/service/db/postgres/index.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import { Storage } from "../index";
|
||||
import { VkEvent } from "../../vk/types";
|
||||
import { Event } from "../types";
|
||||
import { PostgresConfig } from "./types";
|
||||
import { Connection, createConnection } from "typeorm";
|
||||
|
||||
export class PostgresDB implements Storage {
|
||||
private connection: Connection;
|
||||
constructor(private config: PostgresConfig) {}
|
||||
|
||||
connect = async () => {
|
||||
this.connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: this.config.uri,
|
||||
});
|
||||
};
|
||||
|
||||
getEvent = async (
|
||||
type: VkEvent,
|
||||
id: number,
|
||||
groupId: number,
|
||||
channel: string
|
||||
) => {
|
||||
return {
|
||||
type,
|
||||
id,
|
||||
groupId,
|
||||
channel,
|
||||
tgMessageId: 0,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
};
|
||||
|
||||
createEvent = async (event: Event) => event;
|
||||
|
||||
getLikesFor = async (channel: string, messageId: number) => [];
|
||||
|
||||
getLikeBy = async (channel: string, messageId: number, author: number) => ({
|
||||
channel,
|
||||
messageId,
|
||||
author,
|
||||
text: "",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
}
|
3
src/service/db/postgres/types.ts
Normal file
3
src/service/db/postgres/types.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export interface PostgresConfig {
|
||||
uri: string;
|
||||
}
|
7
src/service/db/postgres/validation.ts
Normal file
7
src/service/db/postgres/validation.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { object, string } from "yup";
|
||||
|
||||
export const dbConfigValidatior = object().shape({
|
||||
uri: string()
|
||||
.required()
|
||||
.matches(/^postgres:/),
|
||||
});
|
20
src/service/db/types.ts
Normal file
20
src/service/db/types.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { VkEvent } from "../vk/types";
|
||||
|
||||
export interface Event {
|
||||
type: VkEvent;
|
||||
id: number;
|
||||
groupId: number;
|
||||
channel: string;
|
||||
tgMessageId: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface Like {
|
||||
messageId: number;
|
||||
channel: string;
|
||||
text: string;
|
||||
author: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
|
@ -3,6 +3,7 @@ import { ConfigGroup, GroupInstance, VkEvent } from "../types";
|
|||
import { VkService } from "../index";
|
||||
import { TelegramService } from "../../telegram";
|
||||
import { Template } from "../../template";
|
||||
import { Storage } from "../../db";
|
||||
|
||||
export class VkEventHandler<
|
||||
F extends Record<string, any> = any,
|
||||
|
@ -15,7 +16,8 @@ export class VkEventHandler<
|
|||
protected instance: GroupInstance,
|
||||
protected vk: VkService,
|
||||
protected telegram: TelegramService,
|
||||
protected template: Template<F, V>
|
||||
protected template: Template<F, V>,
|
||||
protected db: Storage
|
||||
) {}
|
||||
|
||||
public execute: (
|
||||
|
|
|
@ -6,6 +6,7 @@ import { VkService } from "../index";
|
|||
import { TelegramService } from "../../telegram";
|
||||
import { Template } from "../../template";
|
||||
import { PostNewHandler } from "./PostNewHandler";
|
||||
import { Storage } from "../../db";
|
||||
|
||||
interface Handler {
|
||||
new (
|
||||
|
@ -15,7 +16,8 @@ interface Handler {
|
|||
instance: GroupInstance,
|
||||
vk: VkService,
|
||||
telegram: TelegramService,
|
||||
template: Template<any, any>
|
||||
template: Template<any, any>,
|
||||
db: Storage
|
||||
): VkEventHandler;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import { vkEventToHandler } from "./handlers";
|
|||
import { TelegramService } from "../telegram";
|
||||
import { Template } from "../template";
|
||||
import { TemplateConfig } from "../../config/types";
|
||||
import { Storage } from "../db";
|
||||
|
||||
/**
|
||||
* Service to handle VK to Telegram interactions
|
||||
|
@ -21,7 +22,8 @@ export class VkService {
|
|||
constructor(
|
||||
private config: VkConfig,
|
||||
private telegram: TelegramService,
|
||||
private templates: TemplateConfig
|
||||
private templates: TemplateConfig,
|
||||
private db: Storage
|
||||
) {
|
||||
if (!config.groups.length) {
|
||||
throw new Error("No vk groups to handle. Specify them in config");
|
||||
|
@ -122,7 +124,8 @@ export class VkService {
|
|||
instance,
|
||||
this,
|
||||
this.telegram,
|
||||
template
|
||||
template,
|
||||
this.db
|
||||
);
|
||||
return { ...acc, [event]: handler };
|
||||
}, {} as Record<VkEvent, VkEventHandler>[])
|
||||
|
|
|
@ -14,7 +14,7 @@ export interface ConfigGroup {
|
|||
channels: GroupChannel[];
|
||||
}
|
||||
|
||||
interface GroupChannel {
|
||||
export interface GroupChannel {
|
||||
id: string;
|
||||
events: VkEvent[];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue