Files
doneit-web/src/app/module/chat/data/data-source/message/message-local-data-source.service.ts
T

140 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-06-05 10:28:38 +01:00
import { Injectable } from '@angular/core';
import { Dexie, EntityTable, liveQuery } from 'Dexie';
import { err, ok } from 'neverthrow';
import { z } from 'zod';
2024-06-13 12:11:17 +01:00
import { from, Observable, Subject } from 'rxjs';
import { filter, switchMap } from 'rxjs/operators';
2024-06-05 10:28:38 +01:00
import { MessageInputDTO } from '../../dto/message/messageInputDtO';
const tableSchema = z.object({
id: z.any().optional(),
2024-06-05 14:31:26 +01:00
messageId: z.string().optional(),
2024-06-05 10:28:38 +01:00
roomId: z.string().uuid(),
senderId: z.number(),
message: z.string(),
messageType: z.number(),
canEdit: z.boolean(),
oneShot: z.boolean(),
2024-06-14 09:30:14 +01:00
sentAt: z.string().optional(),
2024-06-05 10:28:38 +01:00
requireUnlock: z.boolean(),
2024-06-05 15:35:38 +01:00
sender: z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
2024-06-11 08:37:52 +01:00
userPhoto: z.string(),
}),
sending: z.boolean().optional()
2024-06-05 10:28:38 +01:00
})
2024-06-11 08:37:52 +01:00
2024-06-05 10:28:38 +01:00
export type TableMessage = z.infer<typeof tableSchema>
// Database declaration (move this to its own module also)
export const messageDataSource = new Dexie('chat-message') as Dexie & {
message: EntityTable<TableMessage, 'id'>;
};
messageDataSource.version(1).stores({
2024-06-05 14:31:26 +01:00
message: '++id, roomId, senderId, message, messageType, canEdit, oneShot, requireUnlock, messageId'
2024-06-05 10:28:38 +01:00
});
@Injectable({
providedIn: 'root'
})
export class MessageLocalDataSourceService {
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() {
messageDataSource.message.hook('creating', (primKey, obj, trans) => {
// const newMessage = await trans.table('message').get(primKey);
2024-06-14 09:30:14 +01:00
this.messageSubject.next(obj);
2024-06-13 12:11:17 +01:00
// return newMessage
})
}
2024-06-05 10:28:38 +01:00
2024-06-11 08:37:52 +01:00
async sendMessage(data: MessageInputDTO) {
(data as TableMessage).sending = true
try {
const result = await messageDataSource.message.add(data)
return ok(result as string)
} catch (e) {
return err(false)
}
}
2024-06-05 10:28:38 +01:00
async createMessage(data: MessageInputDTO) {
try {
const result = await messageDataSource.message.add(data)
2024-06-05 14:31:26 +01:00
return ok(result as string)
} catch (e) {
return err(false)
}
}
2024-06-11 08:37:52 +01:00
async update(data: TableMessage ) {
2024-06-05 14:31:26 +01:00
try {
const result = await messageDataSource.message.update(data.id, 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
async findOrUpdate(data: TableMessage) {
const findResult = await this.findMessageById(data.messageId)
if(findResult.isOk()) {
return this.update({...findResult.value, ...data})
} else {
return this.createMessage(data)
}
}
2024-06-14 09:30:14 +01:00
getItemsLive(roomId: string) {
return liveQuery(() =>
2024-06-13 12:11:17 +01:00
messageDataSource.message.where('roomId').equals(roomId).sortBy('id')
)
2024-06-05 10:28:38 +01:00
}
2024-06-05 15:35:38 +01:00
async findMessageById(messageId: string) {
try {
const a = await messageDataSource.message.where('messageId').equals(messageId).first()
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
subscribeToNewMessage(roomId: string): Observable<TableMessage> {
return this.messageSubject.pipe(
filter((message: TableMessage) =>
message.roomId === roomId
)
)
}
2024-06-05 10:28:38 +01:00
}