send and receive message

This commit is contained in:
Peter Maquiran
2024-07-11 10:28:21 +01:00
parent 386ca67315
commit b0325a5558
50 changed files with 720 additions and 372 deletions
@@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import { err, ok } from 'neverthrow';
import { WebSocketMessage, WebSocketService } from '../../../infra/socket/socket';
import { SignalRService } from '../../../infra/socket/signal-r.service';
@Injectable({
providedIn: 'root'
})
export class MessageLiveDataSourceService {
constructor(
public socket: WebSocketService,
private signalR: SignalRService) {}
async sendMessage(data: WebSocketMessage) {
try {
const result = await this.socket.sendMessage(data).toPromise()
return ok(result)
} catch (e) {
return err(e)
}
}
}
@@ -0,0 +1,38 @@
import { Injectable } from '@angular/core';
import { err, ok } from 'neverthrow';
import { SignalRService } from '../../../infra/socket/signal-r.service';
interface msgObj {
roomId: string;
senderId: string;
message:string;
messageType:1;
canEdit:Boolean;
oneShot:Boolean;
requestId: string;
}
@Injectable({
providedIn: 'root'
})
export class MessageLiveDataSourceService {
constructor(
private messageLiveSignalRDataSourceService: SignalRService
) {}
async sendMessage(data: msgObj) {
try {
const result = await this.messageLiveSignalRDataSourceService.sendMessage(data)
return ok(result)
} catch (e) {
return err(e)
}
}
}
@@ -0,0 +1,139 @@
import { Injectable } from '@angular/core';
import { Dexie, EntityTable, liveQuery } from 'Dexie';
import { err, ok } from 'neverthrow';
import { z } from 'zod';
import { from, Observable, Subject } from 'rxjs';
import { filter, switchMap } from 'rxjs/operators';
import { MessageInputDTO } from '../../dto/message/messageInputDtO';
const tableSchema = z.object({
id: z.any().optional(),
messageId: z.string().optional(),
roomId: z.string().uuid(),
senderId: z.number(),
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 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({
message: '++id, roomId, senderId, message, messageType, canEdit, oneShot, requireUnlock, messageId'
});
@Injectable({
providedIn: 'root'
})
export class MessageLocalDataSourceService {
messageSubject = new Subject();
constructor() {
messageDataSource.message.hook('creating', (primKey, obj, trans) => {
// const newMessage = await trans.table('message').get(primKey);
this.messageSubject.next(obj);
// return newMessage
})
}
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)
}
}
async createMessage(data: MessageInputDTO) {
try {
const result = await messageDataSource.message.add(data)
return ok(result as string)
} catch (e) {
return err(false)
}
}
async update(data: TableMessage ) {
try {
const result = await messageDataSource.message.update(data.id, data)
return ok(result)
} catch (e) {
return err(false)
}
}
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)
}
}
getItemsLive(roomId: string) {
return liveQuery(() =>
messageDataSource.message.where('roomId').equals(roomId).sortBy('id')
)
}
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')
}
}
subscribeToNewMessage(roomId: string): Observable<TableMessage> {
return this.messageSubject.pipe(
filter((message: TableMessage) =>
message.roomId === roomId
)
)
}
}
@@ -0,0 +1,33 @@
import { Injectable } from '@angular/core';
import { HttpService } from 'src/app/services/http.service';
import { MessageInputDTO, MessageInputDTOSchema } from '../../dto/message/messageInputDtO';
import { ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
import { APIReturn } from 'src/app/services/decorators/api-validate-schema.decorator';
import { MessageOutPutDTO, MessageOutPutDTOSchema } from '../../dto/message/messageOutputDTO';
import { MessageListOutput } from '../../dto/message/messageListOutputDTO';
import { DataSourceReturn } from 'src/app/services/Repositorys/type';
@Injectable({
providedIn: 'root'
})
export class MessageRemoteDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor(private httpService: HttpService) {}
@APIReturn(MessageOutPutDTOSchema, 'get/Messages')
@ValidateSchema(MessageInputDTOSchema)
async sendMessage(data: MessageInputDTO) {
return await this.httpService.post<MessageOutPutDTO>(`${this.baseUrl}/Messages`, data);
}
async reactToMessage(id: string, reaction: any) {
return await this.httpService.post<any>(`${this.baseUrl}/Messages/${id}/React`, reaction);
}
async getMessagesFromRoom(id: string): DataSourceReturn<MessageListOutput> {
return await this.httpService.get(`${this.baseUrl}/Room/${id}/Messages`);
}
}