mirror of
https://github.com/hexastack/hexabot
synced 2025-04-02 20:31:33 +00:00
Merge pull request #442 from Hexastack/fix/channel-data-typing
Some checks are pending
Build and Push Docker API Image / build-and-push (push) Waiting to run
Build and Push Docker Base Image / build-and-push (push) Waiting to run
Build and Push Docker NLU Image / build-and-push (push) Waiting to run
Build and Push Docker UI Image / build-and-push (push) Waiting to run
Some checks are pending
Build and Push Docker API Image / build-and-push (push) Waiting to run
Build and Push Docker Base Image / build-and-push (push) Waiting to run
Build and Push Docker NLU Image / build-and-push (push) Waiting to run
Build and Push Docker UI Image / build-and-push (push) Waiting to run
Fix/channel data typing
This commit is contained in:
commit
3a558dfb17
@ -20,7 +20,9 @@ import {
|
||||
import { Payload } from '@/chat/schemas/types/quick-reply';
|
||||
import { NLU } from '@/helper/types';
|
||||
|
||||
import ChannelHandler, { ChannelNameOf } from './Handler';
|
||||
import { ChannelName } from '../types';
|
||||
|
||||
import ChannelHandler from './Handler';
|
||||
|
||||
export interface ChannelEvent {}
|
||||
|
||||
@ -28,13 +30,15 @@ export interface ChannelEvent {}
|
||||
export default abstract class EventWrapper<
|
||||
A,
|
||||
E,
|
||||
C extends ChannelHandler = ChannelHandler,
|
||||
N extends ChannelName = ChannelName,
|
||||
C extends ChannelHandler = ChannelHandler<N>,
|
||||
S = SubscriberChannelDict[N],
|
||||
> {
|
||||
_adapter: A = {} as A;
|
||||
|
||||
_handler: C;
|
||||
|
||||
channelAttrs: SubscriberChannelDict[ChannelNameOf<C>];
|
||||
channelAttrs: S;
|
||||
|
||||
_profile!: Subscriber;
|
||||
|
||||
@ -50,11 +54,7 @@ export default abstract class EventWrapper<
|
||||
* @param event - The message event received
|
||||
* @param channelAttrs - Channel's specific data
|
||||
*/
|
||||
constructor(
|
||||
handler: C,
|
||||
event: E,
|
||||
channelAttrs: SubscriberChannelDict[ChannelNameOf<C>] = {},
|
||||
) {
|
||||
constructor(handler: C, event: E, channelAttrs: S = {} as S) {
|
||||
this._handler = handler;
|
||||
this._init(event);
|
||||
this.channelAttrs = channelAttrs;
|
||||
@ -106,11 +106,11 @@ export default abstract class EventWrapper<
|
||||
*
|
||||
* @returns Returns any channel related data.
|
||||
*/
|
||||
getChannelData(): SubscriberChannelData<ChannelNameOf<C>> {
|
||||
getChannelData(): SubscriberChannelData<N> {
|
||||
return {
|
||||
name: this._handler.getName(),
|
||||
...this.channelAttrs,
|
||||
} as SubscriberChannelData<ChannelNameOf<C>>;
|
||||
} as SubscriberChannelData<N>;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -267,7 +267,8 @@ type GenericEventAdapter = {
|
||||
|
||||
export class GenericEventWrapper extends EventWrapper<
|
||||
GenericEventAdapter,
|
||||
GenericEvent
|
||||
GenericEvent,
|
||||
ChannelName
|
||||
> {
|
||||
/**
|
||||
* Constructor : channel's event wrapper
|
||||
|
@ -28,8 +28,6 @@ import { ChannelName, ChannelSetting } from '../types';
|
||||
|
||||
import EventWrapper from './EventWrapper';
|
||||
|
||||
export type ChannelNameOf<C> = C extends ChannelHandler<infer N> ? N : never;
|
||||
|
||||
@Injectable()
|
||||
export default abstract class ChannelHandler<
|
||||
N extends ChannelName = ChannelName,
|
||||
@ -176,7 +174,7 @@ export default abstract class ChannelHandler<
|
||||
|
||||
*/
|
||||
abstract sendMessage(
|
||||
event: EventWrapper<any, any>,
|
||||
event: EventWrapper<any, any, N>,
|
||||
envelope: StdOutgoingEnvelope,
|
||||
options: any,
|
||||
context: any,
|
||||
@ -189,7 +187,7 @@ export default abstract class ChannelHandler<
|
||||
|
||||
*/
|
||||
abstract getUserData(
|
||||
event: EventWrapper<any, any>,
|
||||
event: EventWrapper<any, any, N>,
|
||||
): Promise<SubscriberCreateDto>;
|
||||
|
||||
/**
|
||||
|
@ -10,12 +10,12 @@ import { ChannelName } from '@/channel/types';
|
||||
|
||||
export type SubscriberChannelData<
|
||||
C extends ChannelName = null,
|
||||
K extends keyof SubscriberChannelDict[C] = keyof SubscriberChannelDict[C],
|
||||
// K extends keyof SubscriberChannelDict[C] = keyof SubscriberChannelDict[C],
|
||||
> = C extends null
|
||||
? { name: ChannelName }
|
||||
: {
|
||||
name: C;
|
||||
} & {
|
||||
// Channel's specific attributes
|
||||
[P in keyof SubscriberChannelDict[C]]: SubscriberChannelDict[C][K];
|
||||
[P in keyof SubscriberChannelDict[C]]: SubscriberChannelDict[C][P];
|
||||
};
|
||||
|
@ -26,6 +26,7 @@ import { ContentService } from '@/cms/services/content.service';
|
||||
import { MenuService } from '@/cms/services/menu.service';
|
||||
import { webEventText } from '@/extensions/channels/web/__test__/events.mock';
|
||||
import WebChannelHandler from '@/extensions/channels/web/index.channel';
|
||||
import { WEB_CHANNEL_NAME } from '@/extensions/channels/web/settings';
|
||||
import WebEventWrapper from '@/extensions/channels/web/wrapper';
|
||||
import { HelperService } from '@/helper/helper.service';
|
||||
import { LanguageRepository } from '@/i18n/repositories/language.repository';
|
||||
@ -201,7 +202,7 @@ describe('BlockService', () => {
|
||||
.spyOn(botService, 'findBlockAndSendReply')
|
||||
.mockImplementation(
|
||||
(
|
||||
actualEvent: WebEventWrapper,
|
||||
actualEvent: WebEventWrapper<typeof WEB_CHANNEL_NAME>,
|
||||
actualConversation: Conversation,
|
||||
actualBlock: BlockFull,
|
||||
isFallback: boolean,
|
||||
@ -267,7 +268,7 @@ describe('BlockService', () => {
|
||||
.mockImplementation(
|
||||
async (
|
||||
actualConversation: ConversationFull,
|
||||
event: WebEventWrapper,
|
||||
event: WebEventWrapper<typeof WEB_CHANNEL_NAME>,
|
||||
) => {
|
||||
expect(actualConversation).toEqualPayload({
|
||||
next: [],
|
||||
|
@ -1 +0,0 @@
|
||||
Subproject commit cf7004ef6adac1b5e033d06987f493a9b00e01d2
|
@ -20,7 +20,6 @@ import { v4 as uuidv4 } from 'uuid';
|
||||
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
||||
import { AttachmentService } from '@/attachment/services/attachment.service';
|
||||
import { ChannelService } from '@/channel/channel.service';
|
||||
import EventWrapper from '@/channel/lib/EventWrapper';
|
||||
import ChannelHandler from '@/channel/lib/Handler';
|
||||
import { ChannelName } from '@/channel/types';
|
||||
import { MessageCreateDto } from '@/chat/dto/message.dto';
|
||||
@ -782,11 +781,7 @@ export default abstract class BaseWebChannelHandler<
|
||||
data.data = upload;
|
||||
}
|
||||
const channelAttrs = this.getChannelAttributes(req);
|
||||
const event: WebEventWrapper = new WebEventWrapper(
|
||||
this,
|
||||
data,
|
||||
channelAttrs,
|
||||
);
|
||||
const event = new WebEventWrapper<N>(this, data, channelAttrs);
|
||||
if (event.getEventType() === 'message') {
|
||||
// Handler sync message sent by chabbot
|
||||
if (data.sync && data.author === 'chatbot') {
|
||||
@ -1206,7 +1201,7 @@ export default abstract class BaseWebChannelHandler<
|
||||
* @returns The web's response, otherwise an error
|
||||
*/
|
||||
async sendMessage(
|
||||
event: EventWrapper<any, any>,
|
||||
event: WebEventWrapper<N>,
|
||||
envelope: StdOutgoingEnvelope,
|
||||
options: BlockOptions,
|
||||
_context?: any,
|
||||
@ -1280,7 +1275,7 @@ export default abstract class BaseWebChannelHandler<
|
||||
*
|
||||
* @returns The web's response, otherwise an error
|
||||
*/
|
||||
async getUserData(event: WebEventWrapper): Promise<SubscriberCreateDto> {
|
||||
async getUserData(event: WebEventWrapper<N>): Promise<SubscriberCreateDto> {
|
||||
const sender = event.getSender();
|
||||
const {
|
||||
id: _id,
|
||||
|
@ -68,10 +68,12 @@ type WebEventAdapter =
|
||||
raw: Web.IncomingMessage<Web.IncomingAttachmentMessage>;
|
||||
};
|
||||
|
||||
export default class WebEventWrapper<
|
||||
T extends
|
||||
BaseWebChannelHandler<ChannelName> = BaseWebChannelHandler<ChannelName>,
|
||||
> extends EventWrapper<WebEventAdapter, Web.Event> {
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
export default class WebEventWrapper<N extends ChannelName> extends EventWrapper<
|
||||
WebEventAdapter,
|
||||
Web.Event,
|
||||
N
|
||||
> {
|
||||
/**
|
||||
* Constructor : channel's event wrapper
|
||||
*
|
||||
@ -80,7 +82,7 @@ export default class WebEventWrapper<
|
||||
* @param channelAttrs - Channel's specific extra attributes {isSocket, ipAddress}
|
||||
*/
|
||||
constructor(
|
||||
handler: T,
|
||||
handler: BaseWebChannelHandler<N>,
|
||||
event: Web.Event,
|
||||
channelAttrs: SubscriberChannelDict[typeof WEB_CHANNEL_NAME],
|
||||
) {
|
||||
|
Loading…
Reference in New Issue
Block a user