mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: enforce typechecking + refactor
This commit is contained in:
parent
53c83a26a5
commit
560f4f3148
@ -11,6 +11,7 @@ import {
|
||||
AttachmentForeignKey,
|
||||
AttachmentPayload,
|
||||
} from '@/chat/schemas/types/attachment';
|
||||
import { SubscriberChannelData } from '@/chat/schemas/types/channel';
|
||||
import {
|
||||
IncomingMessageType,
|
||||
StdEventType,
|
||||
@ -19,10 +20,11 @@ import {
|
||||
import { Payload } from '@/chat/schemas/types/quick-reply';
|
||||
import { Nlp } from '@/helper/types';
|
||||
|
||||
import ChannelHandler from './Handler';
|
||||
import ChannelHandler, { ChannelNameOf } from './Handler';
|
||||
|
||||
export interface ChannelEvent {}
|
||||
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
export default abstract class EventWrapper<
|
||||
A,
|
||||
E,
|
||||
@ -32,6 +34,8 @@ export default abstract class EventWrapper<
|
||||
|
||||
_handler: C;
|
||||
|
||||
channelAttrs: SubscriberChannelDict[ChannelNameOf<C>];
|
||||
|
||||
_profile!: Subscriber;
|
||||
|
||||
_nlp!: Nlp.ParseEntities;
|
||||
@ -42,14 +46,18 @@ export default abstract class EventWrapper<
|
||||
*
|
||||
* Any method declared in this class should be extended and overridden in any given channel's
|
||||
* event wrapper if needed.
|
||||
* @param handler - The channel's handler
|
||||
* @param event - The message event received
|
||||
* @param channelData - Channel's specific data
|
||||
* @param handler - The channel's handler
|
||||
* @param event - The message event received
|
||||
* @param channelAttrs - Channel's specific data
|
||||
*/
|
||||
constructor(handler: C, event: E, channelData: any = {}) {
|
||||
constructor(
|
||||
handler: C,
|
||||
event: E,
|
||||
channelAttrs: SubscriberChannelDict[ChannelNameOf<C>] = {},
|
||||
) {
|
||||
this._handler = handler;
|
||||
this._init(event);
|
||||
this.set('channelData', channelData);
|
||||
this.channelAttrs = channelAttrs;
|
||||
}
|
||||
|
||||
toString() {
|
||||
@ -98,8 +106,11 @@ export default abstract class EventWrapper<
|
||||
*
|
||||
* @returns Returns any channel related data.
|
||||
*/
|
||||
getChannelData(): any {
|
||||
return this.get('channelData', {});
|
||||
getChannelData(): SubscriberChannelData<ChannelNameOf<C>> {
|
||||
return {
|
||||
name: this._handler.getName(),
|
||||
...this.channelAttrs,
|
||||
} as SubscriberChannelData<ChannelNameOf<C>>;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -285,15 +296,6 @@ export class GenericEventWrapper extends EventWrapper<
|
||||
this._adapter.raw = event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns channel related data
|
||||
*
|
||||
* @returns An object representing the channel specific data
|
||||
*/
|
||||
getChannelData(): any {
|
||||
return this.get('channelData', {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message id
|
||||
*
|
||||
|
@ -28,6 +28,8 @@ 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,
|
||||
@ -48,10 +50,14 @@ export default abstract class ChannelHandler<
|
||||
this.settings = require(path.join(this.getPath(), 'settings')).default;
|
||||
}
|
||||
|
||||
getName() {
|
||||
return this.name as N;
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
await super.onModuleInit();
|
||||
this.channelService.setChannel(
|
||||
this.getName() as ChannelName,
|
||||
this.getName(),
|
||||
this as unknown as ChannelHandler<N>,
|
||||
);
|
||||
this.setup();
|
||||
|
@ -19,7 +19,7 @@ import {
|
||||
import { ChannelName } from '@/channel/types';
|
||||
import { IsObjectId } from '@/utils/validation-rules/is-object-id';
|
||||
|
||||
import { SubscriberChannel } from '../schemas/types/channel';
|
||||
import { SubscriberChannelData } from '../schemas/types/channel';
|
||||
import { IsChannelData } from '../validation-rules/is-channel-data';
|
||||
|
||||
export class SubscriberCreateDto {
|
||||
@ -110,7 +110,7 @@ export class SubscriberCreateDto {
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsChannelData()
|
||||
channel: SubscriberChannel<ChannelName>;
|
||||
channel: SubscriberChannelData<ChannelName>;
|
||||
}
|
||||
|
||||
export class SubscriberUpdateDto extends PartialType(SubscriberCreateDto) {}
|
||||
|
3
api/src/chat/index.d.ts
vendored
3
api/src/chat/index.d.ts
vendored
@ -9,5 +9,6 @@
|
||||
import { ChannelName } from '@/channel/types';
|
||||
|
||||
declare global {
|
||||
interface SubscriberChannelDict extends Record<ChannelName | string, any> {}
|
||||
interface SubscriberChannelDict
|
||||
extends Record<ChannelName | string, Record<string, any>> {}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ import {
|
||||
} from '@/utils/types/filter.types';
|
||||
|
||||
import { Label } from './label.schema';
|
||||
import { SubscriberChannel } from './types/channel';
|
||||
import { SubscriberChannelData } from './types/channel';
|
||||
import { SubscriberContext } from './types/subscriberContext';
|
||||
|
||||
@Schema({ timestamps: true })
|
||||
@ -103,7 +103,7 @@ export class SubscriberStub extends BaseSchema {
|
||||
@Prop({
|
||||
type: Object,
|
||||
})
|
||||
channel: SubscriberChannel;
|
||||
channel: SubscriberChannelData;
|
||||
|
||||
@Prop({
|
||||
type: MongooseSchema.Types.ObjectId,
|
||||
@ -122,7 +122,7 @@ export class SubscriberStub extends BaseSchema {
|
||||
C extends ChannelName,
|
||||
S extends SubscriberStub = Subscriber,
|
||||
>(subscriber: S) {
|
||||
return subscriber.channel as unknown as SubscriberChannel<C>;
|
||||
return subscriber.channel as SubscriberChannelData<C>;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,13 +8,14 @@
|
||||
|
||||
import { ChannelName } from '@/channel/types';
|
||||
|
||||
export type SubscriberChannel<
|
||||
export type SubscriberChannelData<
|
||||
C extends ChannelName = null,
|
||||
K extends keyof SubscriberChannelDict[C] = keyof SubscriberChannelDict[C],
|
||||
> = C extends null
|
||||
? { name: ChannelName }
|
||||
: {
|
||||
[P in keyof SubscriberChannelDict[C]]: SubscriberChannelDict[C][K];
|
||||
} & {
|
||||
name: C;
|
||||
} & {
|
||||
// Channel's specific attributes
|
||||
[P in keyof SubscriberChannelDict[C]]: SubscriberChannelDict[C][K];
|
||||
};
|
||||
|
@ -236,7 +236,11 @@ describe('BlockService', () => {
|
||||
text: 'Hello',
|
||||
},
|
||||
},
|
||||
{},
|
||||
{
|
||||
isSocket: true,
|
||||
ipAddress: '1.1.1.1',
|
||||
agent: 'Chromium',
|
||||
},
|
||||
);
|
||||
const webEventGetStarted = new WebEventWrapper(
|
||||
handlerMock,
|
||||
@ -247,7 +251,11 @@ describe('BlockService', () => {
|
||||
payload: 'GET_STARTED',
|
||||
},
|
||||
},
|
||||
{},
|
||||
{
|
||||
isSocket: true,
|
||||
ipAddress: '1.1.1.1',
|
||||
agent: 'Chromium',
|
||||
},
|
||||
);
|
||||
|
||||
it('should return undefined when no blocks are provided', async () => {
|
||||
|
@ -186,6 +186,7 @@ describe('BlockService', () => {
|
||||
const event = new WebEventWrapper(handler, webEventText, {
|
||||
isSocket: false,
|
||||
ipAddress: '1.1.1.1',
|
||||
agent: 'Chromium',
|
||||
});
|
||||
|
||||
const [block] = await blockService.findAndPopulate({ patterns: ['Hi'] });
|
||||
@ -254,6 +255,7 @@ describe('BlockService', () => {
|
||||
const event = new WebEventWrapper(handler, webEventText, {
|
||||
isSocket: false,
|
||||
ipAddress: '1.1.1.1',
|
||||
agent: 'Chromium',
|
||||
});
|
||||
const webSubscriber = await subscriberService.findOne({
|
||||
foreign_id: 'foreign-id-web-1',
|
||||
@ -307,6 +309,7 @@ describe('BlockService', () => {
|
||||
const event = new WebEventWrapper(handler, webEventText, {
|
||||
isSocket: false,
|
||||
ipAddress: '1.1.1.1',
|
||||
agent: 'Chromium',
|
||||
});
|
||||
const webSubscriber = await subscriberService.findOne({
|
||||
foreign_id: 'foreign-id-web-2',
|
||||
|
@ -244,10 +244,7 @@ export class ChatService {
|
||||
if (!subscriber) {
|
||||
const subscriberData = await handler.getUserData(event);
|
||||
this.eventEmitter.emit('hook:stats:entry', 'new_users', 'New users');
|
||||
subscriberData.channel = {
|
||||
...event.getChannelData(),
|
||||
name: handler.getName(),
|
||||
};
|
||||
subscriberData.channel = event.getChannelData();
|
||||
subscriber = await this.subscriberService.create(subscriberData);
|
||||
} else {
|
||||
// Already existing user profile
|
||||
|
@ -9,7 +9,6 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
import EventWrapper from '@/channel/lib/EventWrapper';
|
||||
import { ChannelName } from '@/channel/types';
|
||||
import { LoggerService } from '@/logger/logger.service';
|
||||
import { BaseService } from '@/utils/generics/base-service';
|
||||
|
||||
@ -70,7 +69,7 @@ export class ConversationService extends BaseService<
|
||||
const msgType = event.getMessageType();
|
||||
const profile = event.getSender();
|
||||
// Capture channel specific context data
|
||||
convo.context.channel = event.getHandler().getName() as ChannelName;
|
||||
convo.context.channel = event.getHandler().getName();
|
||||
convo.context.text = event.getText();
|
||||
convo.context.payload = event.getPayload();
|
||||
convo.context.nlp = event.getNLP();
|
||||
|
@ -14,7 +14,6 @@ declare global {
|
||||
interface Settings extends SettingTree<typeof CONSOLE_CHANNEL_SETTINGS> {}
|
||||
interface SubscriberChannelDict {
|
||||
[CONSOLE_CHANNEL_NAME]: {
|
||||
name: typeof CONSOLE_CHANNEL_NAME;
|
||||
isSocket: boolean;
|
||||
ipAddress: string;
|
||||
agent: string;
|
||||
|
@ -0,0 +1 @@
|
||||
Subproject commit cf7004ef6adac1b5e033d06987f493a9b00e01d2
|
@ -37,6 +37,7 @@ import { SocketEventDispatcherService } from '@/websocket/services/socket-event-
|
||||
import { WebsocketGateway } from '@/websocket/websocket.gateway';
|
||||
|
||||
import WebChannelHandler from '../index.channel';
|
||||
import { WEB_CHANNEL_NAME } from '../settings';
|
||||
import WebEventWrapper from '../wrapper';
|
||||
|
||||
import { webEvents } from './events.mock';
|
||||
@ -119,7 +120,10 @@ describe(`Web event wrapper`, () => {
|
||||
e,
|
||||
expected.channelData,
|
||||
);
|
||||
expect(event.getChannelData()).toEqual(expected.channelData);
|
||||
expect(event.getChannelData()).toEqual({
|
||||
...expected.channelData,
|
||||
name: WEB_CHANNEL_NAME,
|
||||
});
|
||||
expect(event.getId()).toEqual(expected.id);
|
||||
expect(event.getEventType()).toEqual(expected.eventType);
|
||||
expect(event.getMessageType()).toEqual(expected.messageType);
|
||||
|
@ -433,7 +433,6 @@ export default abstract class BaseWebChannelHandler<
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
const channelData = this.getChannelData(req);
|
||||
const newProfile: SubscriberCreateDto = {
|
||||
foreign_id: this.generateId(),
|
||||
first_name: data.first_name ? data.first_name.toString() : 'Anon.',
|
||||
@ -443,8 +442,8 @@ export default abstract class BaseWebChannelHandler<
|
||||
lastvisit: new Date(),
|
||||
retainedFrom: new Date(),
|
||||
channel: {
|
||||
...channelData,
|
||||
name: this.getName() as ChannelName,
|
||||
name: this.getName(),
|
||||
...this.getChannelAttributes(req),
|
||||
},
|
||||
language: '',
|
||||
locale: '',
|
||||
@ -736,13 +735,15 @@ export default abstract class BaseWebChannelHandler<
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle channel event (probably a message)
|
||||
* Return subscriber channel specific attributes
|
||||
*
|
||||
* @param req
|
||||
*
|
||||
* @returns The channel's data
|
||||
* @returns The subscriber channel's attributes
|
||||
*/
|
||||
protected getChannelData(req: Request | SocketRequest): Web.ChannelData {
|
||||
getChannelAttributes(
|
||||
req: Request | SocketRequest,
|
||||
): SubscriberChannelDict[typeof WEB_CHANNEL_NAME] {
|
||||
return {
|
||||
isSocket: 'isSocket' in req && !!req.isSocket,
|
||||
ipAddress: this.getIpAddress(req),
|
||||
@ -780,11 +781,11 @@ export default abstract class BaseWebChannelHandler<
|
||||
if (upload) {
|
||||
data.data = upload;
|
||||
}
|
||||
const channelData = this.getChannelData(req);
|
||||
const channelAttrs = this.getChannelAttributes(req);
|
||||
const event: WebEventWrapper = new WebEventWrapper(
|
||||
this,
|
||||
data,
|
||||
channelData,
|
||||
channelAttrs,
|
||||
);
|
||||
if (event.getEventType() === 'message') {
|
||||
// Handler sync message sent by chabbot
|
||||
|
1
api/src/extensions/channels/web/index.d.ts
vendored
1
api/src/extensions/channels/web/index.d.ts
vendored
@ -16,7 +16,6 @@ declare global {
|
||||
|
||||
interface SubscriberChannelDict {
|
||||
[WEB_CHANNEL_NAME]: {
|
||||
name: typeof WEB_CHANNEL_NAME;
|
||||
isSocket: boolean;
|
||||
ipAddress: string;
|
||||
agent: string;
|
||||
|
@ -30,12 +30,6 @@ export namespace Web {
|
||||
|
||||
export type Settings = Record<SettingLabel, any>;
|
||||
|
||||
export type ChannelData = {
|
||||
isSocket: boolean;
|
||||
ipAddress: string;
|
||||
agent: string;
|
||||
};
|
||||
|
||||
export type RequestSession = {
|
||||
web?: {
|
||||
profile: SubscriberFull;
|
||||
|
@ -21,6 +21,7 @@ import {
|
||||
import { Payload } from '@/chat/schemas/types/quick-reply';
|
||||
|
||||
import BaseWebChannelHandler from './base-web-channel';
|
||||
import { WEB_CHANNEL_NAME } from './settings';
|
||||
import { Web } from './types';
|
||||
|
||||
type WebEventAdapter =
|
||||
@ -76,10 +77,14 @@ export default class WebEventWrapper<
|
||||
*
|
||||
* @param handler - The channel's handler
|
||||
* @param event - The message event received
|
||||
* @param channelData - Channel's specific extra data {isSocket, ipAddress}
|
||||
* @param channelAttrs - Channel's specific extra attributes {isSocket, ipAddress}
|
||||
*/
|
||||
constructor(handler: T, event: Web.Event, channelData: any) {
|
||||
super(handler, event, channelData);
|
||||
constructor(
|
||||
handler: T,
|
||||
event: Web.Event,
|
||||
channelAttrs: SubscriberChannelDict[typeof WEB_CHANNEL_NAME],
|
||||
) {
|
||||
super(handler, event, channelAttrs);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -129,20 +134,6 @@ export default class WebEventWrapper<
|
||||
this._adapter.raw = event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns channel related data
|
||||
*
|
||||
* @returns Channel's data
|
||||
*/
|
||||
getChannelData(): any {
|
||||
return this.get('channelData', {
|
||||
isSocket: true,
|
||||
ipAddress: '0.0.0.0',
|
||||
agent:
|
||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message id
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user