75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
import JsonSerializer from '@burguillosinfo/conquer/serializer';
|
|
import { JsonObject, JsonProperty } from 'typescript-json-serializer';
|
|
import ConquerTeam from '@burguillosinfo/conquer/team';
|
|
|
|
export interface UserData {
|
|
is_admin: number
|
|
kind: string
|
|
last_activity?: string
|
|
registration_date?: string
|
|
username: string
|
|
uuid: string
|
|
}
|
|
|
|
@JsonObject()
|
|
export default class ConquerUser {
|
|
@JsonProperty()
|
|
private is_admin: boolean;
|
|
@JsonProperty()
|
|
private kind: string;
|
|
@JsonProperty()
|
|
private last_activity: string | null;
|
|
@JsonProperty()
|
|
private registration_date: string | null;
|
|
@JsonProperty()
|
|
private username: string;
|
|
@JsonProperty()
|
|
private uuid: string;
|
|
@JsonProperty()
|
|
private team: string | null;
|
|
|
|
constructor(kind: string, uuid: string, username: string, is_admin = false, registration_date: string | null = null, last_activity: string | null = null) {
|
|
this.kind = kind;
|
|
this.uuid = uuid;
|
|
this.username = username;
|
|
this.is_admin = is_admin;
|
|
this.registration_date = registration_date;
|
|
this.last_activity = last_activity;
|
|
}
|
|
|
|
public async getTeam(): Promise<ConquerTeam | null> {
|
|
if (this.team === null) {
|
|
return null;
|
|
}
|
|
return ConquerTeam.getTeam(this.team);
|
|
}
|
|
|
|
public static async getSelfUser(): Promise<ConquerUser | null> {
|
|
const urlUser = new URL('/conquer/user', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port)
|
|
try {
|
|
const response = await fetch(urlUser)
|
|
if (response.status !== 200) {
|
|
throw new Error('Invalid response fetching user.')
|
|
}
|
|
const userData = await response.json()
|
|
const user = JsonSerializer.deserialize(userData, ConquerUser);
|
|
if (!(user instanceof ConquerUser)) {
|
|
throw new Error('Unable to parse user.');
|
|
}
|
|
return user;
|
|
} catch (error) {
|
|
console.error(error)
|
|
return null
|
|
}
|
|
}
|
|
public getUsername(): string {
|
|
if (this.username === null) {
|
|
throw new Error('User username cannot be null.')
|
|
}
|
|
return this.username
|
|
}
|
|
public isAdmin(): boolean {
|
|
return this.is_admin
|
|
}
|
|
}
|