2021-01-02 01:32:49 +03:00
|
|
|
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
|
2021-01-02 01:27:01 +03:00
|
|
|
import { DiscoveryService, ModuleRef, ModulesContainer } from '@nestjs/core';
|
2020-03-19 16:21:35 +03:00
|
|
|
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
|
|
|
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
2021-01-02 01:27:01 +03:00
|
|
|
import { MetadataAccessorService } from './metadata-accessor.service';
|
|
|
|
import {
|
|
|
|
TELEGRAF_BOT_NAME,
|
|
|
|
TELEGRAF_MODULE_OPTIONS,
|
|
|
|
} from '../telegraf.constants';
|
|
|
|
import { Telegraf } from 'telegraf';
|
|
|
|
import { TelegrafModuleOptions } from '../interfaces';
|
|
|
|
import { BaseExplorerService } from './base-explorer.service';
|
|
|
|
import { Module } from '@nestjs/core/injector/module';
|
2020-03-19 16:21:35 +03:00
|
|
|
|
|
|
|
@Injectable()
|
2021-01-02 01:27:01 +03:00
|
|
|
export class UpdatesExplorerService
|
|
|
|
extends BaseExplorerService
|
|
|
|
implements OnModuleInit {
|
2020-03-19 16:21:35 +03:00
|
|
|
constructor(
|
2021-01-02 01:27:01 +03:00
|
|
|
@Inject(TELEGRAF_BOT_NAME)
|
|
|
|
private readonly botName: string,
|
|
|
|
@Inject(TELEGRAF_MODULE_OPTIONS)
|
|
|
|
private readonly telegrafModuleOptions: TelegrafModuleOptions,
|
2020-03-19 16:21:35 +03:00
|
|
|
private readonly moduleRef: ModuleRef,
|
|
|
|
private readonly discoveryService: DiscoveryService,
|
2021-01-02 01:27:01 +03:00
|
|
|
private readonly metadataAccessor: MetadataAccessorService,
|
2020-03-19 16:21:35 +03:00
|
|
|
private readonly metadataScanner: MetadataScanner,
|
2021-01-02 01:27:01 +03:00
|
|
|
private readonly modulesContainer: ModulesContainer,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
2020-03-19 16:21:35 +03:00
|
|
|
|
2021-01-02 01:27:01 +03:00
|
|
|
private bot: Telegraf<any>;
|
2020-09-09 22:46:25 +03:00
|
|
|
|
2021-01-02 01:27:01 +03:00
|
|
|
onModuleInit(): void {
|
|
|
|
this.bot = this.moduleRef.get<Telegraf<any>>(this.botName, {
|
2020-09-09 22:46:25 +03:00
|
|
|
strict: false,
|
|
|
|
});
|
2020-03-19 16:21:35 +03:00
|
|
|
this.explore();
|
|
|
|
}
|
|
|
|
|
|
|
|
explore() {
|
2021-01-02 01:27:01 +03:00
|
|
|
const modules = this.getModules(
|
|
|
|
this.modulesContainer,
|
|
|
|
this.telegrafModuleOptions.include || [],
|
|
|
|
);
|
|
|
|
const updates = this.flatMap(modules, (instance, moduleRef) =>
|
|
|
|
this.applyUpdates(instance, moduleRef),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private applyUpdates(wrapper: InstanceWrapper, moduleRef: Module) {
|
|
|
|
const { instance } = wrapper;
|
|
|
|
if (!instance) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const prototype = Object.getPrototypeOf(instance);
|
|
|
|
|
|
|
|
const providers: InstanceWrapper[] = this.discoveryService.getProviders();
|
|
|
|
const updateProviders: InstanceWrapper[] = providers.filter(
|
|
|
|
(wrapper: InstanceWrapper) =>
|
2020-09-09 22:46:25 +03:00
|
|
|
this.metadataAccessor.isUpdate(wrapper.metatype),
|
2021-01-02 01:27:01 +03:00
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
|
|
|
|
updateProviders.forEach((wrapper: InstanceWrapper) => {
|
|
|
|
const { instance } = wrapper;
|
2021-01-02 01:27:01 +03:00
|
|
|
if (!instance) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
this.metadataScanner.scanFromPrototype(instance, prototype, (name) => {
|
2021-01-02 21:40:13 +03:00
|
|
|
const methodRef = instance[name];
|
|
|
|
if (this.metadataAccessor.isUpdateListener(methodRef)) {
|
|
|
|
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
|
|
|
|
const middlewareFn = methodRef.bind(instance);
|
|
|
|
const { method, args } = metadata;
|
|
|
|
// NOTE: Use "any" to disable "Expected at least 1 arguments, but got 1 or more." error.
|
|
|
|
// Use telegraf instance for non-scene listeners
|
|
|
|
(this.bot[method] as any)(...args, middlewareFn);
|
2021-01-02 01:27:01 +03:00
|
|
|
}
|
|
|
|
});
|
2020-09-09 22:46:25 +03:00
|
|
|
});
|
2020-03-19 17:28:56 +03:00
|
|
|
}
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|