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-16 14:21:01 +01:00
|
|
|
import { MessageRepositoryService } from "src/app/module/chat/data/repository/message-respository.service";
|
|
|
|
|
import { AttachmentRepositoryService } from "src/app/module/chat/data/repository/attachment-repository.service";
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2024-08-13 10:52:35 +01:00
|
|
|
import { InstanceId } from '../chat-service.service';
|
2024-08-13 17:05:46 +01:00
|
|
|
import { createDataURL } from 'src/app/utils/ToBase64';
|
2024-08-15 14:29:11 +01:00
|
|
|
import { zodSafeValidation } from 'src/app/utils/zodValidation';
|
2024-08-16 12:24:26 +01:00
|
|
|
import { Logger } from 'src/app/services/logger/main/service';
|
|
|
|
|
import { MessageAttachmentSource } from '../../data/dto/message/messageOutputDTO';
|
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-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()) {
|
|
|
|
|
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) {
|
|
|
|
|
|
2024-08-16 12:24:26 +01:00
|
|
|
if(attachment.source != MessageAttachmentSource.Webtrix) {
|
|
|
|
|
|
|
|
|
|
this.AttachmentRepositoryService.create({
|
|
|
|
|
$messageId: createMessageLocally.value.$id,
|
|
|
|
|
file: createDataURL(attachment.file, attachment.mimeType)
|
|
|
|
|
}).then((e) => {
|
|
|
|
|
if(e.isErr()) {
|
|
|
|
|
Logger.error('failed to create attachment locally on send message', {
|
|
|
|
|
error: e.error,
|
|
|
|
|
data: createDataURL(attachment.file, attachment.mimeType).slice(0, 100) +'...'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
2024-08-15 14:29:11 +01:00
|
|
|
|
2024-08-16 12:24:26 +01:00
|
|
|
attachment.safeFile = createDataURL(attachment.file, attachment.mimeType)
|
|
|
|
|
}
|
2024-08-15 14:29:11 +01:00
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
|
|
if(!sendToServer.isOk()) {
|
2024-08-16 12:24:26 +01:00
|
|
|
Logger.error('failed to send message to the server', {
|
|
|
|
|
error: sendToServer.error
|
|
|
|
|
})
|
2024-08-15 14:29:11 +01:00
|
|
|
}
|
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) {
|
2024-08-16 12:24:26 +01:00
|
|
|
Logger.error('failed to send message doe to invalid attachment', {
|
|
|
|
|
zodErrorList: validation.error.errors,
|
|
|
|
|
data: message.attachments
|
|
|
|
|
})
|
2024-08-15 14:29:11 +01:00
|
|
|
}
|
2024-08-13 17:05:46 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 11:18:41 +01:00
|
|
|
}
|
|
|
|
|
}
|