feat(decorators): TelegrafCashtag added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 17:16:48 +03:00
parent 68690da83e
commit 7d3e266f0f
5 changed files with 40 additions and 0 deletions

View File

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

View File

@ -0,0 +1,12 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
/**
* Cashtag handling.
* @param cashtag Cashtag
*
* https://telegraf.js.org/#/?id=cashtag
*/
export function TelegrafCashtag(cashtag: string | string[]): MethodDecorator {
return SetMetadata(DECORATORS.CASHTAG, { cashtag });
}

View File

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

View File

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

View File

@ -82,6 +82,11 @@ export class TelegrafExplorer implements OnModuleInit {
instance[key],
);
this.handleTelegrafHashtag(instance, key, telegraf, metadata);
} else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafCashtagMetadata(
instance[key],
);
this.handleTelegrafCashtag(instance, key, telegraf, metadata);
}
},
);
@ -187,4 +192,14 @@ export class TelegrafExplorer implements OnModuleInit {
// @ts-ignore
telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
}
handleTelegrafCashtag(
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
) {
// @ts-ignore
telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
}
}