2024-08-07 11:18:41 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2024-08-15 14:29:11 +01:00
|
|
|
import { MessageEntity, MessageEntitySchema, } from '../entity/message';
|
2024-08-07 11:18:41 +01:00
|
|
|
import { MessageRepositoryService } from "src/app/module/chat/data/repository/message-respository.service"
|
2024-08-13 17:05:46 +01:00
|
|
|
import { AttachmentRepositoryService } from "src/app/module/chat/data/repository/attachment-repository.service"
|
2024-08-15 14:29:11 +01:00
|
|
|
import { z, ZodError } from 'zod';
|
2024-08-13 10:52:35 +01:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
|
import { InstanceId } from '../chat-service.service';
|
2024-08-13 17:05:46 +01:00
|
|
|
import { createDataURL } from 'src/app/utils/ToBase64';
|
|
|
|
|
import { DomSanitizer } from '@angular/platform-browser';
|
2024-08-15 14:29:11 +01:00
|
|
|
import { zodSafeValidation } from 'src/app/utils/zodValidation';
|
2024-08-07 11:18:41 +01:00
|
|
|
|
|
|
|
|
const MessageInputUseCaseSchema = z.object({
|
|
|
|
|
memberId: z.number(),
|
|
|
|
|
roomId: z.string(),
|
|
|
|
|
message: z.string()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export type MessageInputUseCase = z.infer< typeof MessageInputUseCaseSchema>
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class MessageCreateUseCaseService {
|
|
|
|
|
|
|
|
|
|
constructor(
|
2024-08-13 17:05:46 +01:00
|
|
|
private MessageRepositoryService: MessageRepositoryService,
|
|
|
|
|
private AttachmentRepositoryService: AttachmentRepositoryService,
|
2024-08-15 14:29:11 +01:00
|
|
|
private sanitizer: DomSanitizer,
|
2024-08-07 11:18:41 +01:00
|
|
|
) { }
|
|
|
|
|
|
|
|
|
|
|
2024-08-13 17:05:46 +01:00
|
|
|
async execute(message: MessageEntity) {
|
2024-08-07 11:18:41 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
const validation = zodSafeValidation<MessageEntity>(MessageEntitySchema, message)
|
2024-08-07 11:18:41 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
if(validation.isOk()) {
|
|
|
|
|
message.sendAttemp++;
|
2024-08-13 10:52:35 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
message.requestId = InstanceId +'@'+ uuidv4();
|
2024-08-13 17:05:46 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
const createMessageLocally = await this.MessageRepositoryService.createMessageLocally(message)
|
2024-08-14 15:29:16 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
if(createMessageLocally.isOk()) {
|
2024-08-14 15:29:16 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
console.log('==========================',message);
|
|
|
|
|
if(message.hasAttachment) {
|
2024-08-14 15:29:16 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
for (const attachment of message.attachments) {
|
|
|
|
|
|
|
|
|
|
const createAttachmentLocally = this.AttachmentRepositoryService.create({
|
|
|
|
|
$messageId: createMessageLocally.value.$id,
|
|
|
|
|
file: createDataURL(attachment.file, attachment.mimeType)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
attachment.safeFile = createDataURL(attachment.file, attachment.mimeType)
|
|
|
|
|
}
|
2024-08-13 17:05:46 +01:00
|
|
|
|
|
|
|
|
}
|
2024-08-14 15:29:16 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
const sendToServer = await this.MessageRepositoryService.sendMessage(message)
|
|
|
|
|
|
2024-08-13 17:05:46 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
if(!sendToServer.isOk()) {
|
|
|
|
|
console.log('sendToServer error', sendToServer.error)
|
|
|
|
|
}
|
2024-08-13 17:05:46 +01:00
|
|
|
|
|
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
return sendToServer
|
2024-08-14 15:29:16 +01:00
|
|
|
}
|
2024-08-13 17:05:46 +01:00
|
|
|
|
2024-08-15 14:29:11 +01:00
|
|
|
const result = await this.MessageRepositoryService.sendMessage(message)
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
if(validation.error.formErrors.fieldErrors.attachments) {
|
|
|
|
|
console.log(message.attachments)
|
|
|
|
|
console.error('invalid attachment', validation.error.formErrors.fieldErrors.attachments)
|
|
|
|
|
}
|
2024-08-13 17:05:46 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 11:18:41 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|