mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
direct message
This commit is contained in:
@@ -21,7 +21,7 @@ export class RoomSocketRepositoryService {
|
||||
|
||||
listenToCreateRoom() {
|
||||
return this.socket.getData().pipe(
|
||||
filter((data) => data.method == 'UserAddGroup')
|
||||
filter((data) => data?.method == 'UserAddGroup')
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,24 +41,24 @@ export const MessageEntitySchema = z.object({
|
||||
|
||||
type Message = z.infer<typeof MessageEntitySchema>;
|
||||
|
||||
export class MessageEntity implements Message {
|
||||
export class MessageEntity {
|
||||
|
||||
$id: number
|
||||
id: string
|
||||
$id?: number
|
||||
id?: string
|
||||
roomId?: string
|
||||
receiverId?: number
|
||||
message: string
|
||||
message?: string
|
||||
messageType: number = 0
|
||||
canEdit: boolean = false
|
||||
oneShot: boolean = false
|
||||
sentAt: string
|
||||
sentAt?: string
|
||||
requireUnlock: boolean = false
|
||||
info: {
|
||||
memberId?: number
|
||||
readAt?: string,
|
||||
deliverAt?: string
|
||||
}[] = []
|
||||
sender: {
|
||||
sender!: {
|
||||
wxUserId: number,
|
||||
wxFullName: string,
|
||||
wxeMail: string,
|
||||
@@ -82,7 +82,7 @@ export class MessageEntity implements Message {
|
||||
|
||||
reactions = []
|
||||
|
||||
requestId: string
|
||||
requestId!: string
|
||||
|
||||
constructor() {}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
|
||||
import { err, ok } from 'neverthrow';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
import { MessageLocalDataSourceService } from '../../data/repository/message/message-local-data-source.service';
|
||||
import { MessageSocketRepositoryService } from '../../data/repository/message/message-live-signalr-data-source.service';
|
||||
import { MessageRemoteDataSourceService } from '../../data/repository/message/message-remote-data-source.service';
|
||||
import { SignalRService } from '../../infra/socket/signal-r.service';
|
||||
|
||||
@@ -11,7 +12,7 @@ import { SignalRService } from '../../infra/socket/signal-r.service';
|
||||
export class MessageReadAtByIdUseCaseService {
|
||||
|
||||
constructor(
|
||||
private messageRemoteDataSourceService: MessageRemoteDataSourceService,
|
||||
private MessageSocketRepositoryService: MessageSocketRepositoryService,
|
||||
private messageLiveSignalRDataSourceService: SignalRService,
|
||||
private messageLocalDataSourceService: MessageLocalDataSourceService,
|
||||
) { }
|
||||
@@ -21,7 +22,7 @@ export class MessageReadAtByIdUseCaseService {
|
||||
if(result.isOk()) {
|
||||
if(result.value) {
|
||||
|
||||
return await this.messageLiveSignalRDataSourceService.sendReadAt({roomId, memberId: SessionStore.user.UserId, chatMessageId: result.value.id})
|
||||
return await this.MessageSocketRepositoryService.sendReadAt({roomId, memberId: SessionStore.user.UserId, messageId: result.value.id, requestId: ''})
|
||||
}
|
||||
return ok(true)
|
||||
}
|
||||
|
||||
@@ -12,5 +12,8 @@ export class SocketJoinUseCaseService {
|
||||
) { }
|
||||
|
||||
|
||||
execute() {}
|
||||
execute() {
|
||||
|
||||
this.MessageSocketRepositoryService.sendDirectMessage
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable, Subject, timer } from 'rxjs';
|
||||
import { Platform } from '@ionic/angular';
|
||||
import { SignalRConnection } from './signalR';
|
||||
import { SignalRConnection, SocketMessage } from './signalR';
|
||||
import { Plugins } from '@capacitor/core';
|
||||
import { UserTypingDTO } from '../../data/dto/typing/typingInputDTO';
|
||||
import { MessageOutPutDataDTO } from '../../data/dto/message/messageOutputDTO';
|
||||
import { MessageDeleteInputDTO } from '../../data/dto/message/messageDeleteInputDTO';
|
||||
import { z } from 'zod';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { filter, map, switchMap } from 'rxjs/operators';
|
||||
import { Result } from 'neverthrow';
|
||||
import { HubConnection } from '@microsoft/signalr';
|
||||
|
||||
@@ -28,11 +28,7 @@ export type ISignalRInput = z.infer<typeof SignalRInputSchema>;
|
||||
})
|
||||
export class SignalRService {
|
||||
private connection: SignalRConnection;
|
||||
private messageSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
||||
private typingSubject: BehaviorSubject<UserTypingDTO> = new BehaviorSubject<UserTypingDTO>(null);
|
||||
private connectingSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
|
||||
private messageDelete: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
||||
private messageUpdateSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
||||
private sendDataSubject: BehaviorSubject<{method: string, data: any}> = new BehaviorSubject<{method: string, data: any}>(null);
|
||||
|
||||
private deadConnectionBackGround: Subject<any>;
|
||||
@@ -80,24 +76,6 @@ export class SignalRService {
|
||||
this.connection?.closeConnection()
|
||||
this.connection = connection
|
||||
|
||||
this.connection.getSendLater().subscribe(data => {
|
||||
|
||||
})
|
||||
|
||||
this.connection.getMessages().subscribe((data) => {
|
||||
this.messageSubject.next(data)
|
||||
})
|
||||
this.connection.getTyping().subscribe((data) => {
|
||||
this.typingSubject.next(data)
|
||||
})
|
||||
|
||||
this.connection.getMessageDelete().subscribe((data) => {
|
||||
this.messageDelete.next(data)
|
||||
})
|
||||
|
||||
this.connection.getMessageUpdateSubject().subscribe((data) => {
|
||||
this.messageUpdateSubject.next(data)
|
||||
})
|
||||
|
||||
this.connection.getData().subscribe((data) => {
|
||||
this.sendDataSubject.next(data)
|
||||
@@ -123,20 +101,36 @@ export class SignalRService {
|
||||
|
||||
|
||||
getMessage() {
|
||||
return this.messageSubject.asObservable()
|
||||
return this.getData().pipe(
|
||||
filter((e) : e is SocketMessage<MessageOutPutDataDTO>=> e?.method == 'ReceiveMessage'
|
||||
),
|
||||
map((e)=> e.data)
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
getTyping() {
|
||||
return this.typingSubject.asObservable().pipe()
|
||||
return this.getData().pipe(
|
||||
filter((e) : e is SocketMessage<UserTypingDTO>=> e?.method == 'TypingMessage'
|
||||
),
|
||||
map((e)=> e.data)
|
||||
)
|
||||
}
|
||||
|
||||
getMessageDelete() {
|
||||
return this.messageDelete.asObservable()
|
||||
return this.getData().pipe(
|
||||
filter((e) : e is SocketMessage<MessageOutPutDataDTO>=> e?.method == 'DeleteMessage'
|
||||
),
|
||||
map((e)=> e.data)
|
||||
)
|
||||
}
|
||||
|
||||
getMessageUpdate() {
|
||||
return this.messageUpdateSubject.asObservable()
|
||||
return this.getData().pipe(
|
||||
filter((e) : e is SocketMessage<MessageOutPutDataDTO>=> e?.method == 'UpdateMessage'
|
||||
),
|
||||
map((e)=> e.data)
|
||||
)
|
||||
}
|
||||
|
||||
sendData<T>(input: ISignalRInput) {
|
||||
@@ -159,9 +153,6 @@ export class SignalRService {
|
||||
}
|
||||
|
||||
|
||||
async sendReadAt({ roomId, memberId, chatMessageId}) {
|
||||
return await this.connection.sendReadAt({ roomId, memberId, chatMessageId})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -20,10 +20,6 @@ export class SignalRConnection {
|
||||
|
||||
private hubConnection: signalR.HubConnection;
|
||||
private messageSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
||||
private messageDelete: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
||||
private messageUPdateSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
||||
private typingSubject: BehaviorSubject<UserTypingDTO> = new BehaviorSubject<UserTypingDTO>(null);
|
||||
private readAtSubject: BehaviorSubject<string> = new BehaviorSubject<any>(null);
|
||||
private connectionStateSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
|
||||
private disconnectSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
|
||||
private reconnectSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
|
||||
@@ -111,36 +107,6 @@ export class SignalRConnection {
|
||||
}
|
||||
|
||||
|
||||
public async sendReadAt(data: Object & { roomId, memberId, chatMessageId}):Promise<Result<any, any>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
const requestId = uuidv4()
|
||||
if(this.connectionStateSubject.value == true) {
|
||||
|
||||
try {
|
||||
this.hubConnection.invoke("ReadAt", { roomId: data.roomId, memberId: data.memberId, requestId, messageId: data.chatMessageId} as any)
|
||||
|
||||
} catch (error) {}
|
||||
|
||||
this.readAtSubject.pipe(
|
||||
filter((message: any) => {
|
||||
return requestId == message?.requestId
|
||||
}),
|
||||
first()
|
||||
).subscribe(value => {
|
||||
resolve(ok(value));
|
||||
});
|
||||
|
||||
} else {
|
||||
this.sendLaterSubject.next({method: 'SendMessage', args: data})
|
||||
return reject(err(false))
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
sendData<T>(input: ISignalRInput): Promise<Result<T, any>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
@@ -165,88 +131,26 @@ export class SignalRConnection {
|
||||
}
|
||||
|
||||
private addMessageListener(): void {
|
||||
console.log('listening')
|
||||
this.hubConnection.on('ReceiveMessage', (message: MessageOutPutDataDTO) => {
|
||||
console.log('ReceiveMessage', message)
|
||||
this.messageSubject.next(message);
|
||||
this.sendDataSubject.next({
|
||||
method: 'ReceiveMessage',
|
||||
data: message
|
||||
})
|
||||
});
|
||||
|
||||
const methods = ['ReceiveMessage', 'TypingMessage', 'AvailableUsers',
|
||||
'ReadAt', 'DeleteMessage', 'UpdateMessage', 'GroupAddedMembers',
|
||||
'GroupDeletedMembers']
|
||||
|
||||
this.hubConnection.on('TypingMessage', (_typing: UserTypingDTO) => {
|
||||
this.typingSubject.next(_typing);
|
||||
this.sendDataSubject.next({
|
||||
method: 'ReceiveMessage',
|
||||
data: _typing
|
||||
})
|
||||
});
|
||||
|
||||
this.hubConnection.on('AvailableUsers', (data: any) => {
|
||||
this.typingSubject.next(data);
|
||||
this.sendDataSubject.next({
|
||||
method: 'AvailableUsers',
|
||||
data: data
|
||||
})
|
||||
});
|
||||
|
||||
this.hubConnection.on('ReadAt', (_message) => {
|
||||
console.log('ReadAt', _message)
|
||||
this.readAtSubject.next(_message);
|
||||
this.sendDataSubject.next({
|
||||
method: 'ReceiveMessage',
|
||||
data: _message
|
||||
})
|
||||
});
|
||||
|
||||
this.hubConnection.on('DeleteMessage', (_message) => {
|
||||
console.log('DeleteMessage', _message)
|
||||
this.messageDelete.next(_message);
|
||||
this.sendDataSubject.next({
|
||||
method: 'DeleteMessage',
|
||||
data: _message
|
||||
})
|
||||
});
|
||||
|
||||
this.hubConnection.on('UpdateMessage', (_message) => {
|
||||
console.log('UpdateMessage', _message)
|
||||
this.messageUPdateSubject.next(_message);
|
||||
this.sendDataSubject.next({
|
||||
method: 'ReceiveMessage',
|
||||
data: _message
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
this.hubConnection.on('GroupAddedMembers', (_message) => {
|
||||
console.log('GroupAddedMembers', _message)
|
||||
this.sendDataSubject.next({
|
||||
method: 'GroupAddedMembers',
|
||||
data: _message
|
||||
})
|
||||
})
|
||||
|
||||
this.hubConnection.on('GroupDeletedMembers', (_message) => {
|
||||
console.log('GroupDeletedMembers', _message)
|
||||
this.sendDataSubject.next({
|
||||
method: 'GroupDeletedMembers',
|
||||
data: _message
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
public getMessageUpdateSubject() {
|
||||
return this.messageUPdateSubject.asObservable()
|
||||
for(const method of methods) {
|
||||
this.hubConnection.on(method, (message: MessageOutPutDataDTO) => {
|
||||
this.messageSubject.next(message);
|
||||
this.sendDataSubject.next({
|
||||
method: method,
|
||||
data: message
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public getMessages() {
|
||||
return this.messageSubject.asObservable()
|
||||
}
|
||||
|
||||
public getTyping() {
|
||||
return this.typingSubject.asObservable()
|
||||
}
|
||||
|
||||
public getConnectionState(): Observable<boolean> {
|
||||
return this.connectionStateSubject.asObservable();
|
||||
@@ -256,13 +160,7 @@ export class SignalRConnection {
|
||||
return this.disconnectSubject.asObservable();
|
||||
}
|
||||
|
||||
public getSendLater() {
|
||||
return this.sendLaterSubject.asObservable();
|
||||
}
|
||||
|
||||
public getMessageDelete() {
|
||||
return this.messageDelete.asObservable()
|
||||
}
|
||||
|
||||
public getData() {
|
||||
return this.sendDataSubject.asObservable()
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "ES2020",
|
||||
"module": "CommonJS"
|
||||
},
|
||||
"include": [
|
||||
"./**/*.ts" // Include all TypeScript files in the current directory and subdirectories
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user