diff --git a/src/app/module/chat/data/dto/message/messageInputDtO.ts b/src/app/module/chat/data/dto/message/messageInputDtO.ts index f52ac9377..be05c067e 100644 --- a/src/app/module/chat/data/dto/message/messageInputDtO.ts +++ b/src/app/module/chat/data/dto/message/messageInputDtO.ts @@ -1,5 +1,6 @@ import { z } from "zod"; import { MessageAttachmentFileType, MessageAttachmentSource } from "./messageOutputDTO"; +import { base64Schema } from "src/app/utils/zod"; export const MessageInputDTOSchema = z.object({ roomId: z.string().uuid(), @@ -13,10 +14,10 @@ export const MessageInputDTOSchema = z.object({ attachment: z.object({ fileType: z.nativeEnum(MessageAttachmentFileType), source: z.nativeEnum(MessageAttachmentSource), - file: z.string(), - fileName: z.string(), - applicationId: z.string(), - docId: z.string(), + file: base64Schema.optional(), + fileName: z.string().optional(), + applicationId: z.string().optional(), + docId: z.string().optional(), mimeType: z.string().optional() }).optional() }); diff --git a/src/app/module/chat/data/dto/message/messageOutputDTO.ts b/src/app/module/chat/data/dto/message/messageOutputDTO.ts index 5cf63a55a..74b8dfa1e 100644 --- a/src/app/module/chat/data/dto/message/messageOutputDTO.ts +++ b/src/app/module/chat/data/dto/message/messageOutputDTO.ts @@ -42,7 +42,7 @@ export const MessageOutPutDataDTOSchema = z.object({ fileName: z.string().optional(), applicationId: z.string().optional(), docId: z.string().optional(), - id: z.string() + id: z.string().optional() })) }); diff --git a/src/app/module/chat/data/repository/room-repository.service.ts b/src/app/module/chat/data/repository/room-repository.service.ts index f04fc1e7e..5826fff2e 100644 --- a/src/app/module/chat/data/repository/room-repository.service.ts +++ b/src/app/module/chat/data/repository/room-repository.service.ts @@ -227,11 +227,11 @@ export class RoomRepositoryService { @captureAndReraiseAsync('RoomRepositoryService/addMemberToRoom') async addMemberToRoom(data: AddMemberToRoomInputDTO) { - //return this.roomLiveSignalRDataSourceService.addMemberToRoom(data) + return this.roomLiveSignalRDataSourceService.addMemberToRoom(data) - const result = await this.roomRemoteDataSourceService.addMemberToRoom(data) + // const result = await this.roomRemoteDataSourceService.addMemberToRoom(data) - return result + // return result } async updateMemberStatus(data: MemberListUPdateStatusInputDTO) { diff --git a/src/app/module/chat/domain/entity/message.ts b/src/app/module/chat/domain/entity/message.ts index 5db0730c6..8e6107c59 100644 --- a/src/app/module/chat/domain/entity/message.ts +++ b/src/app/module/chat/domain/entity/message.ts @@ -26,8 +26,10 @@ export const MessageEntitySchema = z.object({ file: base64Schema.optional(), fileName: z.string().optional(), applicationId: z.string().optional(), - docId: z.string().optional() - })), + docId: z.string().optional(), + id: z.string().optional(), + mimeType: z.string().optional() + })).optional() }) type Message = z.infer; diff --git a/src/app/module/chat/domain/mapper/messageMapper.ts b/src/app/module/chat/domain/mapper/messageMapper.ts index 5cc2cf9e6..ef1713e3a 100644 --- a/src/app/module/chat/domain/mapper/messageMapper.ts +++ b/src/app/module/chat/domain/mapper/messageMapper.ts @@ -9,7 +9,7 @@ export class MessageMapper { } static fromDomain(entity:MessageEntity, requestId): MessageInputDTO { - return{ + return { canEdit: entity.canEdit, message: entity.message, messageType: entity.messageType, @@ -18,7 +18,16 @@ export class MessageMapper { roomId: entity.roomId, senderId: entity.sender.wxUserId, requestId: requestId, - attachment: entity.attachments[0] + attachment: entity.attachments.map((e)=>({ + fileType:e.fileType, + source: e.source, + file: e.file, + fileName: e.fileName, + applicationId: e.applicationId, + docId: e.docId, + mimeType: e.mimeType + }))[0] + } } } diff --git a/src/app/module/chat/domain/use-case/message-create-use-case.service.ts b/src/app/module/chat/domain/use-case/message-create-use-case.service.ts index 23bf7d370..036503a44 100644 --- a/src/app/module/chat/domain/use-case/message-create-use-case.service.ts +++ b/src/app/module/chat/domain/use-case/message-create-use-case.service.ts @@ -47,9 +47,13 @@ export class MessageCreateUseCaseService { for (const attachment of message.attachments) { - const createAttachmentLocally = this.AttachmentRepositoryService.create({ + this.AttachmentRepositoryService.create({ $messageId: createMessageLocally.value.$id, file: createDataURL(attachment.file, attachment.mimeType) + }).then((e) => { + if(e.isErr()) { + console.log('e', e.error, createDataURL(attachment.file, attachment.mimeType)) + } }) attachment.safeFile = createDataURL(attachment.file, attachment.mimeType) diff --git a/src/app/module/chat/infra/database/dexie/schema/message.ts b/src/app/module/chat/infra/database/dexie/schema/message.ts index 976cff357..05bb8c7ed 100644 --- a/src/app/module/chat/infra/database/dexie/schema/message.ts +++ b/src/app/module/chat/infra/database/dexie/schema/message.ts @@ -31,7 +31,9 @@ export const MessageTableSchema = z.object({ source: z.nativeEnum(MessageAttachmentSource), fileName: z.string().optional(), applicationId: z.string().optional(), - docId: z.string().optional() + docId: z.string().optional(), + id: z.string().optional(), + mimeType: z.string().optional() })).optional() }) diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index aed02c601..399c017eb 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -71,10 +71,7 @@
- +
diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index 65439d8b8..8a1655b75 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -161,7 +161,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy private CameraService: CameraService, private FilePickerWebService: FilePickerWebService, private FilePickerService: FilePickerService, - private SpeakerService: SpeakerService + private SpeakerService: SpeakerService, + private sanitizer: DomSanitizer ) { // update this.checkAudioPermission() @@ -220,9 +221,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy id: message.attachments[0].id }) - if(result.isOk()) { - message.attachments[0].safeFile = result.value + if(result.isOk() && message.attachments[0].fileType == MessageAttachmentFileType.Audio) { + console.log('safe parse', result.value) + message.attachments[0].safeFile = result.value; + } else if(result.isOk()){ + message.attachments[0].safeFile = result.value } } @@ -257,10 +261,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy id: message.attachments[0].id }) - if(result.isOk()) { + if(result.isOk() && message.attachments[0].fileType == MessageAttachmentFileType.Audio) { + console.log('safe parse', result.value) + message.attachments[0].safeFile = result.value; + } else if(result.isOk()){ message.attachments[0].safeFile = result.value - } } @@ -504,16 +510,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy }, 1000) } - async getFile(fileName?: any) { - const audioFile = await Filesystem.readFile({ - path: fileName, - directory: Directory.Data - }) - const base64sound = audioFile.data; - const base64Response = await fetch(`data:audio/ogg;base64,${base64sound}`); - this.audioRecordedSafe = base64Response.url; - } - async startRecording() { const start = await this.SpeakerService.startRecording() @@ -584,8 +580,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy fileName: "audio", source: MessageAttachmentSource.Device, fileType: MessageAttachmentFileType.Audio, - mimeType: this.audioMimeType - + mimeType: this.audioMimeType, // 'audio/webm', + safeFile: this.sanitiser.bypassSecurityTrustResourceUrl(this.audioRecordedDataUrl) }] this.chatServiceService.sendMessage(message) @@ -929,7 +925,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } message.attachments = [{ - file: file.value.base64String.split(',')[1], + file: file.value.base64String, fileName: "foto", source: MessageAttachmentSource.Device, fileType: MessageAttachmentFileType.Image, diff --git a/src/app/utils/zod.ts b/src/app/utils/zod.ts index 68f8d0170..82ba25dee 100644 --- a/src/app/utils/zod.ts +++ b/src/app/utils/zod.ts @@ -1,11 +1,5 @@ import { z } from 'zod'; -/** - * Regular expression to validate a data URL. - * The pattern matches data URLs that start with `data:`, optionally followed by a media type, - * optionally followed by an encoding (e.g., `base64`), and ending with base64 encoded data. - */ -export const zodDataUrlRegex = /^data:(?:[\w+\/-]+)?(?:[\w+\/-]+)?;base64,[\s\S]*$/; /** * Zod schema for validating data URLs. @@ -19,7 +13,7 @@ export const zodDataUrlRegex = /^data:(?:[\w+\/-]+)?(?:[\w+\/-]+)?;base64,[\s\S] * console.error('Validation error:', result.error.errors); * } */ -export const zodDataUrlSchema = z.string().refine(value => zodDataUrlRegex.test(value), { +export const zodDataUrlSchema = z.string().refine(value => value.startsWith('data:'), { message: 'Invalid data URL', });