upload attachment

This commit is contained in:
Peter Maquiran
2024-08-13 17:05:46 +01:00
parent 251f533a68
commit d7eb6a552b
20 changed files with 436 additions and 152 deletions
@@ -17,6 +17,7 @@ import { ListenSendMessageUseCase } from './use-case/listen-send-message.service
import { filter } from 'rxjs/operators';
import { v4 as uuidv4 } from 'uuid'
import { MessageEntity } from './entity/message';
import { MessageAttachmentByMessageIdInput, MessageAttachmentByMessageIdUseCase } from './use-case/message-attachment-by-message-id.service';
export const InstanceId = uuidv4();
@@ -40,7 +41,8 @@ export class ChatServiceService {
private ListenMessageByRoomIdNewUseCase: ListenMessageByRoomIdNewUseCase,
private ListenMessageDeleteService: ListenMessageDeleteByRoomIdService,
private ListenMessageUpdateByRoomIdUseCase: ListenMessageUpdateByRoomIdUseCase,
private ListenSendMessageUseCase: ListenSendMessageUseCase
private ListenSendMessageUseCase: ListenSendMessageUseCase,
private MessageAttachmentByMessageIdService: MessageAttachmentByMessageIdUseCase
) {
this.messageLiveSignalRDataSourceService.getMessageDelete()
.pipe()
@@ -117,6 +119,10 @@ export class ChatServiceService {
return this.MessageCreateUseCaseService.execute(input);
}
getMessageAttachmentByMessageId(input: MessageAttachmentByMessageIdInput) {
return this.MessageAttachmentByMessageIdService.execute(input)
}
listenToIncomingMessage(roomId:string) {
return this.ListenMessageByRoomIdNewUseCase.execute({roomId})
}
+3 -5
View File
@@ -1,5 +1,6 @@
import { z } from "zod";
import { MessageAttachmentFileType, MessageAttachmentSource } from "../../data/dto/message/messageOutputDTO";
import { SafeResourceUrl } from "@angular/platform-browser";
const MessageEntitySchema = z.object({
$id: z.any().optional(),
@@ -55,6 +56,7 @@ export class MessageEntity implements Message {
sendAttemp = 0
attachments: {
safeFile?: SafeResourceUrl;
fileType: MessageAttachmentFileType,
source: MessageAttachmentSource,
file?: string,
@@ -63,11 +65,7 @@ export class MessageEntity implements Message {
docId?: string,
mimeType?: string,
description?: string
}[] = []
attachmentsSource: {
id: string,
file: string
id?: string
}[] = []
reactions = []
@@ -0,0 +1,53 @@
import { Injectable } from '@angular/core';
import { z } from 'zod';
import { AttachmentRemoteDataSourceService } from 'src/app/module/chat/data/data-source/attachment/attachment-remote-data-source.service'
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/data-source/attachment/attachment-local-data-source.service'
import { convertBlobToDataURL } from 'src/app/utils/ToBase64';
import { Result } from 'neverthrow';
const MessageAttachmentByMessageIdSchema = z.object({
$messageId: z.number(),
id: z.string()
})
export type MessageAttachmentByMessageIdInput = z.infer<typeof MessageAttachmentByMessageIdSchema>
@Injectable({
providedIn: 'root'
})
export class MessageAttachmentByMessageIdUseCase {
constructor(
private AttachmentRemoteDataSourceService: AttachmentRemoteDataSourceService,
private AttachmentLocalDataSource: AttachmentLocalDataSource
) { }
async execute(input: MessageAttachmentByMessageIdInput): Promise<Result<string, any>> {
const getLocalAttachment = await this.AttachmentLocalDataSource.findOne({
$messageId: input.$messageId
})
if(getLocalAttachment.isOk() && getLocalAttachment.value) {
if(getLocalAttachment.value) {
console.log('found local', getLocalAttachment.value)
return getLocalAttachment.map(e => e.file)
}
} else {
const result = await this.AttachmentRemoteDataSourceService.getAttachment(input)
return result.asyncMap(async (e) => {
const dataUrl = await convertBlobToDataURL(e)
this.AttachmentLocalDataSource.insert({
$messageId: input.$messageId,
id: input.id,
file: dataUrl
})
return dataUrl
})
}
}
}
@@ -1,10 +1,12 @@
import { Injectable } from '@angular/core';
import { MessageEntity } from '../entity/message';
import { SessionStore } from 'src/app/store/session.service';
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'
import { InstanceId } from '../chat-service.service';
import { createDataURL } from 'src/app/utils/ToBase64';
import { DomSanitizer } from '@angular/platform-browser';
const MessageInputUseCaseSchema = z.object({
memberId: z.number(),
@@ -20,17 +22,55 @@ export type MessageInputUseCase = z.infer< typeof MessageInputUseCaseSchema>
export class MessageCreateUseCaseService {
constructor(
private MessageRepositoryService: MessageRepositoryService
private MessageRepositoryService: MessageRepositoryService,
private AttachmentRepositoryService: AttachmentRepositoryService,
private sanitizer: DomSanitizer
) { }
async execute(input: MessageEntity) {
async execute(message: MessageEntity) {
input.sendAttemp++;
message.sendAttemp++;
input.requestId = InstanceId +'@'+ uuidv4();
message.requestId = InstanceId +'@'+ uuidv4();
const result = await this.MessageRepositoryService.sendMessage(input)
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)
return result
}
@@ -1,6 +1,5 @@
import { Injectable } from '@angular/core';
import { MessageEntity } from '../entity/message';
import { SessionStore } from 'src/app/store/session.service';
import { MessageRepositoryService } from "src/app/module/chat/data/repository/message-respository.service"
import { z } from 'zod';
@@ -24,12 +23,12 @@ export class MessageCreateUseCaseService {
async execute(input: MessageEntity) {
const result = await this.MessageRepositoryService.createMessage(input)
// const result = await this.MessageRepositoryService.(input)
if(result.isOk()) {
// if(result.isOk()) {
}
// }
return result
// return result
}
}