Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

1.9 KB
Raw Blame
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsString, IsNotEmpty, MaxLength, IsOptional, Matches } from 'class-validator';
import { IsMediaUrl } from '../../../common/media/media-url';
import { stripBase64DataUri } from '../../message/media-cap.util';
import { ToStrictString } from '../../../common/utils/strict-boolean';

export class SetProfileNameDto {
  @ApiProperty({ description: 'New display name (WhatsApp limit: 25 characters)', maxLength: 25 })
  @ToStrictString()
  @IsString()
  @IsNotEmpty()
  @MaxLength(25)
  name!: string;
}

export class SetProfileStatusDto {
  @ApiProperty({
    description: 'New about/status text (may be empty to clear it; WhatsApp limit: 139 characters)',
    maxLength: 139,
  })
  @ToStrictString()
  @IsString()
  @MaxLength(139)
  status!: string;
}

/**
 * Media arrives as JSON — a public URL (server-fetched, SSRF-guarded) or inline base64 — the same
 * acceptance pattern as the message module's media endpoints (SendMediaMessageDto).
 */
export class SetProfilePictureDto {
  @ApiPropertyOptional({
    description: 'Image URL (http/https)',
    example: 'https://example.com/avatar.jpg',
  })
  @IsOptional()
  // base64 wins when it holds data; a base64 that is only a data-URI prefix strips to nothing, and then
  // the url is what gets sent, so it is checked.
  @IsMediaUrl<SetProfilePictureDto>({ ignoreWhen: o => !!stripBase64DataUri(o.base64) })
  url?: string;

  @ApiPropertyOptional({
    description: 'Base64 encoded image data',
  })
  @IsOptional()
  @ToStrictString()
  @IsString()
  base64?: string;

  @ApiPropertyOptional({
    description: 'Image MIME type (required when using base64)',
    example: 'image/jpeg',
  })
  @IsOptional()
  @ToStrictString()
  @IsString()
  // A profile picture is an image by definition — fail non-image mimetypes fast (400) instead of
  // letting the engine reject (or worse, accept) an arbitrary payload.
  @Matches(/^image\//)
  mimetype?: string;
}