From 68690da83e3a8a1764ec89c064ea0f27bceb4714 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:14:34 +0300 Subject: [PATCH] feat(decorators): TelegrafHashtag added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-hashtag.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 lib/decorators/telegraf-hashtag.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 759e533..8a61847 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -8,3 +8,4 @@ export * from './telegraf-settings.decorator'; export * from './telegraf-entity.decorator'; export * from './telegraf-mention.decorator'; export * from './telegraf-phone.decorator'; +export * from './telegraf-hashtag.decorator'; diff --git a/lib/decorators/telegraf-hashtag.decorator.ts b/lib/decorators/telegraf-hashtag.decorator.ts new file mode 100644 index 0000000..7ab3942 --- /dev/null +++ b/lib/decorators/telegraf-hashtag.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Hashtag handling. + * @param hashtag Hashtag + * + * https://telegraf.js.org/#/?id=hashtag + */ +export function TelegrafHashtag(hashtag: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.HASHTAG, { hashtag }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index df440f7..ba2aa76 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -100,4 +100,15 @@ export class TelegrafMetadataAccessor { getTelegrafPhoneMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.PHONE, target); } + + isTelegrafHashtag(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.HASHTAG, target); + } + + getTelegrafHashtagMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.HASHTAG, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 80f8de8..3617ba7 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -13,4 +13,5 @@ export const DECORATORS = { ENTITY: `${DECORATORS_PREFIX}/ENTITY`, MENTION: `${DECORATORS_PREFIX}/MENTION`, PHONE: `${DECORATORS_PREFIX}/PHONE`, + HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index f85f1dd..30cd359 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -77,6 +77,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafPhone(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafHashtagMetadata( + instance[key], + ); + this.handleTelegrafHashtag(instance, key, telegraf, metadata); } }, ); @@ -172,4 +177,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.phone(metadata.phone, instance[key].bind(instance)); } + + handleTelegrafHashtag( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.hashtag(metadata.hashtag, instance[key].bind(instance)); + } }