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,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)
}
}
@@ -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)
}
}
@@ -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')
}
}