Implemented SessionManager and TextChannelService + several improvements and fixes
All checks were successful
Setup testing environment and test the code / build (push) Successful in 1m13s
All checks were successful
Setup testing environment and test the code / build (push) Successful in 1m13s
This commit is contained in:
133
src/services/sessionManager.ts
Normal file
133
src/services/sessionManager.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
import {PublicUserData} from "../domain/common.schema";
|
||||
import {DatabaseAPI} from "../storage/database";
|
||||
import {KeyringAPI} from "../storage/keyring";
|
||||
import {KeyValueAPI} from "../storage/keyvalue";
|
||||
import {Session, ValidateSessionReq, ValidateSessionResp} from "../domain/sessionManager.schema";
|
||||
import {AxiosInstance} from "axios";
|
||||
import {getClient} from "../core/http";
|
||||
import {PersonalUserData} from "../domain/userService.schema";
|
||||
|
||||
export class SessionManager {
|
||||
client: AxiosInstance;
|
||||
database: DatabaseAPI;
|
||||
keyring: KeyringAPI;
|
||||
KeyValue: KeyValueAPI;
|
||||
|
||||
constructor(database: DatabaseAPI, keyring: KeyringAPI, KeyValue: KeyValueAPI) {
|
||||
this.database = database;
|
||||
this.keyring = keyring;
|
||||
this.KeyValue = KeyValue;
|
||||
this.client = getClient(false)
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the new session to the database and the keyring
|
||||
* @param userData
|
||||
* @param token
|
||||
*/
|
||||
addSession(userData: PublicUserData, token: string): void {
|
||||
this.database.set("sessions", userData.userid, JSON.stringify(userData))
|
||||
this.keyring.set(userData.userid, token)
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all saved sessions
|
||||
*/
|
||||
loadSessions(): Session[] {
|
||||
const tokens = this.keyring.getAll()
|
||||
const sessions: Session[] = []
|
||||
tokens.forEach(token => {
|
||||
const userData = this.database.get("sessions", token.split(".")[0])
|
||||
if (userData) {
|
||||
sessions.push({
|
||||
token: token,
|
||||
userData: JSON.parse(userData)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return sessions
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the preferred user set by the client
|
||||
*/
|
||||
getPreferredUser(): string {
|
||||
return this.KeyValue.get("preferredUser") ?? ""
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new preferred user
|
||||
* @param userid
|
||||
*/
|
||||
setPreferredUser(userid: string): void {
|
||||
this.KeyValue.set("preferredUser", userid)
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the preferred session by the client
|
||||
*/
|
||||
loadPreferredSession() {
|
||||
const sessions = this.loadSessions()
|
||||
let preferredUser = this.getPreferredUser()
|
||||
if (preferredUser == "") {
|
||||
preferredUser = sessions[0].userData.userid
|
||||
this.setPreferredUser(sessions[0].userData.userid)
|
||||
}
|
||||
|
||||
const preferredSession = sessions.find(s => s.userData.userid == preferredUser)
|
||||
if (preferredSession) {
|
||||
return preferredSession
|
||||
} else {
|
||||
return sessions[0]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and updates all sessions and returns with a new session list
|
||||
* @param sessions
|
||||
*/
|
||||
updateSessions(sessions: Session[]): Session[] {
|
||||
sessions.forEach(async session => {
|
||||
const index = sessions.indexOf(session)
|
||||
if (!await this.validateSession(session.token)) {
|
||||
this.database.delete("sessions", session.userData.userid)
|
||||
this.keyring.delete(session.userData.userid)
|
||||
sessions.splice(index, 1)
|
||||
}
|
||||
|
||||
const updatedUserData = await this.updateUserData(session)
|
||||
this.database.set("sessions", session.userData.userid, updatedUserData)
|
||||
sessions[index] = updatedUserData
|
||||
})
|
||||
|
||||
return sessions
|
||||
}
|
||||
|
||||
private async validateSession(token: string): Promise<boolean> {
|
||||
try {
|
||||
const resp = await this.client.post<ValidateSessionResp>("v2/user/validateSession", <ValidateSessionReq>{
|
||||
token: token,
|
||||
})
|
||||
return resp.data.validationOk
|
||||
} catch (e) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private async updateUserData(session: Session): Promise<Session> {
|
||||
const authenticatedClient = this.client.create({
|
||||
headers: {
|
||||
"Authorization": session.token,
|
||||
}
|
||||
})
|
||||
|
||||
try {
|
||||
const resp = await authenticatedClient.get<PersonalUserData>(`user/byUseridPersonal?userid=${session.userData.userid}`)
|
||||
session.userData = resp.data
|
||||
return session
|
||||
} catch (e) {
|
||||
throw new Error("Session update error")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user