Files
doneit-web/src/app/module/chat/domain/use-case/message/message-create-use-case.service.ts
T

238 lines
8.4 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
2024-08-27 23:36:24 +01:00
import { MessageAttachmentSource, MessageEntity, MessageEntitySchema, } from '../../../../../core/chat/entity/message';
2024-08-27 09:14:59 +01:00
import { AttachmentLocalDataSource } from "src/app/module/chat/data/repository/attachment/attachment-local-repository.service";
2024-08-16 14:21:01 +01:00
import { z } from 'zod';
import { v4 as uuidv4 } from 'uuid';
2024-08-27 09:14:59 +01:00
import { InstanceId } from '../../chat-service.service';
import { createBlobFromBase64, 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';
2024-08-19 16:01:58 +01:00
import { err, Result } from 'neverthrow';
2024-08-27 09:14:59 +01:00
import { MessageMapper } from '../../mapper/messageMapper';
2024-08-26 14:47:03 +01:00
import { RoomType } from "src/app/core/chat/entity/group";
2024-08-20 16:34:47 +01:00
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
2024-08-27 09:14:59 +01:00
import { MemberListLocalRepository } from 'src/app/module/chat/data/repository/member/member-list-local-repository.service'
2024-08-20 16:34:47 +01:00
import { SessionStore } from 'src/app/store/session.service';
2024-08-27 10:15:00 +01:00
import { MessageTable } from 'src/app/infra/database/dexie/instance/chat/schema/message';
2024-08-27 23:36:24 +01:00
import { MessageAttachmentFileType, MessageOutPutDataDTO } from 'src/app/core/chat/repository/dto/messageOutputDTO';
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
import { IMessageSocketRepository } from 'src/app/core/chat/repository/message/message-socket-repository';
import { IMemberLocalRepository } from 'src/app/core/chat/repository/member/member-local-repository';
import { IAttachmentLocalRepository } from 'src/app/core/chat/repository/typing/typing-local-repository';
2024-09-01 14:27:32 +01:00
import { base64Schema } from 'src/app/utils/zod';
2024-08-21 20:14:48 +01:00
2024-09-01 14:27:32 +01:00
export const MessageInputDTOSchema = z.object({
roomId: z.string().uuid().optional(),
receiverId: z.number().optional(),
senderId: z.number(),
message: z.string().nullable().optional(),
messageType: z.number(),
canEdit: z.boolean(),
oneShot: z.boolean(),
requireUnlock: z.boolean(),
requestId: z.string(),
attachment: z.object({
fileType: z.nativeEnum(MessageAttachmentFileType),
source: z.nativeEnum(MessageAttachmentSource),
file: base64Schema.optional(),
fileName: z.string().optional(),
applicationId: z.number().optional(),
docId: z.number().optional(),
mimeType: z.string().optional()
}).optional()
});
export type MessageInputDTO = z.infer<typeof MessageInputDTOSchema>
2024-08-19 16:01:58 +01:00
2024-08-27 20:29:57 +01:00
export const MessageCreatePutDataDTOSchema = z.object({
id: z.string(),
roomId: z.string(),
sender: z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
userPhoto: z.string().optional()
}),
message: z.string().nullable().optional(),
messageType: z.number(),
sentAt: z.string(),
canEdit: z.boolean(),
oneShot: z.boolean(),
requireUnlock: z.boolean(),
requestId: z.string().optional().nullable(),
reactions: z.object({
id: z.string(),
reactedAt: z.string(),
reaction: z.string(),
sender: z.object({}),
}).array(),
info: z.array(z.object({
memberId: z.number(),
readAt: z.string().nullable(),
deliverAt: z.string().nullable()
})),
attachments: z.array(z.object({
fileType: z.nativeEnum(MessageAttachmentFileType),
source: z.nativeEnum(MessageAttachmentSource),
file: z.string().optional(),
fileName: z.string().optional(),
applicationId: z.number().optional(),
docId: z.string().optional(),
id: z.string().optional()
}))
});
export type MessageCreateOutPutDataDTO = z.infer<typeof MessageCreatePutDataDTOSchema>
@Injectable({
providedIn: 'root'
})
export class MessageCreateUseCaseService {
constructor(
private AttachmentLocalRepositoryService: IAttachmentLocalRepository,
private messageLocalDataSourceService: IMessageLocalRepository,
private messageSocketRepositoryService: IMessageSocketRepository,
private MemberListLocalRepository: IMemberLocalRepository
) { }
2024-08-21 11:37:47 +01:00
@XTracerAsync({name:'MessageCreateUseCaseService', module:'chat', bugPrint: true, waitNThrow: 5000})
2024-08-20 16:34:47 +01:00
async execute(message: MessageEntity, messageEnum: RoomType, tracing?: TracingType) {
2024-08-15 14:29:11 +01:00
const validation = zodSafeValidation<MessageEntity>(MessageEntitySchema, message)
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-27 09:14:59 +01:00
message.sending = true;
2024-08-13 17:05:46 +01:00
2024-08-27 09:14:59 +01:00
const createMessageLocally = await this.messageLocalDataSourceService.insert(message)
2024-08-14 15:29:16 +01:00
2024-08-15 14:29:11 +01:00
if(createMessageLocally.isOk()) {
2024-08-18 13:27:57 +01:00
message.$id = createMessageLocally.value
2024-08-15 14:29:11 +01:00
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) {
2024-08-18 15:40:43 +01:00
this.AttachmentLocalRepositoryService.insert({
2024-08-18 13:27:57 +01:00
$messageId: createMessageLocally.value,
base64: createDataURL(attachment.file, attachment.mimeType),
2024-08-17 22:05:57 +01:00
fileType: attachment.fileType,
source: attachment.source,
fileName: attachment.fileName,
applicationId: attachment.applicationId,
docId: attachment.docId,
mimeType: attachment.mimeType,
2024-08-16 12:24:26 +01:00
}).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-18 13:27:57 +01:00
//====================
message.sending = true
2024-08-19 16:01:58 +01:00
let sendMessageResult: Result<MessageOutPutDataDTO, any>
2024-08-19 16:45:29 +01:00
if(messageEnum == RoomType.Group) {
2024-08-20 16:34:47 +01:00
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendGroupMessage(DTO)
2024-08-19 16:01:58 +01:00
} else {
2024-08-20 16:34:47 +01:00
if(message.receiverId) {
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendDirectMessage(DTO)
} else {
const getRoomMembers = await this.MemberListLocalRepository.directMember({
roomId:message.roomId,
currentUserId: SessionStore.user.UserId
})
if(getRoomMembers.isOk()) {
message.receiverId = getRoomMembers.value.wxUserId
const DTO = MessageMapper.fromDomain(message, message.requestId)
2024-08-21 20:14:48 +01:00
sendMessageResult = await this.messageSocketRepositoryService.sendGroupMessage(DTO)
2024-08-20 16:34:47 +01:00
} else {
console.log('not found direct users', getRoomMembers.error)
}
}
2024-08-19 16:01:58 +01:00
}
2024-08-20 16:34:47 +01:00
2024-08-18 13:27:57 +01:00
// return this sendMessageResult
2024-08-15 14:29:11 +01:00
2024-08-18 13:27:57 +01:00
if(sendMessageResult.isOk()) {
2024-09-01 12:57:33 +01:00
console.log('sendMessageResult', sendMessageResult.value.id)
2024-08-18 13:27:57 +01:00
if(sendMessageResult.value.sender == undefined || sendMessageResult.value.sender == null) {
delete sendMessageResult.value.sender
}
let clone: MessageTable = {
...sendMessageResult.value,
id: sendMessageResult.value.id,
$id : message.$id
}
2024-09-01 12:57:33 +01:00
this.messageLocalDataSourceService.update(message.$id, {...clone, sending: false, roomId: message.roomId}).then((data)=> {
if(data.isOk()) {
} else {
tracing.hasError('failed to update send message')
console.log(sendMessageResult)
console.log(data.error)
}
})
2024-08-19 16:01:58 +01:00
return sendMessageResult
2024-08-18 13:27:57 +01:00
} else {
2024-08-16 12:24:26 +01:00
Logger.error('failed to send message to the server', {
2024-08-18 13:27:57 +01:00
error: sendMessageResult.error
2024-08-16 12:24:26 +01:00
})
2024-08-18 13:27:57 +01:00
await this.messageLocalDataSourceService.update(message.$id, {sending: false, $id: message.$id})
return err('no connection')
2024-08-15 14:29:11 +01:00
}
2024-08-20 16:34:47 +01:00
} else {
Logger.error('failed to insert locally', {
error: createMessageLocally.error
})
2024-08-14 15:29:16 +01:00
}
2024-08-15 14:29:11 +01:00
} 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-20 16:34:47 +01:00
} else {
Logger.error('failed to send message, validation failed', {
zodErrorList: validation.error.errors,
data: message
})
2024-08-15 14:29:11 +01:00
}
2024-08-13 17:05:46 +01:00
}
}
}