mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 13:26:08 +00:00
fix chat send attchment
This commit is contained in:
@@ -19,16 +19,7 @@ export class MessageMapper {
|
||||
roomId: entity.roomId,
|
||||
senderId: entity.sender.wxUserId,
|
||||
requestId: entity.requestId || requestId,
|
||||
attachment: entity.attachments.map((e)=>({
|
||||
fileType:e.fileType,
|
||||
source: e.source,
|
||||
file: e.file,
|
||||
fileName: e.fileName,
|
||||
applicationId: e.applicationId || 0,
|
||||
docId: Number(e.docId) || 0,
|
||||
mimeType: e.mimeType,
|
||||
description: e.description
|
||||
})) || [],
|
||||
attachment: entity.attachments.map((e)=>(e.id)) || [],
|
||||
deviceId: getInstanceId()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { DataSourceReturn } from "src/app/services/Repositorys/type";
|
||||
import { IMessageGetAllByRoomIdOutPut } from "../../usecase/message/message-get-all-by-room-Id";
|
||||
import { MessageAttachmentInput } from "../../usecase/message/message-attachment-upload-use-case.service";
|
||||
import { Result } from "neverthrow";
|
||||
import { HttpErrorResponse } from "@angular/common/http";
|
||||
|
||||
export interface IGetMessagesFromRoomParams {
|
||||
roomId: string
|
||||
@@ -8,4 +11,5 @@ export interface IGetMessagesFromRoomParams {
|
||||
|
||||
export abstract class IMessageRemoteRepository {
|
||||
abstract getMessagesFromRoom(input: IGetMessagesFromRoomParams): DataSourceReturn<IMessageGetAllByRoomIdOutPut>
|
||||
abstract messageAttachmentUpload(input: MessageAttachmentInput): Promise<Result<String, HttpErrorResponse>>
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ import { SocketMessage } from "src/app/infra/socket/signalR/signalR";
|
||||
export abstract class IMessageSocketRepository {
|
||||
|
||||
abstract connect(): Promise<Result<HubConnection, false>>
|
||||
abstract sendGroupMessage(data: MessageInputDTO): Promise<Result<MessageCreateOutPutDataDTO, any>>
|
||||
abstract sendDirectMessage(data: MessageInputDTO): Promise<Result<MessageCreateOutPutDataDTO, any>>
|
||||
abstract sendGroupMessage(data: MessageInputDTO): Promise<Result<MessageOutPutDataDTO, any>>
|
||||
abstract sendDirectMessage(data: MessageInputDTO): Promise<Result<MessageOutPutDataDTO, any>>
|
||||
|
||||
// abstract sendDeliverAt(): Promise<Result<any, any>>
|
||||
abstract sendReadAt(data: MessageMarkAsReadInput): Promise<Result<any, any>>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { base64Schema } from 'src/app/utils/zod';
|
||||
import { z } from 'zod';
|
||||
import { MessageAttachmentFileType, MessageAttachmentSource } from '../../entity/message';
|
||||
|
||||
|
||||
|
||||
export const MessageAttachmentDTOSchema = z.object({
|
||||
fileType: z.nativeEnum(MessageAttachmentFileType),
|
||||
source: z.nativeEnum(MessageAttachmentSource),
|
||||
file: base64Schema.optional(),
|
||||
fileName: z.string().optional(),
|
||||
applicationId: z.number(),
|
||||
docId: z.number(),
|
||||
mimeType: z.string().nullable().optional(),
|
||||
description: z.string().optional(),
|
||||
senderId: z.number().optional(),
|
||||
});
|
||||
export type MessageAttachmentInput = z.infer<typeof MessageAttachmentDTOSchema>
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MessageAttachmentUploadUseCaseService {
|
||||
|
||||
constructor() { }
|
||||
|
||||
execute(input: MessageAttachmentInput) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { IMessage, MessageAttachmentSource, MessageEntity, MessageEntitySchema, } from '../../entity/message';
|
||||
import { z } from 'zod';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { createBlobFromBase64, createDataURL } from 'src/app/utils/ToBase64';
|
||||
import { zodSafeValidation } from 'src/app/utils/zodValidation';
|
||||
import { Logger } from 'src/app/services/logger/main/service';
|
||||
import { err, Result } from 'neverthrow';
|
||||
import { MessageMapper } from '../../mapper/messageMapper';
|
||||
import { RoomType } from "src/app/core/chat/entity/group";
|
||||
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
|
||||
import { MessageTable } from 'src/app/infra/database/dexie/instance/chat/schema/message';
|
||||
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';
|
||||
import { base64Schema } from 'src/app/utils/zod';
|
||||
|
||||
|
||||
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().nullable().optional(),
|
||||
description: z.string().optional()
|
||||
}).optional()
|
||||
});
|
||||
export type MessageInputDTO = z.infer<typeof MessageInputDTOSchema>
|
||||
|
||||
|
||||
|
||||
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.number().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
|
||||
) { }
|
||||
|
||||
|
||||
@XTracerAsync({name:'MessageCreateUseCaseService', module:'chat', bugPrint: true, waitNThrow: 5000})
|
||||
async execute(message: IMessage, messageEnum: RoomType, tracing?: TracingType) {
|
||||
|
||||
const validation = zodSafeValidation<IMessage>(MessageEntitySchema, message)
|
||||
|
||||
if(validation.isOk()) {
|
||||
message.sendAttemp++;
|
||||
|
||||
message.requestId = uuidv4();
|
||||
message.sending = true;
|
||||
|
||||
const createMessageLocally = this.messageLocalDataSourceService.insert(message)
|
||||
|
||||
createMessageLocally.then(async (value) => {
|
||||
if(value.isOk()) {
|
||||
|
||||
const localId = value.value
|
||||
message.$id = localId
|
||||
|
||||
if(message.hasAttachment) {
|
||||
|
||||
for (const attachment of message.attachments) {
|
||||
|
||||
if(attachment.source != MessageAttachmentSource.Webtrix) {
|
||||
|
||||
this.AttachmentLocalRepositoryService.insert({
|
||||
$messageId: localId,
|
||||
file: createBlobFromBase64(attachment.file, attachment.mimeType),
|
||||
fileType: attachment.fileType,
|
||||
source: attachment.source,
|
||||
fileName: attachment.fileName,
|
||||
applicationId: attachment.applicationId,
|
||||
docId: attachment.docId,
|
||||
mimeType: attachment.mimeType,
|
||||
base64: 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) +'...'
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
attachment.safeFile = createDataURL(attachment.file, attachment.mimeType)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
Logger.error('failed to insert locally', {
|
||||
error: value.error.message
|
||||
})
|
||||
}
|
||||
|
||||
}).catch((error) => {
|
||||
Logger.error('failed to insert catch', {
|
||||
//error: createMessageLocally.error.message
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
let sendMessageResult: Result<MessageOutPutDataDTO, any>
|
||||
|
||||
if(messageEnum == RoomType.Group) {
|
||||
const DTO = MessageMapper.fromDomain(message, message.requestId)
|
||||
message.sending = true
|
||||
sendMessageResult = await this.messageSocketRepositoryService.sendGroupMessage(DTO)
|
||||
} else {
|
||||
const DTO = MessageMapper.fromDomain(message, message.requestId)
|
||||
delete DTO.roomId
|
||||
message.sending = true
|
||||
sendMessageResult = await this.messageSocketRepositoryService.sendDirectMessage(DTO)
|
||||
}
|
||||
|
||||
// return this sendMessageResult
|
||||
|
||||
if(sendMessageResult.isOk()) {
|
||||
|
||||
message.id = sendMessageResult.value.id
|
||||
|
||||
console.log('sendMessageResult', sendMessageResult.value.id)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
createMessageLocally.then(() => {
|
||||
this.messageLocalDataSourceService.update(message.$id, {...clone, sending: false, roomId: clone.roomId}).then((data)=> {
|
||||
if(data.isOk()) {
|
||||
|
||||
} else {
|
||||
tracing.hasError('failed to update send message')
|
||||
console.log(sendMessageResult)
|
||||
console.log(data.error)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return sendMessageResult
|
||||
} else {
|
||||
Logger.error('failed to send message to the server', {
|
||||
error: sendMessageResult.error
|
||||
})
|
||||
await this.messageLocalDataSourceService.update(message.$id, {sending: false, $id: message.$id})
|
||||
return err('no connection')
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
if(validation.error.formErrors.fieldErrors.attachments) {
|
||||
Logger.error('failed to send message doe to invalid attachment', {
|
||||
zodErrorList: validation.error.errors,
|
||||
data: message.attachments
|
||||
})
|
||||
} else {
|
||||
Logger.error('failed to send message, validation failed', {
|
||||
zodErrorList: validation.error.errors,
|
||||
data: message
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,8 @@ import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/me
|
||||
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';
|
||||
import { base64Schema } from 'src/app/utils/zod';
|
||||
import { IMessageRemoteRepository } from '../../repository/message/message-remote-repository';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
|
||||
|
||||
export const MessageInputDTOSchema = z.object({
|
||||
@@ -28,16 +29,7 @@ export const MessageInputDTOSchema = z.object({
|
||||
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().nullable().optional(),
|
||||
description: z.string().optional()
|
||||
}).array(),
|
||||
attachment: z.string().array(),
|
||||
deviceId: z.string().optional()
|
||||
});
|
||||
export type MessageInputDTO = z.infer<typeof MessageInputDTOSchema>
|
||||
@@ -71,15 +63,7 @@ export const MessageCreatePutDataDTOSchema = z.object({
|
||||
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.number().optional(),
|
||||
id: z.string().optional()
|
||||
}))
|
||||
attachments: z.array(z.string())
|
||||
});
|
||||
|
||||
export type MessageCreateOutPutDataDTO = z.infer<typeof MessageCreatePutDataDTOSchema>
|
||||
@@ -93,7 +77,8 @@ export class MessageCreateUseCaseService {
|
||||
private AttachmentLocalRepositoryService: IAttachmentLocalRepository,
|
||||
private messageLocalDataSourceService: IMessageLocalRepository,
|
||||
private messageSocketRepositoryService: IMessageSocketRepository,
|
||||
private MemberListLocalRepository: IMemberLocalRepository
|
||||
private MemberListLocalRepository: IMemberLocalRepository,
|
||||
private MessageRemoteRepository: IMessageRemoteRepository
|
||||
) { }
|
||||
|
||||
|
||||
@@ -105,53 +90,73 @@ export class MessageCreateUseCaseService {
|
||||
if(validation.isOk()) {
|
||||
message.sendAttemp++;
|
||||
|
||||
const createMessageLocally = this.messageLocalDataSourceService.insert(message)
|
||||
const createMessageLocally = await this.messageLocalDataSourceService.insert(message)
|
||||
|
||||
createMessageLocally.then((value) => {
|
||||
if(value.isOk()) {
|
||||
if(createMessageLocally.isOk()) {
|
||||
|
||||
message.$id = value.value
|
||||
message.$id = createMessageLocally.value
|
||||
|
||||
if(message.hasAttachment) {
|
||||
if(message.hasAttachment) {
|
||||
|
||||
for (const attachment of message.attachments) {
|
||||
for (const attachment of message.attachments) {
|
||||
const isWebtrix = attachment.source === MessageAttachmentSource.Webtrix;
|
||||
|
||||
if(attachment.source != MessageAttachmentSource.Webtrix) {
|
||||
|
||||
this.AttachmentLocalRepositoryService.insert({
|
||||
$messageId: value.value,
|
||||
file: createBlobFromBase64(attachment.file, attachment.mimeType),
|
||||
fileType: attachment.fileType,
|
||||
source: attachment.source,
|
||||
fileName: attachment.fileName,
|
||||
applicationId: attachment.applicationId,
|
||||
docId: attachment.docId,
|
||||
mimeType: attachment.mimeType,
|
||||
base64: 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) +'...'
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
attachment.safeFile = createDataURL(attachment.file, attachment.mimeType)
|
||||
}
|
||||
await this.AttachmentLocalRepositoryService.insert({
|
||||
$messageId: createMessageLocally.value,
|
||||
file: isWebtrix ? null : createBlobFromBase64(attachment.file || '', attachment.mimeType),
|
||||
fileType: attachment.fileType,
|
||||
source: attachment.source,
|
||||
fileName: attachment.fileName,
|
||||
applicationId: attachment.applicationId,
|
||||
docId: attachment.docId,
|
||||
mimeType: attachment.mimeType,
|
||||
base64: isWebtrix ? null : createDataURL(attachment.file || '', attachment.mimeType),
|
||||
id: null,
|
||||
}).then((e) => {
|
||||
if(e.isErr()) {
|
||||
Logger.error('failed to create attachment locally on send message', {
|
||||
error: e.error,
|
||||
data: !isWebtrix ? createDataURL(attachment.file, attachment.mimeType).slice(0, 100) +'...' : undefined
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
if(!isWebtrix) {
|
||||
attachment.safeFile = createDataURL(attachment.file, attachment.mimeType)
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
Logger.error('failed to insert locally', {
|
||||
error: value.error.message
|
||||
})
|
||||
}
|
||||
|
||||
});
|
||||
var attachments = await this.AttachmentLocalRepositoryService.find({
|
||||
$messageId: createMessageLocally.value,
|
||||
});
|
||||
|
||||
if(attachments.isOk()) {
|
||||
var i = 0;
|
||||
for (const att of attachments.value) {
|
||||
|
||||
const isWebtrix = att.source === MessageAttachmentSource.Webtrix;
|
||||
var a = await this.MessageRemoteRepository.messageAttachmentUpload({
|
||||
fileType: att.fileType,
|
||||
source: att.source,
|
||||
file: isWebtrix ? null : att.base64.replace(/^data:[a-zA-Z]+\/[a-zA-Z]+;base64,/, ''),
|
||||
fileName: att.fileName,
|
||||
applicationId: att.applicationId || 0,
|
||||
docId: att.docId || 0,
|
||||
mimeType: att.mimeType,
|
||||
description: att.description || "something",
|
||||
senderId: SessionStore.user.UserId,
|
||||
});
|
||||
|
||||
if(a.isOk()) {
|
||||
att.id = a.value as string;
|
||||
message.attachments[i].id = a.value as string;
|
||||
await this.AttachmentLocalRepositoryService.update(att.$id, att)
|
||||
} else {
|
||||
return err('failed to upload attachment')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//====================
|
||||
message.sending = true
|
||||
@@ -172,7 +177,6 @@ export class MessageCreateUseCaseService {
|
||||
|
||||
tracing.setAttribute("duration", `Execution time: ${duration}ms`);
|
||||
|
||||
// return this sendMessageResult
|
||||
|
||||
if(sendMessageResult.isOk()) {
|
||||
|
||||
@@ -185,24 +189,23 @@ export class MessageCreateUseCaseService {
|
||||
delete sendMessageResult.value.sender
|
||||
}
|
||||
|
||||
createMessageLocally.then((value) => {
|
||||
console.log('sendMessageResult', (sendMessageResult as any).value)
|
||||
let clone: MessageTable = {
|
||||
...(sendMessageResult as any).value,
|
||||
id: (sendMessageResult as any).value.id,
|
||||
$id : message.$id
|
||||
}
|
||||
console.log('set update')
|
||||
this.messageLocalDataSourceService.update(message.$id, {...clone, sending: false, roomId: clone.roomId}).then((data)=> {
|
||||
if(data.isOk()) {
|
||||
|
||||
} else {
|
||||
tracing.hasError('failed to update send message')
|
||||
console.log(sendMessageResult)
|
||||
console.log(data.error)
|
||||
}
|
||||
})
|
||||
});
|
||||
console.log('sendMessageResult', (sendMessageResult as any).value)
|
||||
let clone: MessageTable = {
|
||||
...(sendMessageResult as any).value,
|
||||
id: (sendMessageResult as any).value.id,
|
||||
$id : message.$id
|
||||
}
|
||||
|
||||
this.messageLocalDataSourceService.update(message.$id, {...clone, sending: false, roomId: clone.roomId}).then((data)=> {
|
||||
if(data.isOk()) {
|
||||
|
||||
} else {
|
||||
tracing.hasError('failed to update send message')
|
||||
console.log(sendMessageResult)
|
||||
console.log(data.error)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return sendMessageResult
|
||||
@@ -213,21 +216,8 @@ export class MessageCreateUseCaseService {
|
||||
await this.messageLocalDataSourceService.update(message.$id, {sending: false, $id: message.$id})
|
||||
return err('no connection')
|
||||
}
|
||||
} else {
|
||||
|
||||
if(validation.error.formErrors.fieldErrors.attachments) {
|
||||
Logger.error('failed to send message doe to invalid attachment', {
|
||||
zodErrorList: validation.error.errors,
|
||||
data: message.attachments
|
||||
})
|
||||
} else {
|
||||
Logger.error('failed to send message, validation failed', {
|
||||
zodErrorList: validation.error.errors,
|
||||
data: message
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,16 +6,18 @@ import { v4 as uuidv4 } from 'uuid'
|
||||
import { AttachmentLocalDataSource } from '../../../../module/chat/data/repository/attachment/attachment-local-repository.service';
|
||||
import { RoomLocalRepository } from '../../../../module/chat/data/repository/room/room-local-repository.service';
|
||||
import { MemberListLocalRepository } from 'src/app/module/chat/data/repository/member/member-list-local-repository.service'
|
||||
import { Result } from 'neverthrow';
|
||||
import { err, Result } from 'neverthrow';
|
||||
import { RoomType } from 'src/app/core/chat/entity/group';
|
||||
import { MessageTable } from 'src/app/infra/database/dexie/instance/chat/schema/message';
|
||||
import { MessageOutPutDataDTO } from 'src/app/core/chat/repository/dto/messageOutputDTO';
|
||||
import { MessageAttachmentSource, MessageOutPutDataDTO } from 'src/app/core/chat/repository/dto/messageOutputDTO';
|
||||
import { IDBoolean } from 'src/app/infra/database/dexie/type';
|
||||
import { IMemberLocalRepository } from '../../repository/member/member-local-repository';
|
||||
import { IMessageLocalRepository } from '../../repository/message/message-local-repository';
|
||||
import { IMessageSocketRepository } from '../../repository/message/message-socket-repository';
|
||||
import { IRoomLocalRepository } from '../../repository/room/room-local-repository';
|
||||
import { IAttachmentLocalRepository } from '../../repository/typing/typing-local-repository';
|
||||
import { IMessageRemoteRepository } from '../../repository/message/message-remote-repository';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -29,6 +31,8 @@ export class SendLocalMessagesUseCaseService {
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
private MemberListLocalRepository: IMemberLocalRepository,
|
||||
private messageSocketRepositoryService: IMessageSocketRepository,
|
||||
private MessageRemoteRepository: IMessageRemoteRepository,
|
||||
private AttachmentLocalRepositoryService: IAttachmentLocalRepository,
|
||||
) { }
|
||||
|
||||
async execute() {
|
||||
@@ -53,17 +57,49 @@ export class SendLocalMessagesUseCaseService {
|
||||
|
||||
if(attachments.isOk()) {
|
||||
|
||||
message.attachments = attachments.value.map(e => ({
|
||||
fileType: e.fileType,
|
||||
source: e.source,
|
||||
fileName: e.fileName,
|
||||
applicationId: e.applicationId,
|
||||
docId: e.docId,
|
||||
id: e.id,
|
||||
mimeType: e.mimeType,
|
||||
description: e.description,
|
||||
file: e.base64.split(',')[1]
|
||||
}))
|
||||
message.attachments = attachments.value.map(e => {
|
||||
const base64 = typeof e.base64 === "string"
|
||||
? e.base64.replace(/^data:[a-zA-Z]+\/[a-zA-Z]+;base64,/, "")
|
||||
: "";
|
||||
|
||||
return {
|
||||
fileType: e.fileType,
|
||||
source: e.source,
|
||||
fileName: e.fileName,
|
||||
applicationId: e.applicationId,
|
||||
docId: e.docId,
|
||||
id: e.id,
|
||||
mimeType: e.mimeType,
|
||||
description: e.description,
|
||||
file: base64
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
var i = 0;
|
||||
for (const att of attachments.value) {
|
||||
|
||||
const isWebtrix = att.source === MessageAttachmentSource.Webtrix;
|
||||
var a = await this.MessageRemoteRepository.messageAttachmentUpload({
|
||||
fileType: att.fileType,
|
||||
source: att.source,
|
||||
file: isWebtrix ? null : att.base64.replace(/^data:[a-zA-Z]+\/[a-zA-Z]+;base64,/, ''),
|
||||
fileName: att.fileName,
|
||||
applicationId: att.applicationId || 0,
|
||||
docId: att.docId || 0,
|
||||
mimeType: att.mimeType,
|
||||
description: att.description || "something",
|
||||
senderId: SessionStore.user.UserId,
|
||||
});
|
||||
|
||||
if(a.isOk()) {
|
||||
att.id = a.value as string;
|
||||
message.attachments[i].id = a.value as string;
|
||||
await this.AttachmentLocalRepositoryService.update(att.$id, att)
|
||||
} else {
|
||||
return err('failed to upload attachment')
|
||||
}
|
||||
}
|
||||
|
||||
await this.messageLocalDataSourceService.update(message.$id, { sending: true })
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import { z } from 'zod';
|
||||
|
||||
const SocketMessageCreateOutputSchema = MessageEntitySchema.pick({
|
||||
id: true,
|
||||
attachments: true,
|
||||
canEdit: true,
|
||||
editedAt: true,
|
||||
info: true,
|
||||
|
||||
@@ -7,7 +7,6 @@ import { IMessageLocalRepository } from '../../repository/message/message-local-
|
||||
|
||||
const SocketMessageDeleteOutputSchema = MessageEntitySchema.pick({
|
||||
id: true,
|
||||
attachments: true,
|
||||
canEdit: true,
|
||||
editedAt: true,
|
||||
info: true,
|
||||
|
||||
@@ -11,7 +11,6 @@ import { IMessageLocalRepository } from '../../repository/message/message-local-
|
||||
|
||||
const SocketMessageUpdateOutputSchema = MessageEntitySchema.pick({
|
||||
id: true,
|
||||
attachments: true,
|
||||
canEdit: true,
|
||||
editedAt: true,
|
||||
info: true,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { HttpErrorResponse } from "@angular/common/http";
|
||||
import { Result } from "neverthrow";
|
||||
import { HttpResult } from "src/app/infra/http/type";
|
||||
import { UserLoginInput } from "../use-case/user-login-use-case.service";
|
||||
import { z } from "zod";
|
||||
import { UserPhotoUploadInput } from "../use-case/user-photo-upload.service";
|
||||
|
||||
const UserRepositoryLoginParams = z.object({
|
||||
Auth: z.string(),
|
||||
@@ -55,4 +55,5 @@ export abstract class IUserRemoteRepository {
|
||||
abstract login(input: IUserRepositoryLoginParams): Promise<Result<HttpResult<UserLoginOutputResponse>, HttpErrorResponse>>
|
||||
abstract logout(): Promise<Result<HttpResult<any>, HttpErrorResponse>>
|
||||
abstract refreshToken(input:UserRefreshTokenInputDTO): Promise<Result<HttpResult<any>, HttpErrorResponse>>
|
||||
abstract uploadPhoto(input: UserPhotoUploadInput): Promise<Result<HttpResult<any>, HttpErrorResponse>>
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { UserLocalRepositoryService } from 'src/app/module/user/data/datasource/user-local-repository.service';
|
||||
import { UserRemoteRepositoryService } from 'src/app/module/user/data/datasource/user-remote-repository.service';
|
||||
import { TracingType } from 'src/app/services/monitoring/opentelemetry/tracer';
|
||||
import { z, ZodError, ZodSchema } from 'zod';
|
||||
|
||||
const UserGetByIdInputSchema = z.object({
|
||||
@@ -35,5 +38,21 @@ export type UserGetByIdOutput = z.infer<typeof UserGetByIdOutputSchema>
|
||||
})
|
||||
export class UserGetByIdService {
|
||||
|
||||
constructor() { }
|
||||
constructor(
|
||||
private remote: UserRemoteRepositoryService,
|
||||
private local: UserLocalRepositoryService
|
||||
) { }
|
||||
|
||||
async execute(guid: string , tracing?: TracingType) {
|
||||
const result = await this.remote.getUserProfilePhoto(guid, tracing)
|
||||
|
||||
if(result.isOk()) {
|
||||
this.local.addProfilePicture({base64: result.value.data.data.userPhoto})
|
||||
} else {
|
||||
tracing?.setAttribute("picture.upload", "false")
|
||||
tracing?.hasError("cant upload picture")
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { z } from 'zod';
|
||||
import { IUserRemoteRepository } from '../repository/user-remote-repository';
|
||||
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
|
||||
import { UserLocalRepositoryService } from 'src/app/module/user/data/datasource/user-local-repository.service';
|
||||
|
||||
// Schema for photo validation
|
||||
export const UserPhotoUploadInputSchema = z.object({
|
||||
photo: z.string().regex(/^data:image\/[a-zA-Z]+;base64,/, {
|
||||
message: 'Photo must start with data:image/...;base64,',
|
||||
}),
|
||||
});
|
||||
|
||||
export type UserPhotoUploadInput = z.infer<typeof UserPhotoUploadInputSchema>;
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserPhotoUploadUseCase {
|
||||
constructor(
|
||||
private remote: IUserRemoteRepository,
|
||||
private local: UserLocalRepositoryService
|
||||
) {}
|
||||
|
||||
@XTracerAsync({ name: 'UserLoginUseCaseService', module: 'user', bugPrint: true })
|
||||
async execute(input: unknown, tracing?: TracingType) {
|
||||
// ✅ Validate input with Zod
|
||||
const parsed = UserPhotoUploadInputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
tracing?.setAttribute("picture.upload", "false");
|
||||
tracing?.hasError("invalid input: " + JSON.stringify(parsed.error.format()));
|
||||
}
|
||||
|
||||
const validInput = parsed.data;
|
||||
|
||||
const result = await this.remote.uploadPhoto(validInput);
|
||||
|
||||
if (result.isOk()) {
|
||||
this.local.addProfilePicture({ base64: validInput.photo });
|
||||
} else {
|
||||
tracing?.setAttribute("picture.upload", "false");
|
||||
tracing?.hasError("cant upload picture");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user