feat(decorators): TelegrafHashtag added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 17:14:34 +03:00
parent 225c931dbb
commit 68690da83e
5 changed files with 40 additions and 0 deletions

View File

@ -8,3 +8,4 @@ export * from './telegraf-settings.decorator';
export * from './telegraf-entity.decorator'; export * from './telegraf-entity.decorator';
export * from './telegraf-mention.decorator'; export * from './telegraf-mention.decorator';
export * from './telegraf-phone.decorator'; export * from './telegraf-phone.decorator';
export * from './telegraf-hashtag.decorator';

View File

@ -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 });
}

View File

@ -100,4 +100,15 @@ export class TelegrafMetadataAccessor {
getTelegrafPhoneMetadata(target: Type<any> | Function) { getTelegrafPhoneMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.PHONE, target); return this.reflector.get(DECORATORS.PHONE, target);
} }
isTelegrafHashtag(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.HASHTAG, target);
}
getTelegrafHashtagMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.HASHTAG, target);
}
} }

View File

@ -13,4 +13,5 @@ export const DECORATORS = {
ENTITY: `${DECORATORS_PREFIX}/ENTITY`, ENTITY: `${DECORATORS_PREFIX}/ENTITY`,
MENTION: `${DECORATORS_PREFIX}/MENTION`, MENTION: `${DECORATORS_PREFIX}/MENTION`,
PHONE: `${DECORATORS_PREFIX}/PHONE`, PHONE: `${DECORATORS_PREFIX}/PHONE`,
HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`,
}; };

View File

@ -77,6 +77,11 @@ export class TelegrafExplorer implements OnModuleInit {
instance[key], instance[key],
); );
this.handleTelegrafPhone(instance, key, telegraf, metadata); 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 // @ts-ignore
telegraf.phone(metadata.phone, instance[key].bind(instance)); telegraf.phone(metadata.phone, instance[key].bind(instance));
} }
handleTelegrafHashtag(
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
) {
// @ts-ignore
telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
}
} }