mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
upload attachment
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Subject } from 'rxjs';
|
||||
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
|
||||
import { chatDatabase } from '../../../infra/database/dexie/service';
|
||||
import { AttachmentTable, AttachmentTableSchema } from '../../../infra/database/dexie/schema/attachment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AttachmentLocalDataSource extends DexieRepository<AttachmentTable> {
|
||||
|
||||
messageSubject = new Subject();
|
||||
|
||||
constructor() {
|
||||
super(chatDatabase.attachment, AttachmentTableSchema)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { Injectable, Input } from '@angular/core';
|
||||
import { HttpService } from 'src/app/services/http.service';
|
||||
import { DataSourceReturn } from 'src/app/services/Repositorys/type';
|
||||
import { MessageOutPutDTO } from '../../dto/message/messageOutputDTO';
|
||||
import { MessageAttachmentByMessageIdInput } from '../../../domain/use-case/message-attachment-by-message-id.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AttachmentRemoteDataSourceService {
|
||||
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
|
||||
|
||||
constructor(
|
||||
private httpService: HttpService
|
||||
) { }
|
||||
|
||||
async getAttachment(Input: MessageAttachmentByMessageIdInput): DataSourceReturn<Blob> {
|
||||
return await this.httpService.get(`${this.baseUrl}/attachment/${Input.id}`, { responseType: 'blob' });
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, Subject } from 'rxjs';
|
||||
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
|
||||
import { chatDatabase } from '../../../infra/database/dexie/service';
|
||||
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
|
||||
import { AttachmentTable } from '../../../infra/database/dexie/schema/attachment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AttachmentLocalDataSourceService extends DexieRepository<AttachmentTable> {
|
||||
|
||||
messageSubject = new Subject();
|
||||
|
||||
constructor() {
|
||||
super(chatDatabase.attachment)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+1
-2
@@ -18,7 +18,6 @@ const MemberTableSchema = z.object({
|
||||
joinAt: z.string(),
|
||||
status: z.string()
|
||||
})
|
||||
|
||||
export type IMemberTable = z.infer<typeof MemberTableSchema>
|
||||
|
||||
type IMemberTableSchema = EntityTable<IMemberTable, '$roomIdUserId'>
|
||||
@@ -37,7 +36,7 @@ roomMemberList.version(1).stores({
|
||||
export class MemberListLocalDataSourceService extends DexieRepository<IMemberTable> {
|
||||
|
||||
constructor() {
|
||||
super(roomMemberList.memberList);
|
||||
super(roomMemberList.memberList, MemberTableSchema);
|
||||
|
||||
// messageDataSource.message.hook('creating', (primKey, obj, trans) => {
|
||||
// // const newMessage = await trans.table('message').get(primKey);
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Observable, Subject } from 'rxjs';
|
||||
import { filter } from 'rxjs/operators';
|
||||
import { MessageEntity } from '../../../domain/entity/message';
|
||||
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
|
||||
import { MessageTable } from 'src/app/module/chat/infra/database/dexie/schema/message';
|
||||
import { MessageTable, MessageTableSchema } from 'src/app/module/chat/infra/database/dexie/schema/message';
|
||||
import { chatDatabase } from '../../../infra/database/dexie/service';
|
||||
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
|
||||
|
||||
@@ -17,7 +17,7 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable>
|
||||
messageSubject = new Subject();
|
||||
|
||||
constructor() {
|
||||
super(chatDatabase.message)
|
||||
super(chatDatabase.message, MessageTableSchema)
|
||||
}
|
||||
|
||||
async setAllSenderToFalse() {
|
||||
@@ -63,16 +63,24 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable>
|
||||
|
||||
async sendMessage(data: MessageTable) {
|
||||
|
||||
(data as MessageTable).sending = true
|
||||
const dataValidation = MessageTableSchema.safeParse(data)
|
||||
if(dataValidation.success) {
|
||||
|
||||
try {
|
||||
const result = await chatDatabase.message.add(data)
|
||||
this.messageSubject.next({roomId: data.roomId});
|
||||
return ok(result as number)
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
const safeData = dataValidation.data
|
||||
safeData.sending = true
|
||||
|
||||
try {
|
||||
const result = await chatDatabase.message.add(safeData)
|
||||
this.messageSubject.next({roomId: safeData.roomId});
|
||||
return ok(result as number)
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log(dataValidation)
|
||||
return err(dataValidation)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/data-source/attachment/attachment-local-data-source.service'
|
||||
import { AttachmentTable } from '../../infra/database/dexie/schema/attachment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AttachmentRepositoryService {
|
||||
|
||||
constructor(
|
||||
private AttachmentLocalDataSourceService: AttachmentLocalDataSource
|
||||
) { }
|
||||
|
||||
create(data: AttachmentTable) {
|
||||
return this.AttachmentLocalDataSourceService.insert(data)
|
||||
}
|
||||
|
||||
get findOne() {
|
||||
return this.AttachmentLocalDataSourceService.findOne
|
||||
}
|
||||
|
||||
get insert() {
|
||||
return this.AttachmentLocalDataSourceService.insert
|
||||
}
|
||||
|
||||
get update() {
|
||||
return this.AttachmentLocalDataSourceService.update
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -14,8 +14,7 @@ import { MessageOutPutDataDTO } from '../dto/message/messageOutputDTO';
|
||||
import { MessageTable } from 'src/app/module/chat/infra/database/dexie/schema/message';
|
||||
import { MessageLocalDataSourceService } from '../data-source/message/message-local-data-source.service';
|
||||
import { MessageLiveDataSourceService } from '../data-source/message/message-live-signalr-data-source.service';
|
||||
import { AttachmentLocalDataSourceService } from 'src/app/module/chat/data/data-source/attachment/message-local-data-source.service'
|
||||
import { Subject } from 'rxjs';
|
||||
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/data-source/attachment/attachment-local-data-source.service'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -27,22 +26,13 @@ export class MessageRepositoryService {
|
||||
private messageLiveDataSourceService: MessageLiveDataSourceService,
|
||||
private messageLiveSignalRDataSourceService: SignalRService,
|
||||
private messageLocalDataSourceService: MessageLocalDataSourceService,
|
||||
private AttachmentLocalDataSourceService: AttachmentLocalDataSourceService
|
||||
private AttachmentLocalDataSourceService: AttachmentLocalDataSource
|
||||
) {}
|
||||
|
||||
|
||||
async createMessageLocally(entity: MessageEntity) {
|
||||
const requestId = InstanceId +'@'+ uuidv4();
|
||||
const roomId = entity.roomId
|
||||
|
||||
return await this.messageLocalDataSourceService.sendMessage(entity)
|
||||
|
||||
}
|
||||
|
||||
|
||||
async createMessage(entity: MessageEntity) {
|
||||
//const requestId = InstanceId +'@'+ uuidv4();
|
||||
|
||||
|
||||
const localActionResult = await this.messageLocalDataSourceService.sendMessage(entity)
|
||||
|
||||
return localActionResult.map(e => {
|
||||
@@ -52,42 +42,32 @@ export class MessageRepositoryService {
|
||||
}
|
||||
|
||||
async sendMessage(entity: MessageEntity) {
|
||||
const messageSubject = new Subject<MessageTable | string>();
|
||||
|
||||
const roomId = entity.roomId
|
||||
|
||||
entity.sending = true
|
||||
const localActionResult = await this.messageLocalDataSourceService.sendMessage(entity)
|
||||
|
||||
if(localActionResult.isOk()) {
|
||||
const DTO = MessageMapper.fromDomain(entity, entity.requestId)
|
||||
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage<MessageOutPutDataDTO>(DTO)
|
||||
// return this sendMessageResult
|
||||
const DTO = MessageMapper.fromDomain(entity, entity.requestId)
|
||||
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage<MessageOutPutDataDTO>(DTO)
|
||||
// return this sendMessageResult
|
||||
|
||||
if(sendMessageResult.isOk()) {
|
||||
if(sendMessageResult.isOk()) {
|
||||
|
||||
if(sendMessageResult.value.sender == undefined || sendMessageResult.value.sender == null) {
|
||||
if(sendMessageResult.value.sender == undefined || sendMessageResult.value.sender == null) {
|
||||
|
||||
delete sendMessageResult.value.sender
|
||||
}
|
||||
|
||||
let clone: MessageTable = {
|
||||
...sendMessageResult.value,
|
||||
id: sendMessageResult.value.id,
|
||||
$id : localActionResult.value
|
||||
}
|
||||
|
||||
// console.log('sendMessageResult.value', sendMessageResult.value)
|
||||
// this.attachment(sendMessageResult.value.attachments[0].id)
|
||||
return this.messageLocalDataSourceService.update(localActionResult.value, {...clone, sending: false, roomId: entity.roomId})
|
||||
} else {
|
||||
await this.messageLocalDataSourceService.update(localActionResult.value, {sending: false, $id: localActionResult.value})
|
||||
return err('no connection')
|
||||
delete sendMessageResult.value.sender
|
||||
}
|
||||
|
||||
let clone: MessageTable = {
|
||||
...sendMessageResult.value,
|
||||
id: sendMessageResult.value.id,
|
||||
$id : entity.$id
|
||||
}
|
||||
|
||||
return this.messageLocalDataSourceService.update(entity.$id, {...clone, sending: false, roomId: entity.roomId})
|
||||
} else {
|
||||
// return this.messageLocalDataSourceService.update({sending: false})
|
||||
await this.messageLocalDataSourceService.update(entity.$id, {sending: false, $id: entity.$id})
|
||||
return err('no connection')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { z } from "zod";
|
||||
import { EntityTable } from 'Dexie';
|
||||
import { zodDataUrlSchema } from "src/app/utils/zod";
|
||||
|
||||
export const AttachmentTableSchema = z.object({
|
||||
id: z.string(),
|
||||
$id: z.string(),
|
||||
messageId: z.string(),
|
||||
file: z.string(),
|
||||
id: z.string().optional(), // attachment id
|
||||
$id: z.number().optional(), // local id
|
||||
$messageId: z.number(),
|
||||
attachmentId: z.string().optional(),
|
||||
file: zodDataUrlSchema,
|
||||
})
|
||||
|
||||
export type AttachmentTable = z.infer<typeof AttachmentTableSchema>
|
||||
export type DexieAttachmentsTableSchema = EntityTable<AttachmentTable, '$id'>;
|
||||
export const AttachmentTableColumn = '++$id, id, messageId, file'
|
||||
export const AttachmentTableColumn = '++$id, id, $messageId, messageId, file'
|
||||
|
||||
@@ -2,11 +2,11 @@ import { nativeEnum, z } from "zod";
|
||||
import { EntityTable } from 'Dexie';
|
||||
import { MessageAttachmentFileType, MessageAttachmentSource } from "src/app/module/chat/data/dto/message/messageOutputDTO";
|
||||
|
||||
export const MessageTable = z.object({
|
||||
export const MessageTableSchema = z.object({
|
||||
$id: z.number().optional(),
|
||||
id: z.string().optional(),
|
||||
roomId: z.string().uuid(),
|
||||
message: z.string().nullable(),
|
||||
message: z.string().nullable().optional(),
|
||||
messageType: z.number(),
|
||||
canEdit: z.boolean(),
|
||||
oneShot: z.boolean(),
|
||||
@@ -29,14 +29,13 @@ export const MessageTable = z.object({
|
||||
attachments: z.array(z.object({
|
||||
fileType: z.nativeEnum(MessageAttachmentFileType),
|
||||
source: z.nativeEnum(MessageAttachmentSource),
|
||||
file: z.string().optional(),
|
||||
fileName: z.string().optional(),
|
||||
applicationId: z.string().optional(),
|
||||
docId: z.string().optional()
|
||||
})).optional()
|
||||
})
|
||||
|
||||
export type MessageTable = z.infer<typeof MessageTable>
|
||||
export type MessageTable = z.infer<typeof MessageTableSchema>
|
||||
export type DexieMessageTable = EntityTable<MessageTable, '$id'>;
|
||||
export const messageTableColumn = '++$id, id, roomId, senderId, message, messageType, canEdit, oneShot, requireUnlock'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user