add readAt functionality

This commit is contained in:
Peter Maquiran
2024-07-18 16:19:30 +01:00
parent cf6fe3a4c8
commit cd1c61fe86
11 changed files with 120 additions and 33 deletions
@@ -3,17 +3,21 @@ import { BehaviorSubject } from 'rxjs';
import { Platform } from '@ionic/angular';
import { SignalRConnection } from './signalR';
import { Plugins } from '@capacitor/core';
import { z } from 'zod';
import { UserTypingDTO } from '../../data/dto/typing/typingInputDTO';
import { MessageOutPutDataDTO } from '../../data/dto/message/messageOutputDTO';
const { App } = Plugins;
@Injectable({
providedIn: 'root'
})
export class SignalRService {
private connection: SignalRConnection;
private messageSubject: BehaviorSubject<string> = new BehaviorSubject<any>(null);
private typingSubject: BehaviorSubject<string> = new BehaviorSubject<any>(null);
private messageSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
private typingSubject: BehaviorSubject<UserTypingDTO> = new BehaviorSubject<UserTypingDTO>(null);
private connectingSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
constructor(
@@ -73,7 +77,7 @@ export class SignalRService {
}
getTyping() {
return this.typingSubject.asObservable()
return this.typingSubject.asObservable().pipe()
}
async sendMessage(data: Object) {
@@ -84,7 +88,11 @@ export class SignalRService {
this.establishConnection()
}
async sendTyping({ChatRoomId, UserName}) {
return await this.connection.typing({ ChatRoomId, UserName})
async sendTyping({roomId, UserName}) {
return await this.connection.typing({ roomId, UserName})
}
async sendReadAt({ roomId, memberId, chatMessageId}) {
return await this.connection.sendReadAt({ roomId, memberId, chatMessageId})
}
}
+55 -13
View File
@@ -4,11 +4,15 @@ import { ok, Result, err } from 'neverthrow';
import { SessionStore } from 'src/app/store/session.service';
import { filter, first } from 'rxjs/operators';
import { v4 as uuidv4 } from 'uuid'
import { UserTypingDTO } from '../../data/dto/typing/typingInputDTO';
import { MessageOutPutDataDTO } from '../../data/dto/message/messageOutputDTO';
export class SignalRConnection {
private hubConnection: signalR.HubConnection;
private messageSubject: BehaviorSubject<string> = new BehaviorSubject<any>(null);
private typingSubject: BehaviorSubject<string> = new BehaviorSubject<any>(null);
private messageSubject: 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);
@@ -23,6 +27,7 @@ export class SignalRConnection {
establishConnection(): Promise<Result<signalR.HubConnection, false>> {
return new Promise((resolve, reject) => {
console.log('try to connect');
const hubConnection = new signalR.HubConnectionBuilder()
.withUrl(this.url)
.build();
@@ -35,8 +40,8 @@ export class SignalRConnection {
console.log('Connection started');
this.connectionStateSubject.next(true);
this.hubConnection = hubConnection
this.addMessageListener()
this.join()
this.addMessageListener()
resolve(ok(hubConnection))
})
.catch(error => {
@@ -69,8 +74,8 @@ export class SignalRConnection {
public join() {
if(this.connectionStateSubject.value == true) {
console.log('join=============')
console.log('join=================')
this.hubConnection.invoke("Join", SessionStore.user.UserId, SessionStore.user.FullName);
//this.hubConnection.invoke("Join", 105, "UserFirefox");
} else {
@@ -83,7 +88,7 @@ export class SignalRConnection {
return new Promise((resolve, reject) => {
if(this.connectionStateSubject.value == true) {
console.log('sendMessage')
console.log('sendMessage', data)
this.hubConnection.invoke("SendMessage", data)
this.messageSubject.pipe(
@@ -103,14 +108,14 @@ export class SignalRConnection {
})
}
public async typing(data: Object & { ChatRoomId, UserName}):Promise<Result<any, any>> {
public async typing(data: Object & { roomId, UserName}):Promise<Result<any, any>> {
return new Promise((resolve, reject) => {
const requestId = uuidv4()
if(this.connectionStateSubject.value == true) {
try {
this.hubConnection.invoke("Typing", {UserName: data.UserName, ChatRoomId: data.ChatRoomId, requestId} as any)
this.hubConnection.invoke("Typing", {userName: data.UserName, roomId: data.roomId, requestId} as any)
} catch (error) {}
@@ -132,23 +137,60 @@ 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 } 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))
}
})
}
private addMessageListener(): void {
this.hubConnection.on('ReceiveMessage', (message) => {
console.log('listening')
this.hubConnection.on('ReceiveMessage', (message: MessageOutPutDataDTO) => {
console.log('ReceiveMessage', message)
this.messageSubject.next(message);
});
this.hubConnection.on('Typing', (_message) => {
console.log('_message', _message)
this.typingSubject.next(_message);
this.hubConnection.on('Typing', (_typing: UserTypingDTO) => {
console.log('Typing', _typing)
this.typingSubject.next(_typing);
});
this.hubConnection.on('ReadAt', (_message) => {
console.log('ReadAt', _message)
this.readAtSubject.next(_message);
});
}
public getMessages(): Observable<string> {
public getMessages() {
return this.messageSubject.asObservable()
}
public getTyping(): Observable<string> {
public getTyping() {
return this.typingSubject.asObservable()
}