2024-06-05 10:28:38 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2024-08-13 10:52:35 +01:00
|
|
|
import { liveQuery } from 'Dexie';
|
2024-08-26 14:47:03 +01:00
|
|
|
import { MessageEntity } from '../../../../../core/chat/entity/message';
|
2024-08-07 16:31:31 +01:00
|
|
|
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
|
2024-08-13 10:52:35 +01:00
|
|
|
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
|
2024-08-27 10:15:00 +01:00
|
|
|
import { MessageTable, MessageTableSchema } from 'src/app/infra/database/dexie/instance/chat/schema/message';
|
|
|
|
|
import { chatDatabase } from 'src/app/infra/database/dexie/service';
|
2024-08-27 20:29:57 +01:00
|
|
|
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
|
2024-09-02 12:33:43 +01:00
|
|
|
import { BehaviorSubject, combineLatest, from, Observable } from 'rxjs';
|
|
|
|
|
import { filter, map } from 'rxjs/operators';
|
2024-09-04 22:48:29 +01:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
2024-06-05 10:28:38 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
2024-08-27 20:29:57 +01:00
|
|
|
export class MessageLocalDataSourceService extends DexieRepository<MessageTable, MessageEntity> implements IMessageLocalRepository {
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-09-02 12:33:43 +01:00
|
|
|
private creatingSubject : BehaviorSubject<MessageTable> = new BehaviorSubject<MessageTable>(null);
|
2024-09-04 22:48:29 +01:00
|
|
|
private lastTimestamp = 0;
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-06-13 12:11:17 +01:00
|
|
|
constructor() {
|
2024-08-13 17:05:46 +01:00
|
|
|
super(chatDatabase.message, MessageTableSchema)
|
2024-08-15 10:26:20 +01:00
|
|
|
|
|
|
|
|
this.setAllSenderToFalse();
|
2024-09-02 15:27:07 +01:00
|
|
|
this.onCreatingHook()
|
2024-06-13 12:11:17 +01:00
|
|
|
}
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-09-02 12:33:43 +01:00
|
|
|
private onCreatingHook() {
|
|
|
|
|
chatDatabase.message.hook('creating', (primaryKey, obj, transaction) => {
|
|
|
|
|
|
2024-09-04 22:48:29 +01:00
|
|
|
let now = Date.now();
|
|
|
|
|
|
|
|
|
|
// If the current time is the same as the last, increment
|
|
|
|
|
if (now <= this.lastTimestamp) {
|
|
|
|
|
obj.$createAt = this.lastTimestamp + 1;
|
|
|
|
|
this.lastTimestamp = this.lastTimestamp + 1;
|
|
|
|
|
} else {
|
|
|
|
|
this.lastTimestamp = now;
|
|
|
|
|
obj.$createAt = now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(obj.id) {
|
|
|
|
|
obj.$id = obj.id
|
|
|
|
|
} else {
|
|
|
|
|
obj.$id = 'Local-'+uuidv4()
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 15:27:07 +01:00
|
|
|
this.creatingSubject.next(obj)
|
2024-09-02 12:33:43 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 15:27:07 +01:00
|
|
|
onCreateObservable() {
|
|
|
|
|
return this.creatingSubject.asObservable()
|
|
|
|
|
}
|
2024-09-02 12:33:43 +01:00
|
|
|
|
2024-08-07 16:02:05 +01:00
|
|
|
async setAllSenderToFalse() {
|
|
|
|
|
try {
|
2024-08-07 19:30:20 +01:00
|
|
|
await chatDatabase.transaction('rw', chatDatabase.message, async () => {
|
2024-08-07 16:02:05 +01:00
|
|
|
// Perform the update operation within the transaction
|
2024-08-07 19:30:20 +01:00
|
|
|
await chatDatabase.message.toCollection().modify({ sending: false });
|
2024-08-07 16:02:05 +01:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error updating messages:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-08-13 10:52:35 +01:00
|
|
|
getItems(roomId: string): PromiseExtended<MessageEntity[]> {
|
|
|
|
|
return chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any
|
2024-08-06 16:53:13 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 10:52:35 +01:00
|
|
|
getItemsLive(roomId: string): DexieObservable<MessageEntity[]> {
|
|
|
|
|
return liveQuery(() => chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any)
|
2024-06-05 10:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-07 15:23:23 +01:00
|
|
|
async getOfflineMessages () {
|
|
|
|
|
try {
|
2024-08-07 19:30:20 +01:00
|
|
|
const allMessages = await chatDatabase.message
|
2024-08-07 15:23:23 +01:00
|
|
|
.filter(msg => typeof msg.id !== 'string' && msg.sending == false)
|
|
|
|
|
.toArray();
|
|
|
|
|
|
|
|
|
|
return allMessages as MessageEntity[];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching messages:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-01 12:57:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getLastMessageForRooms(roomIds: string[]): Observable<any[]> {
|
|
|
|
|
const observables = roomIds.map(roomId =>
|
|
|
|
|
from (liveQuery(async() =>{
|
|
|
|
|
const messages = await chatDatabase.message
|
|
|
|
|
.where('roomId')
|
|
|
|
|
.equals(roomId)
|
|
|
|
|
.reverse()
|
|
|
|
|
.sortBy('timestamp')
|
|
|
|
|
|
|
|
|
|
return messages[0] || null; // Return the first item (latest message) or null if no message
|
|
|
|
|
})).pipe(
|
|
|
|
|
map((message) => ({ roomId, message: message || null })) // Attach roomId to the result
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return combineLatest(observables); // Combine all observables into one array of results
|
|
|
|
|
}
|
2024-06-05 10:28:38 +01:00
|
|
|
}
|
|
|
|
|
|