2024-08-07 11:18:41 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { MessageEntity } from '../entity/message';
|
|
|
|
|
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-07 11:18:41 +01:00
|
|
|
import { z } 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-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,
|
|
|
|
|
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-13 17:05:46 +01:00
|
|
|
message.sendAttemp++;
|
2024-08-07 11:18:41 +01:00
|
|
|
|
2024-08-13 17:05:46 +01:00
|
|
|
message.requestId = InstanceId +'@'+ uuidv4();
|
2024-08-13 10:52:35 +01:00
|
|
|
|
2024-08-13 17:05:46 +01:00
|
|
|
const createMessageLocally = await this.MessageRepositoryService.createMessageLocally(message)
|
|
|
|
|
|
|
|
|
|
if(createMessageLocally.isOk()) {
|
|
|
|
|
|
|
|
|
|
console.log('==========================',message);
|
|
|
|
|
if(message.hasAttachment) {
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sendToServer = await this.MessageRepositoryService.sendMessage(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if(sendToServer.isOk()) {
|
|
|
|
|
// for (const attachment of message.attachments) {
|
|
|
|
|
|
|
|
|
|
// const attachment = await this.AttachmentRepositoryService.findOne({
|
|
|
|
|
// $messageId: createMessageLocally.value.$id
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sendToServer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await this.MessageRepositoryService.sendMessage(message)
|
2024-08-07 11:18:41 +01:00
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
}
|