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

226 lines
4.8 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';
2024-07-18 16:19:30 +01:00
import { err, ok, Result } from 'neverthrow';
2024-06-05 10:28:38 +01:00
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({
2024-08-06 11:24:00 +01:00
$id: z.any().optional(),
id: z.string().optional(),
2024-06-05 10:28:38 +01:00
roomId: z.string().uuid(),
2024-07-16 14:15:58 +01:00
message: z.string(),
messageType: z.number(),
canEdit: z.boolean(),
oneShot: z.boolean(),
sentAt: z.string().optional(),
requireUnlock: z.boolean(),
sender: z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
userPhoto: z.string(),
}),
sending: z.boolean().optional()
})
export const IncomingMessageSchema = z.object({
messageId: z.string().optional(),
roomId: z.string().uuid(),
2024-06-05 10:28:38 +01:00
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 & {
2024-08-06 11:24:00 +01:00
message: EntityTable<TableMessage, '$id'>;
2024-06-05 10:28:38 +01:00
};
messageDataSource.version(1).stores({
2024-08-06 11:24:00 +01:00
message: '++$id, id, roomId, message, messageType, canEdit, oneShot, requireUnlock, messageId, info'
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-07-18 16:19:30 +01:00
async getLastMessageByRoomId(roomId: string): Promise<Result<undefined|TableMessage, any>> {
try {
2024-07-25 08:51:04 +01:00
console.log({roomId})
2024-07-18 16:19:30 +01:00
const lastMessage = await messageDataSource.message
.where('roomId')
.equals(roomId)
.reverse()
.sortBy('id');
return ok(lastMessage[0]); // Get the last message
} catch (error) {
return err(error);
}
}
2024-08-06 11:24:00 +01:00
async deleteByMessageId(id: string): Promise<Result<undefined|TableMessage, any>> {
2024-07-31 17:23:44 +01:00
try {
2024-08-06 11:24:00 +01:00
console.log(id)
2024-07-31 17:23:44 +01:00
const lastMessage = await messageDataSource.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-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-07-11 12:12:43 +01:00
incomingSocketMessage() {
}
2024-07-25 08:51:04 +01:00
// @ValidateSchema(tableSchema)
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-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-07-31 17:23:44 +01:00
const existingMessage = await messageDataSource.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-06-11 08:37:52 +01:00
async update(data: TableMessage ) {
2024-06-05 14:31:26 +01:00
try {
2024-08-06 11:24:00 +01:00
const result = await messageDataSource.message.update(data.$id, data)
2024-08-01 16:38:08 +01:00
return ok(result)
} catch (e) {
return err(false)
}
}
async updateByMessageId(data: TableMessage ) {
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) {
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()) {
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-08-06 11:24:00 +01:00
messageDataSource.message.where('roomId').equals(roomId).sortBy('$id')
2024-06-13 12:11:17 +01:00
)
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-06 11:24:00 +01:00
const a = await messageDataSource.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
subscribeToNewMessage(roomId: string): Observable<TableMessage> {
return this.messageSubject.pipe(
filter((message: TableMessage) =>
message.roomId === roomId
)
)
}
2024-06-05 10:28:38 +01:00
}