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
@@ -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
}
}