diff --git a/api/src/channel/lib/EventWrapper.ts b/api/src/channel/lib/EventWrapper.ts index 722be1e7..64a8efc5 100644 --- a/api/src/channel/lib/EventWrapper.ts +++ b/api/src/channel/lib/EventWrapper.ts @@ -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, + S = SubscriberChannelDict[N], > { _adapter: A = {} as A; _handler: C; - channelAttrs: SubscriberChannelDict[ChannelNameOf]; + 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] = {}, - ) { + 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> { + getChannelData(): SubscriberChannelData { return { name: this._handler.getName(), ...this.channelAttrs, - } as SubscriberChannelData>; + } as SubscriberChannelData; } /** @@ -267,7 +267,8 @@ type GenericEventAdapter = { export class GenericEventWrapper extends EventWrapper< GenericEventAdapter, - GenericEvent + GenericEvent, + ChannelName > { /** * Constructor : channel's event wrapper diff --git a/api/src/channel/lib/Handler.ts b/api/src/channel/lib/Handler.ts index c23e58d9..fb277fdf 100644 --- a/api/src/channel/lib/Handler.ts +++ b/api/src/channel/lib/Handler.ts @@ -28,8 +28,6 @@ import { ChannelName, ChannelSetting } from '../types'; import EventWrapper from './EventWrapper'; -export type ChannelNameOf = C extends ChannelHandler ? 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, + event: EventWrapper, envelope: StdOutgoingEnvelope, options: any, context: any, @@ -189,7 +187,7 @@ export default abstract class ChannelHandler< */ abstract getUserData( - event: EventWrapper, + event: EventWrapper, ): Promise; /** diff --git a/api/src/chat/schemas/types/channel.ts b/api/src/chat/schemas/types/channel.ts index 4cab1fef..d53a3c60 100644 --- a/api/src/chat/schemas/types/channel.ts +++ b/api/src/chat/schemas/types/channel.ts @@ -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]; }; diff --git a/api/src/chat/services/bot.service.spec.ts b/api/src/chat/services/bot.service.spec.ts index b9615a0f..5dab34e6 100644 --- a/api/src/chat/services/bot.service.spec.ts +++ b/api/src/chat/services/bot.service.spec.ts @@ -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, actualConversation: Conversation, actualBlock: BlockFull, isFallback: boolean, @@ -267,7 +268,7 @@ describe('BlockService', () => { .mockImplementation( async ( actualConversation: ConversationFull, - event: WebEventWrapper, + event: WebEventWrapper, ) => { expect(actualConversation).toEqualPayload({ next: [], diff --git a/api/src/extensions/channels/hexabot-channel-messenger b/api/src/extensions/channels/hexabot-channel-messenger deleted file mode 160000 index cf7004ef..00000000 --- a/api/src/extensions/channels/hexabot-channel-messenger +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cf7004ef6adac1b5e033d06987f493a9b00e01d2 diff --git a/api/src/extensions/channels/web/base-web-channel.ts b/api/src/extensions/channels/web/base-web-channel.ts index eb428c20..b63a34f6 100644 --- a/api/src/extensions/channels/web/base-web-channel.ts +++ b/api/src/extensions/channels/web/base-web-channel.ts @@ -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(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, + event: WebEventWrapper, 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 { + async getUserData(event: WebEventWrapper): Promise { const sender = event.getSender(); const { id: _id, diff --git a/api/src/extensions/channels/web/wrapper.ts b/api/src/extensions/channels/web/wrapper.ts index b1650be7..b8c7cc7a 100644 --- a/api/src/extensions/channels/web/wrapper.ts +++ b/api/src/extensions/channels/web/wrapper.ts @@ -68,10 +68,12 @@ type WebEventAdapter = raw: Web.IncomingMessage; }; -export default class WebEventWrapper< - T extends - BaseWebChannelHandler = BaseWebChannelHandler, -> extends EventWrapper { +// eslint-disable-next-line prettier/prettier +export default class WebEventWrapper 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, event: Web.Event, channelAttrs: SubscriberChannelDict[typeof WEB_CHANNEL_NAME], ) {