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-07-18 16:19:30 +01:00
|
|
|
import { err, ok, Result } from 'neverthrow';
|
2024-08-13 10:52:35 +01:00
|
|
|
import { Observable, Subject } from 'rxjs';
|
2024-08-06 12:01:59 +01:00
|
|
|
import { filter } from 'rxjs/operators';
|
|
|
|
|
import { MessageEntity } from '../../../domain/entity/message';
|
2024-08-07 16:31:31 +01:00
|
|
|
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
|
2024-08-07 19:30:20 +01:00
|
|
|
import { MessageTable } from 'src/app/module/chat/infra/database/dexie/schema/message';
|
|
|
|
|
import { chatDatabase } from '../../../infra/database/dexie/service';
|
2024-08-13 10:52:35 +01:00
|
|
|
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
|
2024-06-05 10:28:38 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
2024-08-07 19:30:20 +01:00
|
|
|
export class MessageLocalDataSourceService extends DexieRepository<MessageTable> {
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-06-13 12:11:17 +01:00
|
|
|
messageSubject = new Subject();
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-06-13 12:11:17 +01:00
|
|
|
constructor() {
|
2024-08-07 19:30:20 +01:00
|
|
|
super(chatDatabase.message)
|
2024-06-13 12:11:17 +01:00
|
|
|
}
|
2024-06-05 10:28:38 +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
|
|
|
});
|
|
|
|
|
console.log('All messages updated successfully.');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error updating messages:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-08-07 19:30:20 +01:00
|
|
|
async getLastMessageByRoomId(roomId: string): Promise<Result<undefined|MessageTable, any>> {
|
2024-07-18 16:19:30 +01:00
|
|
|
try {
|
2024-07-25 08:51:04 +01:00
|
|
|
console.log({roomId})
|
2024-08-07 19:30:20 +01:00
|
|
|
const lastMessage = await chatDatabase.message
|
2024-07-18 16:19:30 +01:00
|
|
|
.where('roomId')
|
|
|
|
|
.equals(roomId)
|
|
|
|
|
.reverse()
|
|
|
|
|
.sortBy('id');
|
|
|
|
|
|
|
|
|
|
return ok(lastMessage[0]); // Get the last message
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return err(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 19:30:20 +01:00
|
|
|
async deleteByMessageId(id: string): Promise<Result<undefined|MessageTable, any>> {
|
2024-07-31 17:23:44 +01:00
|
|
|
try {
|
2024-08-06 11:24:00 +01:00
|
|
|
console.log(id)
|
2024-08-07 19:30:20 +01:00
|
|
|
const lastMessage = await chatDatabase.message
|
2024-08-06 11:24:00 +01:00
|
|
|
.where('id')
|
|
|
|
|
.equals(id).delete()
|
2024-07-31 17:23:44 +01:00
|
|
|
|
|
|
|
|
return ok(lastMessage[0]); // Get the last message
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return err(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-18 16:19:30 +01:00
|
|
|
|
2024-08-13 10:52:35 +01:00
|
|
|
async sendMessage(data: MessageTable) {
|
2024-06-11 08:37:52 +01:00
|
|
|
|
2024-08-07 19:30:20 +01:00
|
|
|
(data as MessageTable).sending = true
|
2024-06-11 08:37:52 +01:00
|
|
|
|
|
|
|
|
try {
|
2024-08-07 19:30:20 +01:00
|
|
|
const result = await chatDatabase.message.add(data)
|
2024-08-06 16:53:13 +01:00
|
|
|
this.messageSubject.next({roomId: data.roomId});
|
2024-08-07 11:18:41 +01:00
|
|
|
return ok(result as number)
|
2024-06-11 08:37:52 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-07-25 08:51:04 +01:00
|
|
|
// @ValidateSchema(tableSchema)
|
2024-08-13 10:52:35 +01:00
|
|
|
async createMessage(data: MessageTable) {
|
2024-06-05 10:28:38 +01:00
|
|
|
|
|
|
|
|
try {
|
2024-08-07 19:30:20 +01:00
|
|
|
const result = await chatDatabase.message.add(data)
|
2024-08-06 16:53:13 +01:00
|
|
|
this.messageSubject.next({roomId: data.roomId});
|
2024-08-07 11:18:41 +01:00
|
|
|
return ok(result)
|
2024-06-05 14:31:26 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 10:52:35 +01:00
|
|
|
async createManyMessage(data: MessageTable[]) {
|
2024-08-07 16:31:31 +01:00
|
|
|
|
|
|
|
|
try {
|
2024-08-07 19:30:20 +01:00
|
|
|
const result = await chatDatabase.message.bulkAdd(data)
|
2024-08-07 16:31:31 +01:00
|
|
|
this.messageSubject.next({roomId: data[0].roomId});
|
|
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-05 14:31:26 +01:00
|
|
|
|
2024-08-06 11:24:00 +01:00
|
|
|
async messageExist({id}) {
|
2024-07-31 17:23:44 +01:00
|
|
|
try {
|
2024-08-01 16:38:08 +01:00
|
|
|
|
2024-08-06 11:24:00 +01:00
|
|
|
console.log({id});
|
2024-08-01 16:38:08 +01:00
|
|
|
|
2024-08-07 19:30:20 +01:00
|
|
|
const existingMessage = await chatDatabase.message
|
2024-08-06 11:24:00 +01:00
|
|
|
.where('id')
|
|
|
|
|
.equals(id)
|
2024-07-31 17:23:44 +01:00
|
|
|
.first();
|
|
|
|
|
|
|
|
|
|
if (existingMessage) {
|
2024-08-01 16:38:08 +01:00
|
|
|
return ok(existingMessage)
|
2024-07-31 17:23:44 +01:00
|
|
|
} else {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return err(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 11:18:41 +01:00
|
|
|
// not used
|
2024-08-07 19:30:20 +01:00
|
|
|
async updateByMessageId(data: MessageTable ) {
|
2024-08-01 16:38:08 +01:00
|
|
|
|
|
|
|
|
try {
|
2024-08-07 19:30:20 +01:00
|
|
|
const result = await chatDatabase.message.update(data.id as any, data)
|
2024-06-05 10:28:38 +01:00
|
|
|
return ok(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 15:35:38 +01:00
|
|
|
|
2024-08-07 19:30:20 +01:00
|
|
|
async findOrUpdate(data: MessageTable) {
|
2024-08-06 11:24:00 +01:00
|
|
|
const findResult = await this.findMessageById(data.id)
|
2024-06-05 15:35:38 +01:00
|
|
|
|
|
|
|
|
if(findResult.isOk()) {
|
2024-08-07 16:31:31 +01:00
|
|
|
return this.update(findResult.value.$id, data)
|
2024-06-05 15:35:38 +01:00
|
|
|
} else {
|
|
|
|
|
return this.createMessage(data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-06 11:24:00 +01:00
|
|
|
async findMessageById(id: string) {
|
2024-06-05 15:35:38 +01:00
|
|
|
try {
|
2024-08-07 19:30:20 +01:00
|
|
|
const a = await chatDatabase.message.where('id').equals(id).first()
|
2024-06-05 15:35:38 +01:00
|
|
|
|
|
|
|
|
if(a) {
|
|
|
|
|
return ok(a)
|
|
|
|
|
} else {
|
|
|
|
|
return err('not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err('DB error')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-05 10:28:38 +01:00
|
|
|
|
2024-06-13 12:11:17 +01:00
|
|
|
|
2024-08-07 19:30:20 +01:00
|
|
|
subscribeToNewMessage(roomId: string): Observable<MessageTable> {
|
2024-06-13 12:11:17 +01:00
|
|
|
return this.messageSubject.pipe(
|
2024-08-07 19:30:20 +01:00
|
|
|
filter((message: MessageTable) =>
|
2024-06-13 12:11:17 +01:00
|
|
|
message.roomId === roomId
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}
|
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-06-05 10:28:38 +01:00
|
|
|
}
|
|
|
|
|
|