mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
send and receive message
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -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`);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { WebSocketMessage, WebSocketService } from '../../../infra/socket/socket';
|
||||
import { err, ok } from 'neverthrow';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomLiveDataSourceService {
|
||||
|
||||
constructor(private socket: WebSocketService) {}
|
||||
|
||||
async getRoomById(data: WebSocketMessage) {
|
||||
|
||||
try {
|
||||
|
||||
const result = await this.socket.sendMessage(data).toPromise()
|
||||
|
||||
console.log({result})
|
||||
|
||||
return ok(result)
|
||||
} catch (e) {
|
||||
return err(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { createAction, createFeatureSelector, createReducer, createSelector, on, props } from "@ngrx/store";
|
||||
import { TableRoom } from "./rooom-local-data-source.service";
|
||||
import { RoomOutPutDTO } from "../../dto/room/roomOutputDTO";
|
||||
|
||||
|
||||
export interface ChatRoom {
|
||||
[roomId: string]: TableRoom[];
|
||||
}
|
||||
|
||||
export interface RoomRemoteDataSourceState {
|
||||
chatRooms: ChatRoom;
|
||||
}
|
||||
|
||||
export const initialState: RoomRemoteDataSourceState = {
|
||||
chatRooms: {
|
||||
// add more rooms as needed
|
||||
}
|
||||
};
|
||||
|
||||
export const addMessage = createAction(
|
||||
'[Chat] Add Message',
|
||||
props<{ roomId: string; message: any }>()
|
||||
);
|
||||
|
||||
export const addRoom = createAction(
|
||||
'[Chat] Add Room',
|
||||
props<RoomOutPutDTO>()
|
||||
);
|
||||
|
||||
const _chatReducer = createReducer(
|
||||
initialState,
|
||||
on(addMessage, (state, { roomId, message }) => ({
|
||||
...state,
|
||||
chatRooms: {
|
||||
...state.chatRooms,
|
||||
[roomId]: [...(state.chatRooms[roomId] || []), message]
|
||||
}
|
||||
})),
|
||||
on(addRoom, (state, roomData: RoomOutPutDTO) => ({
|
||||
...state,
|
||||
chatRooms: {
|
||||
...state.chatRooms,
|
||||
[roomData.data.roomName]: roomData.data
|
||||
}
|
||||
}))
|
||||
);
|
||||
|
||||
export function chatReducer(state, action) {
|
||||
return _chatReducer(state, action);
|
||||
}
|
||||
|
||||
// Create a feature selector
|
||||
export const selectChatState = createFeatureSelector<RoomRemoteDataSourceState>('chat');
|
||||
|
||||
// Create a selector for a specific room's messages
|
||||
export const selectMessagesByRoom = (roomId: string) => createSelector(
|
||||
selectChatState,
|
||||
(state: RoomRemoteDataSourceState) => state.chatRooms[roomId] || []
|
||||
);
|
||||
|
||||
// Create a selector for the list of rooms
|
||||
export const selectAllRooms = createSelector(
|
||||
selectChatState,
|
||||
(state: RoomRemoteDataSourceState) => Object.keys(state.chatRooms)
|
||||
);
|
||||
@@ -0,0 +1,68 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Result } from 'neverthrow';
|
||||
import { HttpService } from 'src/app/services/http.service';
|
||||
import { RoomListOutPutDTO, RoomListOutPutDTOSchema } from '../../dto/room/roomListOutputDTO';
|
||||
import { RoomInputDTO, RoomInputDTOSchema } from '../../dto/room/roomInputDTO';
|
||||
import { RoomOutPutDTO, RoomOutPutDTOSchema } from '../../dto/room/roomOutputDTO';
|
||||
import { AddMemberToRoomInputDTO, AddMemberToRoomInputDTOSchema } from '../../dto/room/addMemberToRoomInputDto';
|
||||
import { ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
|
||||
import { RoomByIdInputDTO, RoomByIdInputDTOSchema } from '../../dto/room/roomByIdInputDTO';
|
||||
import { RoomByIdOutputDTO, RoomByIdOutputDTOSchema } from '../../dto/room/roomByIdOutputDTO';
|
||||
import { APIReturn } from 'src/app/services/decorators/api-validate-schema.decorator';
|
||||
import { UserRemoveListInputDTO, UserRemoveListInputDTOSchema } from '../../dto/room/userRemoveListInputDTO';
|
||||
import { RoomUpdateInputDTO, RoomUpdateInputDTOSchema } from '../../dto/room/roomUpdateInputDTO';
|
||||
import { RoomUpdateOutputDTO } from '../../dto/room/roomUpdateOutputDTO';
|
||||
import { DataSourceReturn } from 'src/app/services/Repositorys/type';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomRemoteDataSourceService {
|
||||
|
||||
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
|
||||
|
||||
constructor(private httpService: HttpService) {}
|
||||
|
||||
|
||||
@ValidateSchema(RoomInputDTOSchema)
|
||||
@APIReturn(RoomOutPutDTOSchema, 'post/Room')
|
||||
async createRoom(data: RoomInputDTO): DataSourceReturn<RoomOutPutDTO> {
|
||||
return await this.httpService.post<RoomOutPutDTO>(`${this.baseUrl}/Room`, data);
|
||||
}
|
||||
|
||||
|
||||
@APIReturn(RoomListOutPutDTOSchema, 'get/Room')
|
||||
async getRoomList(): Promise<DataSourceReturn<RoomListOutPutDTO>> {
|
||||
return await this.httpService.get<RoomListOutPutDTO>(`${this.baseUrl}/Room`);
|
||||
}
|
||||
|
||||
@ValidateSchema(RoomByIdInputDTOSchema)
|
||||
@APIReturn(RoomByIdOutputDTOSchema,'get/Room/${id}')
|
||||
async getRoom(id: RoomByIdInputDTO): DataSourceReturn<RoomByIdOutputDTO> {
|
||||
return await this.httpService.get(`${this.baseUrl}/Room/${id}`);
|
||||
}
|
||||
|
||||
@ValidateSchema(RoomUpdateInputDTOSchema)
|
||||
//@APIReturn(RoomByIdOutputDTOSchema,'update/Room/${id}')
|
||||
async updateRoom(data: RoomUpdateInputDTO): Promise<DataSourceReturn<RoomUpdateOutputDTO>> {
|
||||
const id = data.roomId
|
||||
delete data.roomId
|
||||
return await this.httpService.put<any>(`${this.baseUrl}/Room/${id}`, data);
|
||||
}
|
||||
|
||||
async deleteRoom(id: string): Promise<Result<any ,any>> {
|
||||
return await this.httpService.delete<any>(`${this.baseUrl}/Room/${id}`, {});
|
||||
}
|
||||
|
||||
@ValidateSchema(AddMemberToRoomInputDTOSchema)
|
||||
async addMemberToRoom(data: AddMemberToRoomInputDTO): DataSourceReturn<AddMemberToRoomInputDTO> {
|
||||
return await this.httpService.post<any>(`${this.baseUrl}/Room/${data.id}/Member`, { members:data.members });
|
||||
}
|
||||
|
||||
|
||||
@ValidateSchema(UserRemoveListInputDTOSchema)
|
||||
async removeMemberFromRoom(data: UserRemoveListInputDTO): Promise<Result<any ,any>> {
|
||||
return await this.httpService.delete<any>(`${this.baseUrl}/Room/${data.id}/Member`, {members:data.members});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RoomListItemOutPutDTO, RoomListOutPutDTO } from '../../dto/room/roomListOutputDTO';
|
||||
import { Dexie, EntityTable, liveQuery, Observable } from 'Dexie';
|
||||
import { err, ok, Result } from 'neverthrow';
|
||||
import { z } from 'zod';
|
||||
import { ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
|
||||
|
||||
const tableSchema = z.object({
|
||||
id: z.string(),
|
||||
roomName: z.string(),
|
||||
createdBy: z.object({
|
||||
wxUserId: z.number(),
|
||||
wxFullName: z.string(),
|
||||
wxeMail: z.string().email(),
|
||||
userPhoto: z.string().nullable().optional()// api check
|
||||
}),
|
||||
createdAt: z.any(),
|
||||
expirationDate: z.any(),
|
||||
roomType: z.any(),
|
||||
// lastMessage: z.object({
|
||||
// sentAt: z.string(),
|
||||
// message: z.string(),
|
||||
// sender: z.object({
|
||||
// wxUserId: z.any(),
|
||||
// wxFullName: z.any(),
|
||||
// }).optional(),s
|
||||
// })
|
||||
})
|
||||
|
||||
const TableMemberListSchema = z.object({
|
||||
$roomIdUserId: z.string().optional(),
|
||||
id: z.string(),
|
||||
roomId: z.string(),
|
||||
user: z.object({
|
||||
wxUserId: z.number(),
|
||||
wxFullName: z.string(),
|
||||
wxeMail: z.string(),
|
||||
userPhoto: z.string().nullable(),
|
||||
}),
|
||||
joinAt: z.string()
|
||||
})
|
||||
|
||||
export type TableRoom = z.infer<typeof tableSchema>
|
||||
export type TableMemberList = z.infer<typeof TableMemberListSchema>
|
||||
|
||||
// Database declaration (move this to its own module also)
|
||||
export const roomDataSource = new Dexie('FriendDatabase') as Dexie & {
|
||||
room: EntityTable<TableRoom, 'id'>;
|
||||
memberList: EntityTable<TableMemberList, '$roomIdUserId'>;
|
||||
};
|
||||
|
||||
roomDataSource.version(1).stores({
|
||||
room: 'id, createdBy, roomName, roomType, expirationDate, lastMessage',
|
||||
memberList: '$roomIdUserId, id, user, joinAt, roomId',
|
||||
});
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomLocalDataSourceService {
|
||||
|
||||
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
|
||||
|
||||
constructor() {}
|
||||
|
||||
@ValidateSchema(tableSchema)
|
||||
async createRoom(data: TableRoom) {
|
||||
try {
|
||||
const result = await roomDataSource.room.add(data)
|
||||
return ok(result)
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async deleteRoomById(id: string) {
|
||||
try {
|
||||
const result = await roomDataSource.room.delete(id)
|
||||
return ok(result)
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async leaveRoom(id: string) {
|
||||
return this.deleteRoomById(id)
|
||||
}
|
||||
|
||||
async updateRoom(data: TableRoom) {
|
||||
try {
|
||||
const result = await roomDataSource.room.update(data.id, data);
|
||||
|
||||
return ok(result)
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
}
|
||||
}
|
||||
|
||||
async createOrUpdateRoom(data: TableRoom) {
|
||||
const createResult = await this.createRoom(data)
|
||||
|
||||
if(createResult.isOk()) {
|
||||
return createResult
|
||||
} else {
|
||||
return this.updateRoom(data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async addMember(data: TableMemberList) {
|
||||
try {
|
||||
data.$roomIdUserId = data.roomId + data.user.wxUserId
|
||||
const result = await roomDataSource.memberList.add(data)
|
||||
return ok(result)
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async removeMemberFromRoom($roomIdUserId): Promise<Result<any ,any>> {
|
||||
try {
|
||||
|
||||
const member = await roomDataSource.memberList.where({ $roomIdUserId: $roomIdUserId }).first();
|
||||
|
||||
if (member) {
|
||||
const result = await ok(roomDataSource.memberList.delete($roomIdUserId));
|
||||
console.log(`Member with $roomIdUserId ${$roomIdUserId} removed from room ${member.roomId}.`);
|
||||
return result
|
||||
} else {
|
||||
console.log(`No member found with $roomIdUserId ${$roomIdUserId}`);
|
||||
return err('not Found')
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
}
|
||||
}
|
||||
|
||||
async getRoomById(id: any) {
|
||||
try {
|
||||
const result = await roomDataSource.room.get(id)
|
||||
return ok(result)
|
||||
} catch (e) {
|
||||
return err(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getRoomList() {
|
||||
return await roomDataSource.room.toArray()
|
||||
}
|
||||
|
||||
|
||||
getItemsLive(): Observable<RoomListOutPutDTO[]> {
|
||||
return liveQuery(() => roomDataSource.room.toArray()) as any;
|
||||
}
|
||||
|
||||
getRoomByIdLive(id: any): Observable<RoomListItemOutPutDTO | undefined> {
|
||||
return liveQuery(() => roomDataSource.room.get(id)) as any;
|
||||
}
|
||||
|
||||
async getRoomMemberById(roomId: any) {
|
||||
return await roomDataSource.memberList.where('roomId').equals(roomId).toArray()
|
||||
}
|
||||
getRoomMemberByIdLive(roomId: any) {
|
||||
return liveQuery(() => roomDataSource.memberList.where('roomId').equals(roomId).toArray())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user