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

Suggestion: extended channelData for channels #339

Closed
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
17 changes: 12 additions & 5 deletions api/src/channel/lib/EventWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { Payload } from '@/chat/schemas/types/quick-reply';
import { Nlp } from '@/helper/types';

import { ExtendedSubscriber } from '../types';

import ChannelHandler from './Handler';

export interface ChannelEvent {}
Expand All @@ -27,12 +29,13 @@ export default abstract class EventWrapper<
A,
E,
C extends ChannelHandler = ChannelHandler,
D = Record<string, never>,
> {
_adapter: A = {} as A;
_adapter: A = { raw: {} } as A;

_handler: C;

_profile!: Subscriber;
_profile!: ExtendedSubscriber<C, D>;

_nlp!: Nlp.ParseEntities;

Expand All @@ -46,7 +49,11 @@ export default abstract class EventWrapper<
* @param event - The message event received
* @param channelData - Channel's specific data
*/
constructor(handler: C, event: E, channelData: any = {}) {
constructor(
handler: C,
event: E,
channelData: D | Record<string, never> = {},
) {
this._handler = handler;
this._init(event);
this.set('channelData', channelData);
Expand Down Expand Up @@ -114,7 +121,7 @@ export default abstract class EventWrapper<
* @param attr - Event attribute name
* @param value - The value to set for the specified attribute.
*/
set(attr: string, value: any) {
set<T>(attr: string, value: T) {
(this._adapter as any).raw[attr] = value;
}

Expand Down Expand Up @@ -171,7 +178,7 @@ export default abstract class EventWrapper<
*
* @param profile - Sender data
*/
setSender(profile: Subscriber) {
setSender(profile: ExtendedSubscriber<C, D>) {
this._profile = profile;
}

Expand Down
14 changes: 14 additions & 0 deletions api/src/channel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import { Subscriber } from '@/chat/schemas/subscriber.schema';
import { ChannelData } from '@/chat/schemas/types/channel';
import { SettingCreateDto } from '@/setting/dto/setting.dto';
import { HyphenToUnderscore } from '@/utils/types/extension';

import ChannelHandler from './lib/Handler';

export type ChannelName = `${string}-channel`;

export type ChannelSetting<N extends string = string> = Omit<
Expand All @@ -17,3 +21,13 @@ export type ChannelSetting<N extends string = string> = Omit<
> & {
group: HyphenToUnderscore<N>;
};

export type ExtendedChannelData<C extends ChannelHandler, D> = ChannelData & {
[key in C extends ChannelHandler<infer N> ? N : never]: D;
};

export type ExtendedSubscriber<C extends ChannelHandler, D> =
| Subscriber
| (Subscriber & {
channelData: ExtendedChannelData<C, D>;
});