fix message

This commit is contained in:
Peter Maquiran
2024-09-05 11:45:54 +01:00
parent 106267aee9
commit b0e1dd74cd
16 changed files with 237 additions and 78 deletions
@@ -3,7 +3,7 @@ import { liveQuery } from 'Dexie';
import { MessageEntity } from '../../../../../core/chat/entity/message';
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
import { MessageTable, MessageTableSchema } from 'src/app/infra/database/dexie/instance/chat/schema/message';
import { DexieMessageTable, MessageTable, MessageTableSchema } from 'src/app/infra/database/dexie/instance/chat/schema/message';
import { chatDatabase } from 'src/app/infra/database/dexie/service';
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
import { BehaviorSubject, combineLatest, from, Observable } from 'rxjs';
@@ -13,13 +13,13 @@ import { v4 as uuidv4 } from 'uuid'
@Injectable({
providedIn: 'root'
})
export class MessageLocalDataSourceService extends DexieRepository<MessageTable, MessageEntity> implements IMessageLocalRepository {
export class MessageLocalDataSourceService extends DexieRepository<MessageTable, MessageEntity, DexieMessageTable> implements IMessageLocalRepository {
private creatingSubject : BehaviorSubject<MessageTable> = new BehaviorSubject<MessageTable>(null);
private lastTimestamp = 0;
constructor() {
super(chatDatabase.message, MessageTableSchema)
super(chatDatabase.message, MessageTableSchema, chatDatabase)
this.setAllSenderToFalse();
this.onCreatingHook()
@@ -54,22 +54,33 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable,
}
async setAllSenderToFalse() {
// this.createTransaction(async (table) => {
// const result = await this.find({sending: true })
// if(result.isOk()) {
// for(const message of result.value) {
// await this.update(message.$id, { sending: false })
// }
// }
// })
try {
await chatDatabase.transaction('rw', chatDatabase.message, async () => {
// Perform the update operation within the transaction
await chatDatabase.message.toCollection().modify({ sending: false });
});
// console.log('All messages updated successfully.');
} catch (error) {
console.error('Error updating messages:', error);
}
}
getItems(roomId: string): PromiseExtended<MessageEntity[]> {
return chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any
return chatDatabase.message.where('roomId').equals(roomId).sortBy('$createAt') as any
}
getItemsLive(roomId: string): DexieObservable<MessageEntity[]> {
return liveQuery(() => chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any)
return liveQuery(() => chatDatabase.message.where('roomId').equals(roomId).sortBy('$createAt') as any)
}
async getOfflineMessages () {
@@ -4,6 +4,8 @@ import { Logger } from 'src/app/services/logger/main/service';
import { convertBlobToDataURL, createBlobUrl } from 'src/app/utils/ToBase64';
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/repository/attachment/attachment-local-repository.service'
import { z } from 'zod';
import { zodSafeValidation } from 'src/app/utils/zodValidation';
import { IMessage, MessageEntitySchema } from 'src/app/core/chat/entity/message';
const DownloadMessageAttachmentByMessageIdSchema = z.object({
$messageId: z.string(),
@@ -24,28 +26,43 @@ export class DownloadMessageAttachmentUserCaseService {
) { }
async execute(input: DownloadMessageAttachmentByMessageId) {
const result = await this.AttachmentRemoteDataSourceService.getAttachment(input.$messageId)
return result.asyncMap(async (blob) => {
const dataUrl = await createBlobUrl(blob)
const validation = zodSafeValidation<IMessage>(DownloadMessageAttachmentByMessageIdSchema, input)
if(dataUrl.isOk()) {
if(validation.isOk()) {
Logger.info('downloaded file #1', {
// data: dataUrl.slice(0, 100)+'...',
context: 'DownloadMessageAttachmentUserCaseService'
})
const result = await this.AttachmentRemoteDataSourceService.getAttachment(input.id)
return result.asyncMap(async (blob) => {
this.AttachmentLocalDataSource.insert({
$messageId: input.$messageId,
id: input.id,
file: blob
})
const dataUrl = await createBlobUrl(blob)
return dataUrl
} else {
if(dataUrl.isOk()) {
Logger.info('downloaded file #1', {
// data: dataUrl.slice(0, 100)+'...',
context: 'DownloadMessageAttachmentUserCaseService'
})
this.AttachmentLocalDataSource.insert({
$messageId: input.$messageId,
id: input.id,
file: blob
})
return dataUrl.value
} else {
}
})
} else {
Logger.error('failed to download message doe to invalid attachment', {
zodErrorList: validation.error.errors,
data: input
})
return validation
}
}
})
}
}
@@ -49,7 +49,7 @@ export class SocketMessageUpdateUseCaseService {
messageToSave.sending = false
if(result.isOk() && result.value) {
tracing?.addEvent("Message found")
const updateResult = await this.messageLocalDataSourceService.update(result.value.$id, messageToSave)
tracing.setAttribute('outcome', 'success')