import { Injectable } from '@angular/core'; 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 { chatDatabase } from 'src/app/infra/database/dexie/service'; import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository'; @Injectable({ providedIn: 'root' }) export class MessageLocalDataSourceService extends DexieRepository implements IMessageLocalRepository { constructor() { super(chatDatabase.message, MessageTableSchema) this.setAllSenderToFalse(); } async setAllSenderToFalse() { try { await chatDatabase.transaction('rw', chatDatabase.message, async () => { // Perform the update operation within the transaction await chatDatabase.message.toCollection().modify({ sending: false }); }); } catch (error) { console.error('Error updating messages:', error); } } getItems(roomId: string): PromiseExtended { return chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any } getItemsLive(roomId: string): DexieObservable { return liveQuery(() => chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any) } async getOfflineMessages () { try { const allMessages = await chatDatabase.message .filter(msg => typeof msg.id !== 'string' && msg.sending == false) .toArray(); return allMessages as MessageEntity[]; } catch (error) { console.error('Error fetching messages:', error); } } }