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';
|
|
|
|
|
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(),
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
|
|
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2024-06-05 15:35:38 +01:00
|
|
|
console.log('add error')
|
2024-06-05 14:31:26 +01:00
|
|
|
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-10 16:34:43 +01:00
|
|
|
getItemsLive(roomId: string) {
|
|
|
|
|
return liveQuery(() => messageDataSource.message.where('roomId').equals(roomId).toArray() )
|
2024-06-05 10:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-06-05 15:35:38 +01:00
|
|
|
async findMessageById(messageId: string) {
|
|
|
|
|
try {
|
|
|
|
|
console.log('messageId', messageId)
|
|
|
|
|
const a = await messageDataSource.message.where('messageId').equals(messageId).first()
|
|
|
|
|
|
|
|
|
|
if(a) {
|
|
|
|
|
return ok(a)
|
|
|
|
|
} else {
|
|
|
|
|
return err('not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('error')
|
|
|
|
|
return err('DB error')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-05 10:28:38 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|