Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/channel data typing #442

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions api/src/channel/lib/EventWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ 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 {}

// eslint-disable-next-line prettier/prettier
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;

Expand All @@ -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;
Expand Down Expand Up @@ -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>;
}

/**
Expand Down Expand Up @@ -267,7 +267,8 @@ type GenericEventAdapter = {

export class GenericEventWrapper extends EventWrapper<
GenericEventAdapter,
GenericEvent
GenericEvent,
ChannelName
> {
/**
* Constructor : channel's event wrapper
Expand Down
6 changes: 2 additions & 4 deletions api/src/channel/lib/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -189,7 +187,7 @@ export default abstract class ChannelHandler<

*/
abstract getUserData(
event: EventWrapper<any, any>,
event: EventWrapper<any, any, N>,
): Promise<SubscriberCreateDto>;

/**
Expand Down
4 changes: 2 additions & 2 deletions api/src/chat/schemas/types/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
5 changes: 3 additions & 2 deletions api/src/chat/services/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -201,7 +202,7 @@ describe('BlockService', () => {
.spyOn(botService, 'findBlockAndSendReply')
.mockImplementation(
(
actualEvent: WebEventWrapper,
actualEvent: WebEventWrapper<typeof WEB_CHANNEL_NAME>,
actualConversation: Conversation,
actualBlock: BlockFull,
isFallback: boolean,
Expand Down Expand Up @@ -267,7 +268,7 @@ describe('BlockService', () => {
.mockImplementation(
async (
actualConversation: ConversationFull,
event: WebEventWrapper,
event: WebEventWrapper<typeof WEB_CHANNEL_NAME>,
) => {
expect(actualConversation).toEqualPayload({
next: [],
Expand Down
1 change: 0 additions & 1 deletion api/src/extensions/channels/hexabot-channel-messenger
Submodule hexabot-channel-messenger deleted from cf7004
11 changes: 3 additions & 8 deletions api/src/extensions/channels/web/base-web-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions api/src/extensions/channels/web/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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],
) {
Expand Down
Loading