diff --git a/config.example.yml b/config.example.yml index 6521fef..ecfc5bc 100644 --- a/config.example.yml +++ b/config.example.yml @@ -14,7 +14,7 @@ vk: # Default path for POST requests from VK api endpoint: / groups: [] -templates: +templates: # (required) message_new: templates/message_new.md wall_post_new: templates/post_new.md group_join: templates/group_join.md @@ -26,6 +26,8 @@ templates: # secretKey: 'groupSecretKey' # apiKey: 'callbackApiKey' # post_types: ['post','copy','reply','postpone','suggest'] +# templates: # group's custom templates (optional) +# message_new: templates/custom/message_new.md # channels: # - id: '@pogonia_test_chan' # events: @@ -34,4 +36,5 @@ templates: # - group_join # - group_leave # - message_new - +# templates: # channel custom templates (optional) +# wall_post_new: templates/custom/post_new.md diff --git a/src/config/validate.ts b/src/config/validate.ts index 2b1ab8f..429ccc3 100644 --- a/src/config/validate.ts +++ b/src/config/validate.ts @@ -6,8 +6,18 @@ import { telegramConfigSchema } from "../service/telegram/validation"; import { loggerConfigSchema } from "../service/logger/config"; import { dbConfigValidatior } from "../service/db/postgres/validation"; -const templateConfigSchema = object().shape({ +export const templateConfigSchema = object().required().shape({ message_new: string().required(), + wall_post_new: string().required(), + group_join: string().required(), + group_leave: string().required(), +}); + +export const templateOptionalSchema = object().shape({ + message_new: string(), + wall_post_new: string(), + group_join: string(), + group_leave: string(), }); const configSchema = object().required().shape({ diff --git a/src/service/vk/index.ts b/src/service/vk/index.ts index 0e19853..5d86e51 100644 --- a/src/service/vk/index.ts +++ b/src/service/vk/index.ts @@ -2,7 +2,7 @@ import { ConfigGroup, GroupInstance, VkConfig, VkEvent } from "./types"; import { API, Updates, Upload } from "vk-io"; import logger from "../logger"; import { Request, Response } from "express"; -import { flatten, has, keys } from "ramda"; +import { flatten, has, keys, prop } from "ramda"; import { NextFunction } from "connect"; import { VkEventHandler } from "./handlers/VkEventHandler"; import { vkEventToHandler } from "./handlers"; @@ -137,7 +137,11 @@ export class VkService { return flatten( group.channels.map((chan) => chan.events.reduce((acc, event) => { - const template = new Template(this.templates[event]); + const template = new Template( + prop(event, chan?.templates) || + prop(event, group?.templates) || + prop(event, this.templates) + ); const handler = new vkEventToHandler[event]( event, diff --git a/src/service/vk/types.ts b/src/service/vk/types.ts index 9a438df..ab4e6e8 100644 --- a/src/service/vk/types.ts +++ b/src/service/vk/types.ts @@ -1,5 +1,6 @@ import { API, Upload, Updates } from "vk-io"; import { WallPostType } from "vk-io/lib/api/schemas/objects"; +import { TemplateConfig } from "../../config/types"; export interface VkConfig extends Record { groups: ConfigGroup[]; @@ -13,12 +14,14 @@ export interface ConfigGroup { secretKey: string; apiKey: string; channels: GroupChannel[]; + templates: Partial; } export interface GroupChannel { id: string; events: VkEvent[]; post_types: WallPostType[]; + templates: Partial; } export enum VkEvent { diff --git a/src/service/vk/validation.ts b/src/service/vk/validation.ts index f379115..4c10e0c 100644 --- a/src/service/vk/validation.ts +++ b/src/service/vk/validation.ts @@ -1,5 +1,6 @@ import * as yup from "yup"; import { VkConfig, VkEvent } from "./types"; +import { templateOptionalSchema } from "../../config/validate"; const vkChannelEventSchema = yup.string().oneOf(Object.values(VkEvent)); @@ -12,6 +13,7 @@ const vkChannelSchema = yup .required() .matches(/^@/, ({ path }) => `${path} should start with "@"`), events: yup.array().of(vkChannelEventSchema), + templates: templateOptionalSchema, }); export const vkConfigSchema = yup @@ -30,6 +32,7 @@ export const vkConfigSchema = yup secretKey: yup.string().required(), apiKey: yup.string().required(), channels: yup.array().of(vkChannelSchema), + templates: templateOptionalSchema, }) ), });