core: use postinit status code

This commit is contained in:
2024-01-08 20:57:43 +01:00
parent 6f9903e309
commit 476ed0b833
12 changed files with 87 additions and 89 deletions

View File

@ -2,6 +2,12 @@ import { Profile } from "./profile.js";
class MyProfile extends Profile
{
constructor (client)
{
super(client, "me")
}
async change_avatar(form_data)
{
let response = await this.client._patch_file(`/api/profiles/me`, form_data);
@ -10,10 +16,6 @@ class MyProfile extends Profile
return response_data;
}
async init()
{
super.init("me");
}
}
export {MyProfile}

View File

@ -21,8 +21,7 @@ class Channels {
});
let data = await response.json();
let exit_code = await response.status;
if (exit_code >= 300)
if (data.status >= 300)
return undefined;
let messages = undefined;

View File

@ -100,14 +100,17 @@ class Client
async _update_logged(state)
{
if (!this.logged && state)
if (this.logged == state)
return;
if (state)
{
this.me = new MyProfile(this);
await this.me.init();
}
if (this.logged && !state)
else
{
navigateTo("/login");
this.me = undefined;
}
this.logged = state;
}
@ -115,13 +118,13 @@ class Client
async login(username, password)
{
let response = await this._post("/api/accounts/login", {username: username, password: password})
let data = await response.json();
if (data.id != undefined)
{
await this._update_logged(true);
return null;
}
return data;
if (response.status != 200)
return response.status;
this._update_logged(true);
return 0;
}
async logout()
@ -133,11 +136,9 @@ class Client
async _test_logged()
{
let response = await this._get("/api/accounts/logged");
let data = await response.json();
if (data.id !== undefined)
await this._update_logged(true);
return data.id !== undefined;
await this._update_logged(response.status === 200);
return response.status === 200
}
}

View File

@ -5,43 +5,45 @@ class Profile
/**
* @param {Client} client
*/
constructor (client, username = undefined, avatar_url = undefined, user_id = undefined)
constructor (client, id, username = undefined, avatar_url = undefined)
{
/**
* @type {Client} client
*/
this.client = client;
this.id = id;
this.username = username;
this.avatar_url = avatar_url;
this.user_id = user_id;
this.isBlocked = false;
}
async init(user_id)
async init()
{
let response = await this.client._get(`/api/profiles/${user_id}`);
let response = await this.client._get(`/api/profiles/${this.id}`);
if (response.status === 404)
return 1;
if (response.status !== 200)
return response.status;
let response_data = await response.json();
this.user_id = response_data.user_id;
this.id = response_data.user_id;
this.username = response_data.username;
this.avatar_url = response_data.avatar_url;
if (this.client.me == undefined)
return;
let block_response = await this.client._get("/api/profiles/block");
if (block_response.status == 404)
return
return;
let block_data = await block_response.json();
let block_list = JSON.parse(block_data);
block_list.forEach(block => {
let blocker = block.fields.blocker;
let blocked = block.fields.blocked;
if (blocker == this.client.me.user_id && blocked == user_id)
return this.isBlocked = true;
let blocker = block.fields.blocker;
let blocked = block.fields.blocked;
if (blocker == this.client.me.user_id && blocked == user_id)
return this.isBlocked = true;
});
}
}

View File

@ -20,15 +20,15 @@ class Profiles
let profiles = []
response_data.forEach((profile) => {
profiles.push(new Profile(this.client, profile.username, profile.avatar_url, profile.user_id))
profiles.push(new Profile(this.client, profile.user_id, profile.username, profile.avatar_url))
});
return profiles;
}
async getProfile(user_id)
{
let profile = new Profile(this.client);
if (await profile.init(user_id))
let profile = new Profile(this.client, user_id);
if (await profile.init())
return null;
return profile;
}

View File

@ -5,7 +5,7 @@ class Tourmanent
/**
* @param {Client} client
*/
constructor(client, name = undefined, nb_players = undefined, nb_players_by_game = undefined, level = undefined, started = undefined, finished = undefined, levels = undefined, id = undefined)
constructor(client, id, name = undefined, nb_players = undefined, nb_players_by_game = undefined, level = undefined, started = undefined, finished = undefined, levels = undefined, state = undefined)
{
/**
* @type {Client}
@ -18,28 +18,18 @@ class Tourmanent
this.started = started;
this.finished = finished;
this.levels = levels;
this.state = this.get_state();
this.state = state;
this.id = id
this.connected = false;
}
get_state()
{
if (this.finished)
return "finished";
if (this.started)
return "started";
else
return "waiting";
}
async init(id)
async init()
{
let response = await this.client._get(`/api/tournaments/${id}`);
if (response.status === 404)
return 1;
if (response.status !== 200)
return response.status;
let response_data = await response.json();
@ -50,8 +40,7 @@ class Tourmanent
this.started = response_data.started;
this.finished = response_data.finished;
this.levels = response_data.levels;
this.id = response_data.id
this.state = this.get_state();
this.state = response_data.state;
}
leave(event)

View File

@ -26,11 +26,8 @@ class Tourmanents
{
let response = await this.client._post("/api/tournaments/", {nb_players: nb_players, nb_players_by_game: nb_players_by_game, name: name});
if (response.status === 403)
{
this.client._update_logged(false);
return null;
}
if (response.status !== 200)
return response.status;
let response_data = await response.json();
return response_data;