Code quality improvements and cleanup + Implemented ChatService
Some checks failed
Setup testing environment and test the code / build (push) Has been cancelled

This commit is contained in:
2026-04-05 15:19:55 +02:00
parent ec418ca7c9
commit 4a34d73c2f
20 changed files with 380 additions and 39 deletions

116
src/services/chatService.ts Normal file
View File

@@ -0,0 +1,116 @@
import {DatabaseAPI} from "../storage/database";
import {MessageListener} from "../domain/websocket.schema";
import {getClient} from "../core/http";
import {WebSocketHandler} from "../core/webSocketHandler";
import {AxiosInstance, isAxiosError} from "axios";
import {NetworkInvite} from "../domain/networkService.schema";
import {GenericErrorBody} from "../domain/http.schema";
import {
Chat,
GetAvailabilityReq,
GetAvailabilityResp,
StartNewReq,
ToggleChatMuteReq
} from "../domain/chatService.schema";
/**
* ChatService is an exception because it's one instance for all chats because it's unnecessary to create a new instance for each chat
*/
export class ChatService {
userid: string;
database: DatabaseAPI;
client: AxiosInstance;
constructor(userid: string, token: string, database: DatabaseAPI, wsMessageListener: MessageListener) {
this.userid = userid;
this.database = database;
this.client = getClient(false).create({
headers: {
"Authorization": token,
}
})
WebSocketHandler.getInstance().registerService({
identifier: userid,
onNewConnId: this.onNewConnId,
onNewMessage: wsMessageListener,
})
}
private onNewConnId(newConnId: string) {
console.log("ChatService: New connection id")
this.client.defaults.headers["X-WS-ID"] = newConnId;
}
/**
* Fetches all chat partners
*/
async get(): Promise<Chat[]> {
try {
const resp = await this.client.get<Chat[]>(`chat/get?userid=${this.userid}`);
return resp.data
} catch (e) {
console.log(e)
if (isAxiosError<GenericErrorBody>(e)) {
throw e;
}
throw new Error("Unexpected error")
}
}
/**
* Gets the availability of the specified user
* @param userid
*/
async getUserAvailability(userid: string): Promise<boolean> {
try {
const resp = await this.client.get<GetAvailabilityResp>(`chat/availability?target=${this.userid}&connId=${WebSocketHandler.getInstance().connId}`);
return resp.data.available
} catch (e) {
console.log(e)
if (isAxiosError<GenericErrorBody>(e)) {
throw e;
}
throw new Error("Unexpected error")
}
}
/**
* (un)mutes the specified chat
* @param chatid
*/
async toggleMute(chatid: string): Promise<void> {
try {
const resp = await this.client.post("v2/chat/toggleMute", <ToggleChatMuteReq>{
userid: this.userid,
chatid: chatid,
});
return
} catch (e) {
console.log(e)
if (isAxiosError<GenericErrorBody>(e)) {
throw e;
}
throw new Error("Unexpected error")
}
}
/**
* Starts a new chat with the specified user
* @param peerUsername
*/
async startNew(peerUsername: string): Promise<Chat> {
try {
const resp = await this.client.post("chat/startNew", <StartNewReq>{
userid: this.userid,
peerUsername: peerUsername,
});
return resp.data
} catch (e) {
console.log(e)
if (isAxiosError<GenericErrorBody>(e)) {
throw e;
}
throw new Error("Unexpected error")
}
}
}