2020-12-29 22:41:06 +03:00
|
|
|
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
|
2021-01-03 01:30:57 +03:00
|
|
|
import { Injectable as IInjectable } from '@nestjs/common/interfaces/injectable.interface';
|
|
|
|
import { DiscoveryService, ModuleRef, ModulesContainer } from '@nestjs/core';
|
2020-03-19 16:21:35 +03:00
|
|
|
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
2020-12-27 21:35:01 +03:00
|
|
|
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
2021-01-03 01:30:57 +03:00
|
|
|
import { PipesContextCreator } from '@nestjs/core/pipes/pipes-context-creator';
|
|
|
|
import { PipesConsumer } from '@nestjs/core/pipes/pipes-consumer';
|
|
|
|
import { GuardsContextCreator } from '@nestjs/core/guards/guards-context-creator';
|
|
|
|
import { GuardsConsumer } from '@nestjs/core/guards/guards-consumer';
|
|
|
|
import { InterceptorsContextCreator } from '@nestjs/core/interceptors/interceptors-context-creator';
|
|
|
|
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';
|
|
|
|
import { isFunction, isNil } from '@nestjs/common/utils/shared.utils';
|
|
|
|
import { fromPromise } from 'rxjs/internal-compatibility';
|
|
|
|
import { filter, mergeAll } from 'rxjs/operators';
|
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
import { Context, Telegraf } from 'telegraf';
|
2020-12-30 01:49:09 +03:00
|
|
|
import { TelegrafMetadataAccessor } from '../telegraf.metadata-accessor';
|
2021-01-03 01:30:57 +03:00
|
|
|
import { TelegrafContextCreator } from '../context/telegraf-context-creator';
|
|
|
|
import { TelegrafProxy } from '../context/telegraf-proxy';
|
|
|
|
import { FiltersContextCreator } from '../context/filters-context-creator';
|
2020-03-19 16:21:35 +03:00
|
|
|
|
|
|
|
@Injectable()
|
2020-12-29 22:41:06 +03:00
|
|
|
export class TelegrafUpdateExplorer implements OnModuleInit {
|
2021-01-03 01:30:57 +03:00
|
|
|
private readonly contextCreator: TelegrafContextCreator;
|
|
|
|
|
2020-03-19 16:21:35 +03:00
|
|
|
constructor(
|
2021-01-03 01:30:57 +03:00
|
|
|
private readonly moduleRef: ModuleRef,
|
|
|
|
private readonly modulesContainer: ModulesContainer,
|
2020-03-19 16:21:35 +03:00
|
|
|
private readonly discoveryService: DiscoveryService,
|
|
|
|
private readonly metadataAccessor: TelegrafMetadataAccessor,
|
|
|
|
private readonly metadataScanner: MetadataScanner,
|
2021-01-03 01:30:57 +03:00
|
|
|
@Inject(Telegraf) private readonly telegraf: Telegraf<never>,
|
|
|
|
) {
|
|
|
|
this.contextCreator = this.getContextCreator();
|
|
|
|
}
|
|
|
|
|
|
|
|
private getContextCreator(): TelegrafContextCreator {
|
|
|
|
const { container } = this.moduleRef as any;
|
|
|
|
return new TelegrafContextCreator(
|
|
|
|
new TelegrafProxy(),
|
|
|
|
new FiltersContextCreator(container),
|
|
|
|
new PipesContextCreator(container),
|
|
|
|
new PipesConsumer(),
|
|
|
|
new GuardsContextCreator(container),
|
|
|
|
new GuardsConsumer(),
|
|
|
|
new InterceptorsContextCreator(container),
|
|
|
|
new InterceptorsConsumer(),
|
|
|
|
);
|
|
|
|
}
|
2020-03-19 16:21:35 +03:00
|
|
|
|
2020-12-25 23:11:16 +03:00
|
|
|
onModuleInit(): void {
|
2020-03-19 16:21:35 +03:00
|
|
|
this.explore();
|
|
|
|
}
|
|
|
|
|
2020-12-29 22:41:06 +03:00
|
|
|
private explore(): void {
|
2021-01-03 01:30:57 +03:00
|
|
|
this.modulesContainer.forEach(({ providers }, moduleName) => {
|
|
|
|
this.exploreProviders(providers, moduleName);
|
2020-12-26 16:11:09 +03:00
|
|
|
});
|
2020-12-25 23:11:16 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 01:30:57 +03:00
|
|
|
private exploreProviders(
|
|
|
|
providers: Map<string, InstanceWrapper<IInjectable>>,
|
|
|
|
moduleName: string,
|
|
|
|
): void {
|
|
|
|
[...providers.values()]
|
|
|
|
.filter((wrapper) => wrapper && !wrapper.isNotMetatype)
|
|
|
|
.forEach((wrapper) => {
|
|
|
|
const { instance } = wrapper;
|
|
|
|
|
|
|
|
const prototype = Object.getPrototypeOf(instance);
|
|
|
|
this.metadataScanner.scanFromPrototype(
|
|
|
|
instance,
|
|
|
|
prototype,
|
|
|
|
(methodKey: string) =>
|
|
|
|
this.registerIfListener(
|
|
|
|
instance as Record<string, Function>,
|
|
|
|
methodKey,
|
|
|
|
moduleName,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2020-12-26 14:54:10 +03:00
|
|
|
}
|
|
|
|
|
2020-12-26 16:24:38 +03:00
|
|
|
private registerIfListener(
|
2020-12-25 23:11:16 +03:00
|
|
|
instance: Record<string, Function>,
|
|
|
|
methodKey: string,
|
2021-01-03 01:30:57 +03:00
|
|
|
moduleName: string,
|
2020-12-25 23:11:16 +03:00
|
|
|
): void {
|
2021-01-03 01:30:57 +03:00
|
|
|
const methodRef = instance[methodKey] as (...args: unknown[]) => unknown;
|
|
|
|
const contextHandlerFn = this.contextCreator.create(
|
|
|
|
instance,
|
|
|
|
methodRef,
|
|
|
|
moduleName,
|
|
|
|
methodKey,
|
|
|
|
);
|
2020-12-25 23:11:16 +03:00
|
|
|
|
2020-12-27 21:35:01 +03:00
|
|
|
const listenerMetadata = this.metadataAccessor.getListenerMetadata(
|
|
|
|
methodRef,
|
|
|
|
);
|
|
|
|
if (!listenerMetadata) return;
|
|
|
|
|
|
|
|
const { method, args } = listenerMetadata;
|
2021-01-03 01:30:57 +03:00
|
|
|
this.telegraf[method](
|
|
|
|
...args,
|
|
|
|
async (ctx: Context, next: () => Promise<void>) => {
|
|
|
|
const defferedResult = contextHandlerFn.call(instance, ctx, next);
|
|
|
|
const result = this.pickResult(defferedResult);
|
|
|
|
fromPromise(result)
|
|
|
|
.pipe(
|
|
|
|
mergeAll(),
|
|
|
|
filter((response: any) => !isNil(response)),
|
|
|
|
)
|
|
|
|
.subscribe((text) => {
|
|
|
|
// TODO: More processing method return logic (files, images, etc)
|
|
|
|
// Example: https://github.com/nestjs/nest/blob/01dc358aade27d3d7ca510506696aa62bfb1cc43/packages/platform-socket.io/adapters/io-adapter.ts#L56
|
|
|
|
return ctx.reply(text);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async pickResult(
|
|
|
|
defferedResult: Promise<any>,
|
|
|
|
): Promise<Observable<any>> {
|
|
|
|
const result = await defferedResult;
|
|
|
|
|
|
|
|
if (result && isFunction(result.subscribe)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
return fromPromise(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return of(result);
|
2020-03-19 17:25:47 +03:00
|
|
|
}
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|