Merge branch 'main' into refactor/category-dialog

This commit is contained in:
yassinedorbozgithub 2025-02-04 05:41:35 +01:00
commit 5c5db5b6af
7 changed files with 52 additions and 70 deletions

View File

@ -1,28 +1,36 @@
/* /*
* Copyright © 2024 Hexastack. All rights reserved. * Copyright © 2025 Hexastack. All rights reserved.
* *
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 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). * 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 { z } from 'zod';
export enum ButtonType { export enum ButtonType {
postback = 'postback', postback = 'postback',
web_url = 'web_url', web_url = 'web_url',
} }
export type PostBackButton = { const postBackButtonSchema = z.object({
type: ButtonType.postback; type: z.literal(ButtonType.postback),
title: string; title: z.string(),
payload: string; payload: z.string(),
}; });
export type WebUrlButton = { const webUrlButtonSchema = z.object({
type: ButtonType.web_url; type: z.literal(ButtonType.web_url),
title: string; title: z.string(),
url: string; url: z.string().url(),
messenger_extensions?: boolean; messenger_extensions: z.boolean().optional(),
webview_height_ratio?: 'compact' | 'tall' | 'full'; webview_height_ratio: z.enum(['compact', 'tall', 'full']).optional(),
}; });
export type Button = PostBackButton | WebUrlButton; export const buttonSchema = z.union([postBackButtonSchema, webUrlButtonSchema]);
export type PostBackButton = z.infer<typeof postBackButtonSchema>;
export type WebUrlButton = z.infer<typeof webUrlButtonSchema>;
export type Button = z.infer<typeof buttonSchema>;

View File

@ -13,7 +13,7 @@ import { z } from 'zod';
// entity is `String` for NLP entities // entity is `String` for NLP entities
export const captureVarSchema = z.object({ export const captureVarSchema = z.object({
entity: z.union([z.number().min(-2).max(-1), z.string()]), entity: z.union([z.number().min(-2).max(-1), z.string()]),
context_var: z.string(), context_var: z.string().regex(/^[a-z][a-z_0-9]*$/),
}); });
export type CaptureVar = z.infer<typeof captureVarSchema>; export type CaptureVar = z.infer<typeof captureVarSchema>;

View File

@ -1,11 +1,13 @@
/* /*
* Copyright © 2024 Hexastack. All rights reserved. * Copyright © 2025 Hexastack. All rights reserved.
* *
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 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). * 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 { z } from 'zod';
import { ChannelName } from '@/channel/types'; import { ChannelName } from '@/channel/types';
export type SubscriberChannelData<C extends ChannelName = 'unknown-channel'> = export type SubscriberChannelData<C extends ChannelName = 'unknown-channel'> =
@ -17,3 +19,11 @@ export type SubscriberChannelData<C extends ChannelName = 'unknown-channel'> =
// Channel's specific attributes // Channel's specific attributes
[P in keyof SubscriberChannelDict[C]]: SubscriberChannelDict[C][P]; [P in keyof SubscriberChannelDict[C]]: SubscriberChannelDict[C][P];
}; };
export const channelDataSchema = z
.object({
name: z.string().regex(/-channel$/) as z.ZodType<ChannelName>,
})
.passthrough();
export type Channel = z.infer<typeof channelDataSchema>;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright © 2024 Hexastack. All rights reserved. * Copyright © 2025 Hexastack. All rights reserved.
* *
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -13,17 +13,15 @@ import {
ValidatorConstraintInterface, ValidatorConstraintInterface,
} from 'class-validator'; } from 'class-validator';
export function isChannelData(channel: any) { import { Channel, channelDataSchema } from '../schemas/types/channel';
return (
typeof channel === 'object' && export function isChannelData(channel: Channel) {
channel.name && return channelDataSchema.safeParse(channel).success;
typeof channel.name === 'string'
);
} }
@ValidatorConstraint({ async: false }) @ValidatorConstraint({ async: false })
export class ChannelDataValidator implements ValidatorConstraintInterface { export class ChannelDataValidator implements ValidatorConstraintInterface {
validate(channel: any) { validate(channel: Channel) {
return isChannelData(channel); return isChannelData(channel);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright © 2024 Hexastack. All rights reserved. * Copyright © 2025 Hexastack. All rights reserved.
* *
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -13,18 +13,10 @@ import {
ValidatorConstraintInterface, ValidatorConstraintInterface,
} from 'class-validator'; } from 'class-validator';
import { Position } from '../schemas/types/position'; import { Position, positionSchema } from '../schemas/types/position';
export function isPosition(position: Position) { export function isPosition(position: Position) {
return ( return positionSchema.safeParse(position).success;
typeof position === 'object' &&
!isNaN(position.x) &&
!isNaN(position.y) &&
position.x !== Infinity &&
position.x !== -Infinity &&
position.y !== Infinity &&
position.y !== -Infinity
);
} }
@ValidatorConstraint({ async: false }) @ValidatorConstraint({ async: false })

View File

@ -1,5 +1,5 @@
/* /*
* Copyright © 2024 Hexastack. All rights reserved. * Copyright © 2025 Hexastack. All rights reserved.
* *
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -12,43 +12,17 @@ import {
ValidatorConstraint, ValidatorConstraint,
ValidatorConstraintInterface, ValidatorConstraintInterface,
} from 'class-validator'; } from 'class-validator';
import Joi from 'joi';
type Tentity = -1 | -2; import { CaptureVar, captureVarSchema } from '../schemas/types/capture-var';
export interface CaptureVar {
// entity=`-1` to match text message
// entity=`-2` for postback payload
// entity is `String` for NLP entities
entity: Tentity | string;
context_var: string;
}
const allowedEntityValues: Tentity[] = [-1, -2];
export function isValidVarCapture(vars: CaptureVar[]) { export function isValidVarCapture(vars: CaptureVar[]) {
const captureSchema = Joi.array().items( if (!Array.isArray(vars)) {
Joi.object().keys({ return false;
entity: Joi.alternatives().try(
// `-1` to match text message & `-2` for postback payload
Joi.number()
.valid(...allowedEntityValues)
.required(),
// String for NLP entities
Joi.string().required(),
),
context_var: Joi.string()
.regex(/^[a-z][a-z_0-9]*$/)
.required(),
}),
);
const captureCheck = captureSchema.validate(vars);
if (captureCheck.error) {
// eslint-disable-next-line
console.log('Capture vars validation failed!', captureCheck.error);
} }
return !captureCheck.error;
return vars.every(
(captureVar) => captureVarSchema.safeParse(captureVar).success,
);
} }
@ValidatorConstraint({ async: false }) @ValidatorConstraint({ async: false })

View File

@ -13,6 +13,7 @@ import {
import { BlockFull } from '@/chat/schemas/block.schema'; import { BlockFull } from '@/chat/schemas/block.schema';
import { FileType } from '@/chat/schemas/types/attachment'; import { FileType } from '@/chat/schemas/types/attachment';
import { ButtonType } from '@/chat/schemas/types/button'; import { ButtonType } from '@/chat/schemas/types/button';
import { CaptureVar } from '@/chat/schemas/types/capture-var';
import { import {
OutgoingMessageFormat, OutgoingMessageFormat,
PayloadType, PayloadType,
@ -20,7 +21,6 @@ import {
import { BlockOptions, ContentOptions } from '@/chat/schemas/types/options'; import { BlockOptions, ContentOptions } from '@/chat/schemas/types/options';
import { Pattern } from '@/chat/schemas/types/pattern'; import { Pattern } from '@/chat/schemas/types/pattern';
import { QuickReplyType } from '@/chat/schemas/types/quick-reply'; import { QuickReplyType } from '@/chat/schemas/types/quick-reply';
import { CaptureVar } from '@/chat/validation-rules/is-valid-capture';
import { modelInstance } from './misc'; import { modelInstance } from './misc';