mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-30 04:18:51 +03:00
feat: add GlobalUpdate decorator (#2)
This commit is contained in:
@@ -48,8 +48,6 @@ export class ListenersExplorerService
|
||||
this.bot = this.moduleRef.get<Telegraf<any>>(this.botName, {
|
||||
strict: false,
|
||||
});
|
||||
this.bot.use(this.stage.middleware());
|
||||
|
||||
this.explore();
|
||||
}
|
||||
|
||||
@@ -59,10 +57,23 @@ export class ListenersExplorerService
|
||||
this.telegrafOptions.include || [],
|
||||
);
|
||||
|
||||
this.registerGlobalUpdates(modules);
|
||||
|
||||
this.bot.use(this.stage.middleware());
|
||||
|
||||
this.registerUpdates(modules);
|
||||
this.registerScenes(modules);
|
||||
}
|
||||
|
||||
private registerGlobalUpdates(modules: Module[]): void {
|
||||
const globalUpdates = this.flatMap<InstanceWrapper>(modules, (instance) =>
|
||||
this.filterGlobalUpdates(instance),
|
||||
);
|
||||
globalUpdates.forEach((wrapper) =>
|
||||
this.registerListeners(this.bot, wrapper),
|
||||
);
|
||||
}
|
||||
|
||||
private registerUpdates(modules: Module[]): void {
|
||||
const updates = this.flatMap<InstanceWrapper>(modules, (instance) =>
|
||||
this.filterUpdates(instance),
|
||||
@@ -92,6 +103,20 @@ export class ListenersExplorerService
|
||||
});
|
||||
}
|
||||
|
||||
private filterGlobalUpdates(
|
||||
wrapper: InstanceWrapper,
|
||||
): InstanceWrapper<unknown> {
|
||||
const { instance } = wrapper;
|
||||
if (!instance) return undefined;
|
||||
|
||||
const isGlobalUpdate = this.metadataAccessor.isGlobalUpdate(
|
||||
wrapper.metatype,
|
||||
);
|
||||
if (!isGlobalUpdate) return undefined;
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
private filterUpdates(wrapper: InstanceWrapper): InstanceWrapper<unknown> {
|
||||
const { instance } = wrapper;
|
||||
if (!instance) return undefined;
|
||||
|
@@ -3,6 +3,7 @@ import { Reflector } from '@nestjs/core';
|
||||
import {
|
||||
SCENE_METADATA,
|
||||
LISTENERS_METADATA,
|
||||
GLOBAL_UPDATE_METADATA,
|
||||
UPDATE_METADATA,
|
||||
WIZARD_STEP_METADATA,
|
||||
} from '../telegraf.constants';
|
||||
@@ -16,6 +17,11 @@ import {
|
||||
export class MetadataAccessorService {
|
||||
constructor(private readonly reflector: Reflector) {}
|
||||
|
||||
isGlobalUpdate(target: Function): boolean {
|
||||
if (!target) return false;
|
||||
return !!this.reflector.get(GLOBAL_UPDATE_METADATA, target);
|
||||
}
|
||||
|
||||
isUpdate(target: Function): boolean {
|
||||
if (!target) return false;
|
||||
return !!this.reflector.get(UPDATE_METADATA, target);
|
||||
|
Reference in New Issue
Block a user